Skip to content

modified get_pulses to reduce errors #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 20, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 8 additions & 14 deletions adafruit_dht.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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()
Expand All @@ -137,25 +135,26 @@ 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:
self._humidity = buf[0]
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:
Expand All @@ -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):
Expand Down