Skip to content

Commit

Permalink
add a new DHT11_2022 type for 0.01 °C resolution devices
Browse files Browse the repository at this point in the history
  • Loading branch information
hugueslarrive committed Jun 13, 2023
1 parent 44ebdad commit e62c6b8
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 14 deletions.
11 changes: 5 additions & 6 deletions drivers/dht/dht.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ static int _parse_raw_values(dht_t *dev, uint8_t *data)

switch (dev->params.type) {
case DHT11:
case DHT11_2022:
DEBUG_PUTS("[dht] parse raw values with DHT11 data format");
dev->last_val.humidity = data[BYTEPOS_HUMIDITY_HIGH] * 10
+ data[BYTEPOS_HUMIDITY_LOW];
Expand All @@ -155,13 +156,10 @@ static int _parse_raw_values(dht_t *dev, uint8_t *data)
data[BYTEPOS_TEMPERATURE_LOW] &= ~0x80;
/* 2022-12 aosong.com data sheet uses interprets low bits as
* 0.01°C per LSB */
if (!dev->params.lsb_is_hundreds && data[BYTEPOS_TEMPERATURE_LOW] > 9) {
/* in case of miss configuration */
dev->params.lsb_is_hundreds = 1;
}
if (dev->params.lsb_is_hundreds) {
if (dev->params.type == DHT11_2022) {
data[BYTEPOS_TEMPERATURE_LOW] /= 10;
}
assert(data[BYTEPOS_TEMPERATURE_LOW] < 10);
dev->last_val.temperature = data[BYTEPOS_TEMPERATURE_HIGH] * 10
+ data[BYTEPOS_TEMPERATURE_LOW];
break;
Expand Down Expand Up @@ -198,7 +196,8 @@ int dht_init(dht_t *dev, const dht_params_t *params)
/* check parameters and configuration */
assert(dev && params);
/* AM2301 == DHT21 == DHT22 (same value in enum) */
assert((params->type == DHT11) || (params->type == DHT22));
assert((params->type == DHT11) || (params->type == DHT11_2022)
|| (params->type == DHT22));

memset(dev, 0, sizeof(dht_t));
dev->params = *params;
Expand Down
5 changes: 0 additions & 5 deletions drivers/dht/include/dht_params.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,12 @@ extern "C" {
#ifndef DHT_PARAM_TYPE
#define DHT_PARAM_TYPE (DHT11)
#endif
#ifndef DHT_PARAM_LSB_IS_HUNDREDS
#define DHT_PARAM_LSB_IS_HUNDREDS (0)
#endif
#ifndef DHT_PARAM_PULL
#define DHT_PARAM_PULL (GPIO_IN)
#endif
#ifndef DHT_PARAMS
#define DHT_PARAMS { .pin = DHT_PARAM_PIN, \
.type = DHT_PARAM_TYPE, \
.lsb_is_hundreds = \
DHT_PARAM_LSB_IS_HUNDREDS, \
.in_mode = DHT_PARAM_PULL }
#endif
#ifndef DHT_SAULINFO
Expand Down
6 changes: 3 additions & 3 deletions drivers/include/dht.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ typedef struct {
* @brief Device type of the DHT device
*/
typedef enum {
DHT11, /**< DHT11 device identifier */
DHT11, /**< Older DHT11 variants with either 1 °C or
* 0.1 °C resolution */
DHT11_2022, /**< New DHT11 variant with 0.01 °C resolution */
DHT22, /**< DHT22 device identifier */
DHT21 = DHT22, /**< DHT21 device identifier */
AM2301 = DHT22, /**< AM2301 device identifier */
Expand All @@ -75,8 +77,6 @@ typedef enum {
typedef struct {
gpio_t pin; /**< GPIO pin of the device's data pin */
dht_type_t type; /**< type of the DHT device */
bool lsb_is_hundreds; /**< 2022-12 aosong.com data sheet uses interprets
* low bits as 0.01°C per LSB */
gpio_mode_t in_mode; /**< input pin configuration, with or without pull
* resistor */
} dht_params_t;
Expand Down

0 comments on commit e62c6b8

Please sign in to comment.