Skip to content

fix DHT22 handling of negative temperatures #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 29, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions adafruit_dht.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,18 +148,24 @@ def measure(self):
for byte_start in range(0, 80, 16):
buf.append(self._pulses_to_binary(pulses, byte_start, byte_start+16))

# humidity is 2 bytes
if self._dht11:
# humidity is 1 byte
self._humidity = buf[0]
else:
# humidity is 2 bytes
self._humidity = ((buf[0]<<8) | buf[1]) / 10

# temperature is 2 bytes
if self._dht11:
# temperature is 1 byte
self._temperature = buf[2]
else:
self._temperature = ((buf[2]<<8) | buf[3]) / 10

# temperature is 2 bytes
# MSB is sign, bits 0-14 are magnitude)
raw_temperature = (((buf[2] & 0x7f)<<8) | buf[3]) / 10
# set sign
if buf[2] & 0x80:
raw_temperature = -raw_temperature
self._temperature = raw_temperature
# calc checksum
chk_sum = 0
for b in buf[0:4]:
Expand All @@ -170,7 +176,6 @@ def measure(self):
# check sum failed to validate
raise RuntimeError("Checksum did not validate. Try again.")


else:
raise RuntimeError("A full buffer was not returned. Try again.")

Expand Down