Every 4th byte comes back with zero time elapsed high.

Really don't know why
This commit is contained in:
2023-10-30 22:11:39 +00:00
parent 089d8a62f1
commit e139aa7e9c
3 changed files with 18 additions and 12 deletions

View File

@@ -24,25 +24,28 @@ void dht22_handle_error(dht22_error e) {
ESP_LOGE(TAG, "Error calculating checksum"); ESP_LOGE(TAG, "Error calculating checksum");
} else if (e == DHT22_TIMING_ERROR) { } else if (e == DHT22_TIMING_ERROR) {
ESP_LOGE(TAG, "Timing error while obtaining data"); ESP_LOGE(TAG, "Timing error while obtaining data");
} else if (e == DHT22_TIMEOUT_ERROR) {
ESP_LOGE(TAG, "Timeout error - waited >2ms for signal to change");
} else {
ESP_LOGE(TAG, "Unknown error: %i", e);
} }
} }
uint32_t wait_for_signal_to_change_from(int signal_level) { uint32_t wait_for_signal_to_change_from(int signal_level) {
uint32_t tick_total = 0;
uint64_t start_time = esp_timer_get_time(); uint64_t start_time = esp_timer_get_time();
uint64_t next_time = start_time;
while (gpio_get_level(CONFIG_DHT22_DATA_GPIO) == signal_level) { while (gpio_get_level(CONFIG_DHT22_DATA_GPIO) == signal_level) {
tick_total += 1; next_time = esp_timer_get_time();
// TODO: Find a better way to determine the max timeout - perhaps find number of ticks if ((next_time - start_time) >= 1000 * 10) {
// in 50ms or something return DHT22_TIMEOUT_ERROR;
// 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; uint32_t elapsed_time = next_time - start_time;
if (elapsed_time == 0) {
return 1000;
}
return elapsed_time; return elapsed_time;
} }
@@ -97,19 +100,19 @@ dht22_error dht22_read() {
for (int bit_i = 0; bit_i <= 8; bit_i++) { for (int bit_i = 0; bit_i <= 8; bit_i++) {
elapsed_time = wait_for_signal_to_change_from(0); elapsed_time = wait_for_signal_to_change_from(0);
if (elapsed_time == DHT22_TIMEOUT_ERROR) {
return DHT22_TIMEOUT_ERROR;
}
if (elapsed_time < 35 || elapsed_time > 65) { if (elapsed_time < 35 || elapsed_time > 65) {
return DHT22_TIMING_ERROR; return elapsed_time;
} }
elapsed_time = wait_for_signal_to_change_from(1); elapsed_time = wait_for_signal_to_change_from(1);
/*
if (elapsed_time < 60 || elapsed_time > 80) { if (elapsed_time < 60 || elapsed_time > 80) {
data[byte_i] |= (1 << bit_i); data[byte_i] |= (1 << bit_i);
} }
*/
ESP_LOGE(TAG, "elapsed_time: %lu", elapsed_time); 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

View File

@@ -1,9 +1,11 @@
#pragma once #pragma once
#include <limits.h>
#include <stdint.h> #include <stdint.h>
typedef enum { typedef enum {
DHT22_OK, DHT22_OK,
DHT22_TIMEOUT_ERROR,
DHT22_TIMING_ERROR, DHT22_TIMING_ERROR,
DHT22_CHECKSUM_ERROR, DHT22_CHECKSUM_ERROR,
} dht22_error; } dht22_error;

View File

@@ -73,6 +73,7 @@ void read_from_dht22() {
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 ); ESP_LOGW(TAG, "%f & %f", latest_datapoint.temp / 10.0, latest_datapoint.rh / 10.0 );