Fix DHT sensor timeout recognition

Fix DHT sensor timeout recognition by distinguish "signal already there"
from "timeout" (#1233)
This commit is contained in:
arendst 2017-11-22 11:02:23 +01:00
parent 9ab00b533c
commit 53b100fdeb
1 changed files with 11 additions and 11 deletions

View File

@ -51,13 +51,13 @@ void DhtReadPrep()
} }
} }
uint32_t DhtExpectPulse(byte sensor, bool level) int32_t DhtExpectPulse(byte sensor, bool level)
{ {
uint32_t count = 0; int32_t count = 0;
while (digitalRead(Dht[sensor].pin) == level) { while (digitalRead(Dht[sensor].pin) == level) {
if (count++ >= dht_max_cycles) { if (count++ >= dht_max_cycles) {
return 0; return -1; // Timeout
} }
} }
return count; return count;
@ -65,7 +65,7 @@ uint32_t DhtExpectPulse(byte sensor, bool level)
void DhtRead(byte sensor) void DhtRead(byte sensor)
{ {
uint32_t cycles[80]; int32_t cycles[80];
uint32_t currenttime = millis(); uint32_t currenttime = millis();
if ((currenttime - Dht[sensor].lastreadtime) < MIN_INTERVAL) { if ((currenttime - Dht[sensor].lastreadtime) < MIN_INTERVAL) {
@ -92,12 +92,12 @@ void DhtRead(byte sensor)
delayMicroseconds(40); delayMicroseconds(40);
pinMode(Dht[sensor].pin, INPUT_PULLUP); pinMode(Dht[sensor].pin, INPUT_PULLUP);
delayMicroseconds(10); delayMicroseconds(10);
if (0 == DhtExpectPulse(sensor, LOW)) { if (-1 == DhtExpectPulse(sensor, LOW)) {
AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_DHT D_TIMEOUT_WAITING_FOR " " D_START_SIGNAL_LOW " " D_PULSE)); AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_DHT D_TIMEOUT_WAITING_FOR " " D_START_SIGNAL_LOW " " D_PULSE));
Dht[sensor].lastresult++; Dht[sensor].lastresult++;
return; return;
} }
if (0 == DhtExpectPulse(sensor, HIGH)) { if (-1 == DhtExpectPulse(sensor, HIGH)) {
AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_DHT D_TIMEOUT_WAITING_FOR " " D_START_SIGNAL_HIGH " " D_PULSE)); AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_DHT D_TIMEOUT_WAITING_FOR " " D_START_SIGNAL_HIGH " " D_PULSE));
Dht[sensor].lastresult++; Dht[sensor].lastresult++;
return; return;
@ -108,17 +108,17 @@ void DhtRead(byte sensor)
} }
interrupts(); interrupts();
for (int i=0; i<40; ++i) { for (int i = 0; i < 40; ++i) {
uint32_t lowCycles = cycles[2*i]; int32_t lowCycles = cycles[2*i];
uint32_t highCycles = cycles[2*i+1]; int32_t highCycles = cycles[2*i+1];
if ((0 == lowCycles) || (0 == highCycles)) { if ((-1 == lowCycles) || (-1 == highCycles)) {
AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_DHT D_TIMEOUT_WAITING_FOR " " D_PULSE)); AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_DHT D_TIMEOUT_WAITING_FOR " " D_PULSE));
Dht[sensor].lastresult++; Dht[sensor].lastresult++;
return; return;
} }
dht_data[i/8] <<= 1; dht_data[i/8] <<= 1;
if (highCycles > lowCycles) { if (highCycles > lowCycles) {
dht_data[i/8] |= 1; dht_data[i / 8] |= 1;
} }
} }