From e8c1dc0647ce4ad392fd7e80899662f39803cfe5 Mon Sep 17 00:00:00 2001 From: Arthur Roberts Date: Wed, 25 Oct 2023 22:11:04 +0100 Subject: [PATCH] Added a random error checking function Not actually sure I'll use it? --- components/dht22/dht22.c | 10 +++++++++- components/dht22/dht22.h | 1 + main/main.c | 12 ++++++++---- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/components/dht22/dht22.c b/components/dht22/dht22.c index e707979..d7d49e0 100644 --- a/components/dht22/dht22.c +++ b/components/dht22/dht22.c @@ -12,6 +12,15 @@ static const char* TAG = "DHT22"; deci_percent _dht22_relative_humidity = UINT16_MAX; deci_degrees_c _dht22_temperature = INT16_MIN; +void dht22_handle_error(dht22_error e) { + if (e == DHT22_OK) { + ESP_LOGI(TAG, "No errors reading DHT22 value"); + } else if (e == DHT22_CHECKSUM_ERROR) { + ESP_LOGE(TAG, "Error calculating checksum"); + } else if (e == DHT22_TIMING_ERROR) { + ESP_LOGE(TAG, "Timing error while obtaining data"); + } +} dht22_error dht22_read() { uint8_t data[5] = {0}; @@ -28,7 +37,6 @@ dht22_error dht22_read() { if (checksum_val != data[4]) { _dht22_relative_humidity = UINT16_MAX; _dht22_temperature = INT16_MIN; - ESP_LOGE(TAG, "Error in final checksum check"); return DHT22_CHECKSUM_ERROR; } diff --git a/components/dht22/dht22.h b/components/dht22/dht22.h index 7f26cdc..6d5a394 100644 --- a/components/dht22/dht22.h +++ b/components/dht22/dht22.h @@ -11,6 +11,7 @@ typedef enum { typedef uint16_t deci_percent; typedef int16_t deci_degrees_c; +void dht22_handle_error(); deci_percent dht22_relative_humidity(); deci_degrees_c dht22_temperature(); dht22_error dht22_read(); diff --git a/main/main.c b/main/main.c index bc44e37..cd6e871 100644 --- a/main/main.c +++ b/main/main.c @@ -20,8 +20,8 @@ static const char* TAG = "YASPAM"; static volatile bool run_dht = true; static volatile bool run_pms5003 = true; -static TaskHandle_t dht22_handle; -static TaskHandle_t pms5003_handle; +static TaskHandle_t dht22_handle = NULL; +static TaskHandle_t pms5003_handle = NULL; #ifdef CONFIG_PMS5003_ENABLED #include "pms5003.h" @@ -49,8 +49,12 @@ read_from_dht22() portDISABLE_INTERRUPTS(); int ret = dht22_read(); portENABLE_INTERRUPTS(); - ESP_LOGI(TAG, "Got DHT22 data!"); - // TODO: Write the data out somewhere - into RAM maybe? + if (ret != DHT22_OK) { + dht22_handle_error(ret); + } else { + ESP_LOGI(TAG, "Got DHT22 data!"); + // TODO: Write the data out somewhere - into RAM maybe? + } vTaskDelay(pdMS_TO_TICKS(CONFIG_DHT22_PERIOD_POLL)); } }