Fix DHT driver

Fix DHT driver mixing values for different sensors (#1797)
This commit is contained in:
Theo Arends 2018-07-09 17:59:56 +02:00
parent 0c902b7d64
commit 39a0867b6a
2 changed files with 23 additions and 30 deletions

View File

@ -1,4 +1,5 @@
/* 6.1.0a /* 6.1.0a
* Fix DHT driver mixing values for different sensors (#1797)
* Change DHT driver to provide better instant results and add decimals to DHT11 (#3164) * Change DHT driver to provide better instant results and add decimals to DHT11 (#3164)
* Change DS18x20 driver to provide better instant results (#3169) * Change DS18x20 driver to provide better instant results (#3169)
* Change DS18B20 driver to provide better instant results * Change DS18B20 driver to provide better instant results

View File

@ -28,7 +28,6 @@
#define DHT_MAX_SENSORS 3 #define DHT_MAX_SENSORS 3
#define DHT_MAX_RETRY 8 #define DHT_MAX_RETRY 8
#define MIN_INTERVAL 2000
uint32_t dht_max_cycles; uint32_t dht_max_cycles;
uint8_t dht_data[5]; uint8_t dht_data[5];
@ -63,15 +62,10 @@ int32_t DhtExpectPulse(byte sensor, bool level)
return count; return count;
} }
void DhtRead(byte sensor) boolean DhtRead(byte sensor)
{ {
int32_t cycles[80]; int32_t cycles[80];
uint32_t currenttime = millis(); uint8_t error = 0;
if ((currenttime - Dht[sensor].lastreadtime) < MIN_INTERVAL) {
return;
}
Dht[sensor].lastreadtime = currenttime;
dht_data[0] = dht_data[1] = dht_data[2] = dht_data[3] = dht_data[4] = 0; dht_data[0] = dht_data[1] = dht_data[2] = dht_data[3] = dht_data[4] = 0;
@ -99,27 +93,27 @@ void DhtRead(byte sensor)
delayMicroseconds(10); delayMicroseconds(10);
if (-1 == 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++; error = 1;
return;
} }
if (-1 == DhtExpectPulse(sensor, HIGH)) { else 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++; error = 1;
return;
} }
for (int i = 0; i < 80; i += 2) { else {
cycles[i] = DhtExpectPulse(sensor, LOW); for (int i = 0; i < 80; i += 2) {
cycles[i+1] = DhtExpectPulse(sensor, HIGH); cycles[i] = DhtExpectPulse(sensor, LOW);
cycles[i+1] = DhtExpectPulse(sensor, HIGH);
}
} }
interrupts(); interrupts();
if (error) { return false; }
for (int i = 0; i < 40; ++i) { for (int i = 0; i < 40; ++i) {
int32_t lowCycles = cycles[2*i]; int32_t lowCycles = cycles[2*i];
int32_t highCycles = cycles[2*i+1]; int32_t highCycles = cycles[2*i+1];
if ((-1 == lowCycles) || (-1 == 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++; return false;
return;
} }
dht_data[i/8] <<= 1; dht_data[i/8] <<= 1;
if (highCycles > lowCycles) { if (highCycles > lowCycles) {
@ -131,12 +125,12 @@ void DhtRead(byte sensor)
dht_data[0], dht_data[1], dht_data[2], dht_data[3], dht_data[4], (dht_data[0] + dht_data[1] + dht_data[2] + dht_data[3]) & 0xFF); dht_data[0], dht_data[1], dht_data[2], dht_data[3], dht_data[4], (dht_data[0] + dht_data[1] + dht_data[2] + dht_data[3]) & 0xFF);
AddLog(LOG_LEVEL_DEBUG); AddLog(LOG_LEVEL_DEBUG);
if (dht_data[4] == ((dht_data[0] + dht_data[1] + dht_data[2] + dht_data[3]) & 0xFF)) { if (dht_data[4] != ((dht_data[0] + dht_data[1] + dht_data[2] + dht_data[3]) & 0xFF)) {
Dht[sensor].lastresult = 0;
} else {
AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_DHT D_CHECKSUM_FAILURE)); AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_DHT D_CHECKSUM_FAILURE));
Dht[sensor].lastresult++; return false;
} }
return true;
} }
void DhtReadTempHum(byte sensor) void DhtReadTempHum(byte sensor)
@ -145,8 +139,7 @@ void DhtReadTempHum(byte sensor)
Dht[sensor].t = NAN; Dht[sensor].t = NAN;
Dht[sensor].h = NAN; Dht[sensor].h = NAN;
} }
DhtRead(sensor); if (DhtRead(sensor)) {
if (!Dht[sensor].lastresult) {
switch (Dht[sensor].type) { switch (Dht[sensor].type) {
case GPIO_DHT11: case GPIO_DHT11:
Dht[sensor].h = dht_data[0]; Dht[sensor].h = dht_data[0];
@ -162,6 +155,9 @@ void DhtReadTempHum(byte sensor)
break; break;
} }
Dht[sensor].t = ConvertTemp(Dht[sensor].t); Dht[sensor].t = ConvertTemp(Dht[sensor].t);
Dht[sensor].lastresult = 0;
} else {
Dht[sensor].lastresult++;
} }
} }
@ -211,7 +207,6 @@ void DhtShow(boolean json)
char temperature[10]; char temperature[10];
char humidity[10]; char humidity[10];
byte dsxflg = 0;
for (byte i = 0; i < dht_sensors; i++) { for (byte i = 0; i < dht_sensors; i++) {
dtostrfd(Dht[i].t, Settings.flag2.temperature_resolution, temperature); dtostrfd(Dht[i].t, Settings.flag2.temperature_resolution, temperature);
dtostrfd(Dht[i].h, Settings.flag2.humidity_resolution, humidity); dtostrfd(Dht[i].h, Settings.flag2.humidity_resolution, humidity);
@ -219,19 +214,16 @@ void DhtShow(boolean json)
if (json) { if (json) {
snprintf_P(mqtt_data, sizeof(mqtt_data), JSON_SNS_TEMPHUM, mqtt_data, Dht[i].stype, temperature, humidity); snprintf_P(mqtt_data, sizeof(mqtt_data), JSON_SNS_TEMPHUM, mqtt_data, Dht[i].stype, temperature, humidity);
#ifdef USE_DOMOTICZ #ifdef USE_DOMOTICZ
if ((0 == tele_period) && !dsxflg) { if ((0 == tele_period) && (0 == i)) {
DomoticzTempHumSensor(temperature, humidity); DomoticzTempHumSensor(temperature, humidity);
dsxflg++;
} }
#endif // USE_DOMOTICZ #endif // USE_DOMOTICZ
#ifdef USE_KNX #ifdef USE_KNX
if (0 == tele_period) { if ((0 == tele_period) && (0 == i)) {
KnxSensor(KNX_TEMPERATURE, Dht[i].t); KnxSensor(KNX_TEMPERATURE, Dht[i].t);
KnxSensor(KNX_HUMIDITY, Dht[i].h); KnxSensor(KNX_HUMIDITY, Dht[i].h);
} }
#endif // USE_KNX #endif // USE_KNX
#ifdef USE_WEBSERVER #ifdef USE_WEBSERVER
} else { } else {
snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_SNS_TEMP, mqtt_data, Dht[i].stype, temperature, TempUnit()); snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_SNS_TEMP, mqtt_data, Dht[i].stype, temperature, TempUnit());