DHT11 read sometimes failed with checksum error (#2679)

* DHT11 read sometimes failed with checksum error.

The code assumed DHT11 devices only ever return zero in the temperature and humidity decimal fraction bytes. The datasheet doesn't guarantee this is the case, and by observation I have noticed that indeed the DHT11 may sometimes return another number, usually close to zero. This means that the code would fail with a checksum error, as the fraction bytes were not included when the checksum was calculated. These bytes are now taken into account and also returned as part of the measurement.

This also means that the related dht.read() function is non-functional. If you have a DHT11 device that returns a non-zero decimal part, dht.read() will interpret it as a DHT22 result and return the wrong measurement. For this reason dht.read() should be retired. This patch does not address this issue.
This commit is contained in:
Arnim Läuger 2019-02-23 22:03:21 +01:00 committed by Marcel Stör
parent f801bf126d
commit ebd147b34f
1 changed files with 1 additions and 1 deletions

View File

@ -156,7 +156,7 @@ int dht_read11(uint8_t pin)
// TEST CHECKSUM
// dht_bytes[1] && dht_bytes[3] both 0
uint8_t sum = dht_bytes[0] + dht_bytes[2];
uint8_t sum = dht_bytes[0] + dht_bytes[1] + dht_bytes[2] + dht_bytes[3];
if (dht_bytes[4] != sum) return DHTLIB_ERROR_CHECKSUM;
return DHTLIB_OK;