Skip to content

Commit

Permalink
Fix negative temperatures for DHT22 (#16)
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
danielfaust authored and winlinvip committed Jan 2, 2018
1 parent 5e91c5d commit 3c63797
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion SimpleDHT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ int SimpleDHT22::read2(int pin, float* ptemperature, float* phumidity, byte pdat
memcpy(pdata, data, 40);
}
if (ptemperature) {
*ptemperature = (float)temperature / 10.0;
*ptemperature = (float)((temperature & 0x8000 ? -1 : 1) * (temperature & 0x7FFF)) / 10.0;
}
if (phumidity) {
*phumidity = (float)humidity / 10.0;
Expand Down

0 comments on commit 3c63797

Please sign in to comment.