I get a timing error on every 4th bit

No idea why
This commit is contained in:
2023-10-30 21:38:35 +00:00
parent 6b40f97ba5
commit 089d8a62f1
2 changed files with 56 additions and 33 deletions

View File

@@ -27,9 +27,28 @@ void dht22_handle_error(dht22_error e) {
} }
} }
uint32_t wait_for_signal_to_change_from(int signal_level) {
uint32_t tick_total = 0;
uint64_t start_time = esp_timer_get_time();
while (gpio_get_level(CONFIG_DHT22_DATA_GPIO) == signal_level) {
tick_total += 1;
// TODO: Find a better way to determine the max timeout - perhaps find number of ticks
// in 50ms or something
// Or perhaps do actually run esp_timer_get_time in the loop or something
if (tick_total == (UINT32_MAX - 1)) {
return DHT22_TIMING_ERROR;
}
}
uint64_t next_time = esp_timer_get_time();
uint32_t elapsed_time = next_time - start_time;
return elapsed_time;
}
dht22_error dht22_read() { dht22_error dht22_read() {
uint8_t data[5] = {0}; uint8_t data[5] = {0};
uint32_t tick_total = 0; uint32_t elapsed_time;
/* /*
* From datasheet: * From datasheet:
@@ -56,43 +75,47 @@ dht22_error dht22_read() {
// wait for a while - should only be 20 uS // wait for a while - should only be 20 uS
} }
elapsed_time = wait_for_signal_to_change_from(0);
uint64_t start_time = esp_timer_get_time();
while (gpio_get_level(CONFIG_DHT22_DATA_GPIO) == 0) {
tick_total += 1;
if (tick_total == UINT32_MAX - 1) {
return DHT22_TIMING_ERROR;
}
}
uint64_t next_time = esp_timer_get_time();
uint32_t elapsed_time = next_time - start_time;
if (elapsed_time < 75 || elapsed_time > 85) {
return DHT22_TIMING_ERROR;
}
tick_total = 0;
start_time = esp_timer_get_time();
while (gpio_get_level(CONFIG_DHT22_DATA_GPIO) == 0) {
tick_total += 1;
if (tick_total == UINT32_MAX - 1) {
return DHT22_TIMING_ERROR;
}
}
next_time = esp_timer_get_time();
elapsed_time = next_time - start_time;
portENABLE_INTERRUPTS();
ESP_LOGW(TAG, "etime: %lu", elapsed_time);
if (elapsed_time < 70 || elapsed_time > 90) { if (elapsed_time < 70 || elapsed_time > 90) {
return DHT22_TIMING_ERROR; return DHT22_TIMING_ERROR;
} }
elapsed_time = wait_for_signal_to_change_from(1);
if (elapsed_time < 70 || elapsed_time > 90) {
return DHT22_TIMING_ERROR;
}
/*
* From datasheet:
* - DHT pulls low 50us -> into to next bit
* - DHT pulls high:
* - if 26-28us -> 0 bit
* - if 70us -> 1 bit
*/
portENABLE_INTERRUPTS();
for (int byte_i = 4; byte_i >= 0; byte_i--) {
for (int bit_i = 0; bit_i <= 8; bit_i++) {
elapsed_time = wait_for_signal_to_change_from(0);
if (elapsed_time < 35 || elapsed_time > 65) {
return DHT22_TIMING_ERROR;
}
elapsed_time = wait_for_signal_to_change_from(1);
/*
if (elapsed_time < 60 || elapsed_time > 80) {
data[byte_i] |= (1 << bit_i);
}
*/
ESP_LOGE(TAG, "elapsed_time: %lu", elapsed_time);
}
}
// TODO test that data[x] << 8 will be promoted to 16bits otherwise I'm // TODO test that data[x] << 8 will be promoted to 16bits otherwise I'm
// shifting them to just zeros // shifting them to just zeros
_dht22_relative_humidity = data[0] << 8 | data[1]; _dht22_relative_humidity = (data[0] << 8) | data[1];
_dht22_temperature = (data[2] & 0x7f) << 8 | data[3]; _dht22_temperature = ((data[2] & 0x7f) << 8) | data[3];
if (data[2] & 0x80) { if (data[2] & 0x80) {
_dht22_temperature = -1 * _dht22_temperature; _dht22_temperature = -1 * _dht22_temperature;
} }

View File

@@ -70,12 +70,12 @@ void read_from_dht22() {
portDISABLE_INTERRUPTS(); portDISABLE_INTERRUPTS();
int ret = dht22_read(); int ret = dht22_read();
portENABLE_INTERRUPTS(); portENABLE_INTERRUPTS();
ESP_LOGE(TAG, "dht_ret: %i", ret);
if (ret != DHT22_OK) { if (ret != DHT22_OK) {
dht22_handle_error(ret); dht22_handle_error(ret);
} else { } else {
latest_datapoint.temp = dht22_temperature(); latest_datapoint.temp = dht22_temperature();
latest_datapoint.rh = dht22_relative_humidity(); latest_datapoint.rh = dht22_relative_humidity();
ESP_LOGW(TAG, "%f & %f", latest_datapoint.temp / 10.0, latest_datapoint.rh / 10.0 );
if (has_ntp_time_obtained_once()) { if (has_ntp_time_obtained_once()) {
// I _think_ I only want to be doing that rolling average and saving // I _think_ I only want to be doing that rolling average and saving