Skip to content

Commit 3c63797

Browse files
danielfaustwinlinvip
authored andcommitted
Fix negative temperatures for DHT22 (#16)
This commit fixes issue #14 The MSB carries the sign information, which is filtered with `& 0x8000`. If the flag is present, the remainder will get multiplied by `-1`. All the remaining bits contain the value and are filtered with `& 0x7FFF`.
1 parent 5e91c5d commit 3c63797

1 file changed

Lines changed: 1 addition & 1 deletion

File tree

SimpleDHT.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ int SimpleDHT22::read2(int pin, float* ptemperature, float* phumidity, byte pdat
202202
memcpy(pdata, data, 40);
203203
}
204204
if (ptemperature) {
205-
*ptemperature = (float)temperature / 10.0;
205+
*ptemperature = (float)((temperature & 0x8000 ? -1 : 1) * (temperature & 0x7FFF)) / 10.0;
206206
}
207207
if (phumidity) {
208208
*phumidity = (float)humidity / 10.0;

0 commit comments

Comments
 (0)