diff --git a/adafruit_dht.py b/adafruit_dht.py index 068405f..e7e8595 100644 --- a/adafruit_dht.py +++ b/adafruit_dht.py @@ -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]: @@ -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.")