Commit ebd147b3 authored by Arnim Läuger's avatar Arnim Läuger Committed by Marcel Stör
Browse files

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.
parent f801bf12
...@@ -156,7 +156,7 @@ int dht_read11(uint8_t pin) ...@@ -156,7 +156,7 @@ int dht_read11(uint8_t pin)
// TEST CHECKSUM // TEST CHECKSUM
// dht_bytes[1] && dht_bytes[3] both 0 // 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; if (dht_bytes[4] != sum) return DHTLIB_ERROR_CHECKSUM;
return DHTLIB_OK; return DHTLIB_OK;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment