From 354dbf94f9467a56e9038df8fb0ebb62d6402c02 Mon Sep 17 00:00:00 2001 From: Jerry Needell Date: Mon, 16 Apr 2018 08:01:32 -0400 Subject: [PATCH 1/4] modifued get_pulses to reduce errors - wait for timeout then use last 81 pulses - adjust lockout for dht11 --- adafruit_dht.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/adafruit_dht.py b/adafruit_dht.py index f455fe6..d15e6d9 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,13 +114,15 @@ 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 len(pulse_in) >= 82: + # break + if time.monotonic()-tmono > 0.25: # time out after 1/4 seconds break + #print(len(pulse_in)) pulse_in.pause() while pulse_in: pulses.append(pulse_in.popleft()) @@ -137,11 +138,15 @@ 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) + #print(pulses) + #print(len(pulses)) if len(pulses) >= 80: buf = array.array('B') @@ -169,6 +174,7 @@ def measure(self): # checksum is the last byte if chk_sum & 0xff != buf[4]: # check sum failed to validate + #print(pulses) raise RuntimeError("Checksum did not validate. Try again.") #print("checksum did not match. Temp: {} Humidity: {} Checksum:{}" #.format(self._temperature,self._humidity,bites[4])) From 053e0f46218f3cc51103f43db429a31baca3a521 Mon Sep 17 00:00:00 2001 From: Jerry Needell Date: Mon, 16 Apr 2018 09:28:50 -0400 Subject: [PATCH 2/4] pylint fix --- adafruit_dht.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_dht.py b/adafruit_dht.py index d15e6d9..4c9b5b4 100644 --- a/adafruit_dht.py +++ b/adafruit_dht.py @@ -139,7 +139,7 @@ def measure(self): data returned from the device (try again) """ delay_between_readings = 0.5 - if(self._dht11): + if self._dht11 : delay_between_readings = 1.0 if time.monotonic()-self._last_called > delay_between_readings: self._last_called = time.monotonic() From 0cf82dc9e51c03c6930cc13556cf6a0f239e5322 Mon Sep 17 00:00:00 2001 From: Jerry Needell Date: Mon, 16 Apr 2018 09:34:01 -0400 Subject: [PATCH 3/4] pylint fix --- adafruit_dht.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_dht.py b/adafruit_dht.py index 4c9b5b4..57c9d88 100644 --- a/adafruit_dht.py +++ b/adafruit_dht.py @@ -139,7 +139,7 @@ def measure(self): data returned from the device (try again) """ delay_between_readings = 0.5 - if self._dht11 : + if self._dht11: delay_between_readings = 1.0 if time.monotonic()-self._last_called > delay_between_readings: self._last_called = time.monotonic() From 45a7fb5dba817581cce777f482052c137e617818 Mon Sep 17 00:00:00 2001 From: Jerry Needell Date: Mon, 16 Apr 2018 13:39:39 -0400 Subject: [PATCH 4/4] remove commented code --- adafruit_dht.py | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/adafruit_dht.py b/adafruit_dht.py index 57c9d88..068405f 100644 --- a/adafruit_dht.py +++ b/adafruit_dht.py @@ -117,12 +117,9 @@ def _get_pulses(self): # time out after 1/4 second tmono = time.monotonic() while True: - #if len(pulse_in) >= 82: - # break if time.monotonic()-tmono > 0.25: # time out after 1/4 seconds break - #print(len(pulse_in)) pulse_in.pause() while pulse_in: pulses.append(pulse_in.popleft()) @@ -145,14 +142,11 @@ def measure(self): self._last_called = time.monotonic() pulses = self._get_pulses() - #print(pulses) - #print(len(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: @@ -160,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: @@ -174,17 +168,11 @@ def measure(self): # checksum is the last byte if chk_sum & 0xff != buf[4]: # check sum failed to validate - #print(pulses) 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):