Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DHTSensor: fix Si7021 compatibility #2000

Merged
merged 4 commits into from
Nov 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions code/espurna/config/hardware.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
62 changes: 48 additions & 14 deletions code/espurna/sensors/DHTSensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -45,7 +72,7 @@ class DHTSensor : public BaseSensor {
_gpio = gpio;
}

void setType(unsigned char type) {
void setType(DHTChipType type) {
_type = type;
}

Expand All @@ -55,7 +82,11 @@ class DHTSensor : public BaseSensor {
return _gpio;
}

unsigned char getType() {
int getType() {
return dhtchip_to_number(_type);
}

DHTChipType getChipType() {
return _type;
}

Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
Expand Down