Skip to content

Commit 354dbf9

Browse files
committed
modifued get_pulses to reduce errors - wait for timeout then use last 81 pulses - adjust lockout for dht11
1 parent 7f2dad7 commit 354dbf9

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

adafruit_dht.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ def _get_pulses(self):
100100
pulses will have 81 elements for the DHT11/22 type devices.
101101
"""
102102
pulses = array.array('H')
103-
tmono = time.monotonic()
104103

105104
# create the PulseIn object using context manager
106105
with pulseio.PulseIn(self._pin, 81, True) as pulse_in:
@@ -115,13 +114,15 @@ def _get_pulses(self):
115114
pulse_in.resume(self._trig_wait)
116115

117116
# loop until we get the return pulse we need or
118-
# time out after 1/2 seconds
117+
# time out after 1/4 second
118+
tmono = time.monotonic()
119119
while True:
120-
if len(pulse_in) >= 80:
121-
break
122-
if time.monotonic()-tmono > 0.5: # time out after 1/2 seconds
120+
#if len(pulse_in) >= 82:
121+
# break
122+
if time.monotonic()-tmono > 0.25: # time out after 1/4 seconds
123123
break
124124

125+
#print(len(pulse_in))
125126
pulse_in.pause()
126127
while pulse_in:
127128
pulses.append(pulse_in.popleft())
@@ -137,11 +138,15 @@ def measure(self):
137138
Raises RuntimeError exception for checksum failure and for insuffcient
138139
data returned from the device (try again)
139140
"""
140-
if time.monotonic()-self._last_called > 0.5:
141+
delay_between_readings = 0.5
142+
if(self._dht11):
143+
delay_between_readings = 1.0
144+
if time.monotonic()-self._last_called > delay_between_readings:
141145
self._last_called = time.monotonic()
142146

143147
pulses = self._get_pulses()
144-
##print(pulses)
148+
#print(pulses)
149+
#print(len(pulses))
145150

146151
if len(pulses) >= 80:
147152
buf = array.array('B')
@@ -169,6 +174,7 @@ def measure(self):
169174
# checksum is the last byte
170175
if chk_sum & 0xff != buf[4]:
171176
# check sum failed to validate
177+
#print(pulses)
172178
raise RuntimeError("Checksum did not validate. Try again.")
173179
#print("checksum did not match. Temp: {} Humidity: {} Checksum:{}"
174180
#.format(self._temperature,self._humidity,bites[4]))

0 commit comments

Comments
 (0)