Skip to content

Commit ddda939

Browse files
authored
Merge pull request #8 from jerryneedell/jerryn_dht
modified get_pulses to reduce errors
2 parents 7f2dad7 + 45a7fb5 commit ddda939

File tree

1 file changed

+8
-14
lines changed

1 file changed

+8
-14
lines changed

adafruit_dht.py

Lines changed: 8 additions & 14 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,11 +114,10 @@ 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 time.monotonic()-tmono > 0.25: # time out after 1/4 seconds
123121
break
124122

125123
pulse_in.pause()
@@ -137,25 +135,26 @@ def measure(self):
137135
Raises RuntimeError exception for checksum failure and for insuffcient
138136
data returned from the device (try again)
139137
"""
140-
if time.monotonic()-self._last_called > 0.5:
138+
delay_between_readings = 0.5
139+
if self._dht11:
140+
delay_between_readings = 1.0
141+
if time.monotonic()-self._last_called > delay_between_readings:
141142
self._last_called = time.monotonic()
142143

143144
pulses = self._get_pulses()
144-
##print(pulses)
145145

146146
if len(pulses) >= 80:
147147
buf = array.array('B')
148148
for byte_start in range(0, 80, 16):
149149
buf.append(self._pulses_to_binary(pulses, byte_start, byte_start+16))
150-
#print(buf)
151150

152151
# humidity is 2 bytes
153152
if self._dht11:
154153
self._humidity = buf[0]
155154
else:
156155
self._humidity = ((buf[0]<<8) | buf[1]) / 10
157156

158-
# tempature is 2 bytes
157+
# temperature is 2 bytes
159158
if self._dht11:
160159
self._temperature = buf[2]
161160
else:
@@ -170,15 +169,10 @@ def measure(self):
170169
if chk_sum & 0xff != buf[4]:
171170
# check sum failed to validate
172171
raise RuntimeError("Checksum did not validate. Try again.")
173-
#print("checksum did not match. Temp: {} Humidity: {} Checksum:{}"
174-
#.format(self._temperature,self._humidity,bites[4]))
175172

176-
# checksum matches
177-
#print("Temp: {} C Humidity: {}% ".format(self._temperature, self._humidity))
178173

179174
else:
180175
raise RuntimeError("A full buffer was not returned. Try again.")
181-
#print("did not get a full return. number returned was: {}".format(len(r)))
182176

183177
@property
184178
def temperature(self):

0 commit comments

Comments
 (0)