From 7b29ea24dbf28e644759d07911ed234544b2b157 Mon Sep 17 00:00:00 2001 From: Arthur Roberts Date: Tue, 31 Oct 2023 21:24:21 +0000 Subject: [PATCH] Cleanedup code and added some logging I think the averaging is working - will need to check the 10 minutes --- components/dht22/dht22.c | 15 +++------------ main/main.c | 16 ++++++++-------- 2 files changed, 11 insertions(+), 20 deletions(-) diff --git a/components/dht22/dht22.c b/components/dht22/dht22.c index 13dde69..e3bf2c3 100644 --- a/components/dht22/dht22.c +++ b/components/dht22/dht22.c @@ -76,10 +76,7 @@ dht22_error dht22_read() { if (elapsed_time == DHT22_TIMEOUT_ERROR) { portENABLE_INTERRUPTS(); return DHT22_TIMEOUT_ERROR; - } /*else if (elapsed_time > 50) { - portENABLE_INTERRUPTS(); - return DHT22_TIMING_ERROR; - }*/ + } /* * From datasheet: @@ -91,19 +88,13 @@ dht22_error dht22_read() { if (elapsed_time == DHT22_TIMEOUT_ERROR) { portENABLE_INTERRUPTS(); return DHT22_TIMEOUT_ERROR; - } /*else if (elapsed_time < 50 || elapsed_time > 110) { - portENABLE_INTERRUPTS(); - return DHT22_TIMING_ERROR; - }*/ + } elapsed_time = wait_for_signal_to_change_from(1); if (elapsed_time == DHT22_TIMEOUT_ERROR) { portENABLE_INTERRUPTS(); return DHT22_TIMEOUT_ERROR; - } /*else if (elapsed_time < 50 || elapsed_time > 110) { - portENABLE_INTERRUPTS(); - return DHT22_TIMING_ERROR; - }*/ + } /* * From datasheet: diff --git a/main/main.c b/main/main.c index 75fdb14..761a145 100644 --- a/main/main.c +++ b/main/main.c @@ -17,6 +17,7 @@ #include #include #include +#include static const char *TAG = "YASPAM"; @@ -64,9 +65,8 @@ uint32_t num_samples_last_ten_mins = 0; void read_from_dht22() { // From datasheet: - // Don't start within 1 second of powering on - // FIXME - still doesn't actually fix the problem though - //vTaskDelay(pdMS_TO_TICKS(20000)); + // - Don't start within 1 second of powering on + vTaskDelay(pdMS_TO_TICKS(1000)); while (run_dht) { int ret = dht22_read(); @@ -96,13 +96,13 @@ void read_from_dht22() { num_samples_last_ten_mins; } num_samples_last_ten_mins += 1; + ESP_LOGD(TAG, "ten_minute_average.temp: %"PRIu16", num_samples: %"PRIu32, ten_minute_rolling_average.temp, num_samples_last_ten_mins); - // This should be: if (number of samples in 10 mins taken) - // This _will_ break if sampled too quickly, because we'll have more - // samples per 10 minutes than can be stored in a uint32 - I'll - // calculate what that is and perhaps raise a compile error if too high. + // This should be: if (10 minutes of sample taken) + // TODO: check max samples / second that can fit in 10 minutes if (num_samples_last_ten_mins >= - ((uint32_t) CONFIG_DHT22_PERIOD_POLL * 1000 * 60 * 10)) { + ((uint32_t)CONFIG_DHT22_PERIOD_POLL * 1000 * 60 * 10)) { + ESP_LOGW(TAG, "10 minutes of samples collected. Average temp: %i", ten_minute_rolling_average.temp); num_samples_last_ten_mins = 0; time_t epoch;