From 383d5a8a5d918b8052d30112ef08a846a60837ea Mon Sep 17 00:00:00 2001 From: Max Prokhorov Date: Thu, 21 Nov 2019 01:29:58 +0300 Subject: [PATCH] DHTSensor: fix Si7021 compatibility (#2000) * sensor: add DHT_CHIP_SI7021 references * sensor: update DHT_TYPE definitions, use enum * changelog --- CHANGELOG.md | 3 +- code/espurna/config/hardware.h | 1 + code/espurna/sensors/DHTSensor.h | 62 ++++++++++++++++++++++++-------- 3 files changed, 51 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 21b7b62c..3f5d99c8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -54,7 +54,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Fix stored energy values when using kWh ([#1334](https://github.com/xoseperez/espurna/issues/1334) - Remove pinMode(0, ...) from AnalogSensor ([#1777](https://github.com/xoseperez/espurna/issues/1777), [#1827](https://github.com/xoseperez/espurna/issues/1827)) - Check value range for PMSX005 and SenseAir CO2 sensor ([#1865](https://github.com/xoseperez/espurna/issues/1865), thanks to **[@Yonsm](https://github.com/Yonsm)**) -- DHT: Increase read delay per datasheet value ([#1918](https://github.com/xoseperez/espurna/issues/1918), [#1979](https://github.com/xoseperez/espurna/issues/1979), thanks to **[@JavierAder](https://github.com/JavierAder)** and **[@structuralB](https://github.com/structuralB)**) +- DHT: Increase read delay to 1100 usec per datasheet value for `DHT_CHIP_DHT22` ([#1918](https://github.com/xoseperez/espurna/issues/1918), [#1979](https://github.com/xoseperez/espurna/issues/1979), thanks to **[@JavierAder](https://github.com/JavierAder)** and **[@structuralB](https://github.com/structuralB)**) +- DHT: Add `DHT_CHIP_SI7021` for `ITEAD_SONOFF_TH`, use 500 usec read delay ([#1918](https://github.com/xoseperez/espurna/issues/1918#issuecomment-555672628), thanks to **[@icevoodoo](https://github.com/icevoodoo)**) - DHT: Set pin mode before digitalWrite ([#1979](https://github.com/xoseperez/espurna/issues/1979)) - DHT: Wait DHT_MIN_INTERVAL after initialization ([#1979](https://github.com/xoseperez/espurna/issues/1979)) #### Build diff --git a/code/espurna/config/hardware.h b/code/espurna/config/hardware.h index b0606209..ffca25c5 100644 --- a/code/espurna/config/hardware.h +++ b/code/espurna/config/hardware.h @@ -398,6 +398,7 @@ #define DHT_SUPPORT 1 #endif #define DHT_PIN 14 + #define DHT_TYPE DHT_CHIP_SI7021 //#define I2C_SDA_PIN 4 //#define I2C_SCL_PIN 14 diff --git a/code/espurna/sensors/DHTSensor.h b/code/espurna/sensors/DHTSensor.h index 626907c9..4f199701 100644 --- a/code/espurna/sensors/DHTSensor.h +++ b/code/espurna/sensors/DHTSensor.h @@ -10,17 +10,44 @@ #include "Arduino.h" #include "BaseSensor.h" -#define DHT_MAX_DATA 5 -#define DHT_MAX_ERRORS 5 -#define DHT_MIN_INTERVAL 2000 - -#define DHT_CHIP_DHT11 11 -#define DHT_CHIP_DHT12 12 -#define DHT_CHIP_DHT22 22 -#define DHT_CHIP_DHT21 21 -#define DHT_CHIP_AM2301 21 +constexpr const double DHT_DUMMY_VALUE = -255; +constexpr const size_t DHT_MAX_DATA = 5; +constexpr const size_t DHT_MAX_ERRORS = 5; +constexpr const uint32_t DHT_MIN_INTERVAL = 2000; + +enum class DHTChipType { + DHT11, + DHT12, + DHT21, + DHT22, + AM2301, + SI7021 +}; -#define DHT_DUMMY_VALUE -255 +// Note: backwards compatibility for configuration headers +#define DHT_CHIP_DHT11 DHTChipType::DHT11 +#define DHT_CHIP_DHT12 DHTChipType::DHT12 +#define DHT_CHIP_DHT22 DHTChipType::DHT22 +#define DHT_CHIP_DHT21 DHTChipType::DHT21 +#define DHT_CHIP_AM2301 DHTChipType::AM2301 +#define DHT_CHIP_SI7021 DHTChipType::SI7021 + +int dhtchip_to_number(DHTChipType chip) { + switch (chip) { + case DHTChipType::DHT11: + return 11; + case DHTChipType::DHT12: + return 12; + case DHTChipType::DHT21: + case DHTChipType::AM2301: + return 21; + case DHTChipType::DHT22: + case DHTChipType::SI7021: + return 22; + default: + return -1; + } +} class DHTSensor : public BaseSensor { @@ -45,7 +72,7 @@ class DHTSensor : public BaseSensor { _gpio = gpio; } - void setType(unsigned char type) { + void setType(DHTChipType type) { _type = type; } @@ -55,7 +82,11 @@ class DHTSensor : public BaseSensor { return _gpio; } - unsigned char getType() { + int getType() { + return dhtchip_to_number(_type); + } + + DHTChipType getChipType() { return _type; } @@ -94,7 +125,7 @@ class DHTSensor : public BaseSensor { // Descriptive name of the sensor String description() { char buffer[20]; - snprintf(buffer, sizeof(buffer), "DHT%d @ GPIO%d", _type, _gpio); + snprintf(buffer, sizeof(buffer), "DHT%d @ GPIO%d", dhtchip_to_number(_type), _gpio); return String(buffer); } @@ -158,6 +189,8 @@ class DHTSensor : public BaseSensor { digitalWrite(_gpio, LOW); if ((_type == DHT_CHIP_DHT11) || (_type == DHT_CHIP_DHT12)) { nice_delay(20); + } else if (_type == DHT_CHIP_SI7021) { + delayMicroseconds(500); } else { delayMicroseconds(1100); } @@ -248,9 +281,10 @@ class DHTSensor : public BaseSensor { return uSec; } + DHTChipType _type = DHT_CHIP_DHT22; + unsigned char _gpio = GPIO_NONE; unsigned char _previous = GPIO_NONE; - unsigned char _type = DHT_CHIP_DHT22; unsigned long _last_ok = 0; unsigned char _errors = 0;