From 6b9ec20b726f5fecf8d0d9881232f0ffbb0c968d Mon Sep 17 00:00:00 2001 From: Arthur Roberts Date: Mon, 23 Oct 2023 22:34:21 +0100 Subject: [PATCH] Started some DHT22 stuff Haven't tested it at all --- components/dht22/dht22.c | 34 ++++++++++++++++++++++++++++++---- components/dht22/dht22.h | 14 +++++++++++--- main/main.c | 2 +- 3 files changed, 42 insertions(+), 8 deletions(-) diff --git a/components/dht22/dht22.c b/components/dht22/dht22.c index 862cf18..efd683a 100644 --- a/components/dht22/dht22.c +++ b/components/dht22/dht22.c @@ -7,10 +7,36 @@ #error "Please define your DHT22 GPIO Data Pin" #endif /* CONFIG_DHT22_DATA_GPIO == -1 */ -dht22_error dht_read() { - return DHT_OK; +static const char* TAG = "DHT22"; +deci_percent _dht22_relative_humidity = UINT16_MAX; +deci_degrees_c _dht22_temperature = INT16_MIN; + + +dht22_error dht22_read() { + uint8_t data[5] = {0}; + + + // TODO: Actually figure out how to read the data from the chip. + + // TODO test that data[x] << 8 will be promoted to 16bits otherwise I'm shifting them to just zeros + _dht22_relative_humidity = data[0] << 8 | data[1]; + _dht22_temperature = (data[2] & 0x7f) << 8 | data[3]; + if (data[2] & 0x80) { + _dht22_temperature = -1 * _dht22_temperature; + } + uint8_t checksum_val = (data[0] + data[1] + data[2] + data[2]) & 0xff; + if (checksum_val != data[5]) { + _dht22_relative_humidity = UINT16_MAX; + _dht22_temperature = INT16_MIN; + return DHT22_CHECKSUM_ERROR; + } + + return DHT22_OK; } -int dht_temperature() { - return DATA_GPIO; +deci_percent dht22_relative_humidity() { + return _dht22_relative_humidity; +} +deci_degrees_c dht22_temperature() { + return _dht22_temperature; } diff --git a/components/dht22/dht22.h b/components/dht22/dht22.h index 2752291..7f26cdc 100644 --- a/components/dht22/dht22.h +++ b/components/dht22/dht22.h @@ -1,10 +1,18 @@ #pragma once +#include + typedef enum { - DHT_OK + DHT22_OK, + DHT22_TIMING_ERROR, + DHT22_CHECKSUM_ERROR, } dht22_error; -int dht_temperature(); -dht22_error dht_read(); +typedef uint16_t deci_percent; +typedef int16_t deci_degrees_c; + +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 ed8e535..d2f9506 100644 --- a/main/main.c +++ b/main/main.c @@ -11,7 +11,7 @@ void app_main(void) { - int ret = dht_read(); + int ret = dht22_temperature(); printf("Hello world! - dhtRet: %i\n", ret); for (int i = 10; i >= 0; i--) { printf("Restarting in %d seconds...\n", i);