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 fixes #1979

Merged
merged 3 commits into from
Nov 9, 2019
Merged
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
20 changes: 15 additions & 5 deletions code/espurna/sensors/DHTSensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#define DHT_CHIP_DHT21 21
#define DHT_CHIP_AM2301 21

#define DHT_DUMMY_VALUE -255

class DHTSensor : public BaseSensor {

public:
Expand Down Expand Up @@ -75,6 +77,9 @@ class DHTSensor : public BaseSensor {
}
_previous = _gpio;

// Set now to fail the check in _read at least once
_last_ok = millis();

_count = 2;
_ready = true;

Expand Down Expand Up @@ -126,7 +131,11 @@ class DHTSensor : public BaseSensor {
void _read() {

if ((_last_ok > 0) && (millis() - _last_ok < DHT_MIN_INTERVAL)) {
_error = SENSOR_ERROR_OK;
if ((_temperature == DHT_DUMMY_VALUE) && (_humidity == DHT_DUMMY_VALUE)) {
_error = SENSOR_ERROR_WARM_UP;
} else {
_error = SENSOR_ERROR_OK;
}
return;
}

Expand All @@ -137,19 +146,20 @@ class DHTSensor : public BaseSensor {
unsigned char byteInx = 0;
unsigned char bitInx = 7;

pinMode(_gpio, OUTPUT);

// Send start signal to DHT sensor
if (++_errors > DHT_MAX_ERRORS) {
_errors = 0;
digitalWrite(_gpio, HIGH);
nice_delay(250);
}
pinMode(_gpio, OUTPUT);
noInterrupts();
digitalWrite(_gpio, LOW);
if ((_type == DHT_CHIP_DHT11) || (_type == DHT_CHIP_DHT12)) {
nice_delay(20);
} else {
delayMicroseconds(500);
delayMicroseconds(1100);
}
digitalWrite(_gpio, HIGH);
delayMicroseconds(40);
Expand Down Expand Up @@ -245,8 +255,8 @@ class DHTSensor : public BaseSensor {
unsigned long _last_ok = 0;
unsigned char _errors = 0;

double _temperature = 0;
double _humidity = 0;
double _temperature = DHT_DUMMY_VALUE;
double _humidity = DHT_DUMMY_VALUE;

};

Expand Down