diff --git a/adafruit_dht.py b/adafruit_dht.py index f455fe6..068405f 100644 --- a/adafruit_dht.py +++ b/adafruit_dht.py @@ -100,7 +100,6 @@ def _get_pulses(self): pulses will have 81 elements for the DHT11/22 type devices. """ pulses = array.array('H') - tmono = time.monotonic() # create the PulseIn object using context manager with pulseio.PulseIn(self._pin, 81, True) as pulse_in: @@ -115,11 +114,10 @@ def _get_pulses(self): pulse_in.resume(self._trig_wait) # loop until we get the return pulse we need or - # time out after 1/2 seconds + # time out after 1/4 second + tmono = time.monotonic() while True: - if len(pulse_in) >= 80: - break - if time.monotonic()-tmono > 0.5: # time out after 1/2 seconds + if time.monotonic()-tmono > 0.25: # time out after 1/4 seconds break pulse_in.pause() @@ -137,17 +135,18 @@ def measure(self): Raises RuntimeError exception for checksum failure and for insuffcient data returned from the device (try again) """ - if time.monotonic()-self._last_called > 0.5: + delay_between_readings = 0.5 + if self._dht11: + delay_between_readings = 1.0 + if time.monotonic()-self._last_called > delay_between_readings: self._last_called = time.monotonic() pulses = self._get_pulses() - ##print(pulses) if len(pulses) >= 80: buf = array.array('B') for byte_start in range(0, 80, 16): buf.append(self._pulses_to_binary(pulses, byte_start, byte_start+16)) - #print(buf) # humidity is 2 bytes if self._dht11: @@ -155,7 +154,7 @@ def measure(self): else: self._humidity = ((buf[0]<<8) | buf[1]) / 10 - # tempature is 2 bytes + # temperature is 2 bytes if self._dht11: self._temperature = buf[2] else: @@ -170,15 +169,10 @@ def measure(self): if chk_sum & 0xff != buf[4]: # check sum failed to validate raise RuntimeError("Checksum did not validate. Try again.") - #print("checksum did not match. Temp: {} Humidity: {} Checksum:{}" - #.format(self._temperature,self._humidity,bites[4])) - # checksum matches - #print("Temp: {} C Humidity: {}% ".format(self._temperature, self._humidity)) else: raise RuntimeError("A full buffer was not returned. Try again.") - #print("did not get a full return. number returned was: {}".format(len(r))) @property def temperature(self):