Skip to content

Commit a67ac23

Browse files
authored
Merge pull request #46 from adafruit/pulseio-fix
Deinitialize pulsein object on exit, gave option to choose whether or not to use pulseio
2 parents 35cc6b4 + a515f92 commit a67ac23

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

adafruit_dht.py

+19-8
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
import array
3232
import time
33+
from os import uname
3334
from digitalio import DigitalInOut, Pull, Direction
3435

3536
_USE_PULSEIO = False
@@ -51,23 +52,33 @@ class DHTBase:
5152

5253
__hiLevel = 51
5354

54-
def __init__(self, dht11, pin, trig_wait):
55+
def __init__(self, dht11, pin, trig_wait, use_pulseio):
5556
"""
5657
:param boolean dht11: True if device is DHT11, otherwise DHT22.
5758
:param ~board.Pin pin: digital pin used for communication
5859
:param int trig_wait: length of time to hold trigger in LOW state (microseconds)
60+
:param boolean use_pulseio: False to force bitbang when pulseio available (only with Blinka)
5961
"""
6062
self._dht11 = dht11
6163
self._pin = pin
6264
self._trig_wait = trig_wait
6365
self._last_called = 0
6466
self._humidity = None
6567
self._temperature = None
68+
self._use_pulseio = use_pulseio
69+
if "Linux" not in uname() and not self._use_pulseio:
70+
raise Exception("Bitbanging is not supported when using CircuitPython.")
6671
# We don't use a context because linux-based systems are sluggish
6772
# and we're better off having a running process
68-
if _USE_PULSEIO:
73+
if self._use_pulseio:
6974
self.pulse_in = PulseIn(self._pin, 81, True)
7075

76+
def exit(self):
77+
""" Cleans up the PulseIn process. Must be called explicitly """
78+
if self._use_pulseio:
79+
print("De-initializing self.pulse_in")
80+
self.pulse_in.deinit()
81+
7182
def _pulses_to_binary(self, pulses, start, stop):
7283
"""Takes pulses, a list of transition times, and converts
7384
them to a 1's or 0's. The pulses array contains the transition times.
@@ -108,7 +119,7 @@ def _get_pulses_pulseio(self):
108119
pulses will have 81 elements for the DHT11/22 type devices.
109120
"""
110121
pulses = array.array("H")
111-
if _USE_PULSEIO:
122+
if self._use_pulseio:
112123
# The DHT type device use a specialize 1-wire protocol
113124
# The microprocessor first sends a LOW signal for a
114125
# specific length of time. Then the device sends back a
@@ -183,7 +194,7 @@ def measure(self):
183194
new_temperature = 0
184195
new_humidity = 0
185196

186-
if _USE_PULSEIO:
197+
if self._use_pulseio:
187198
pulses = self._get_pulses_pulseio()
188199
else:
189200
pulses = self._get_pulses_bitbang()
@@ -259,8 +270,8 @@ class DHT11(DHTBase):
259270
:param ~board.Pin pin: digital pin used for communication
260271
"""
261272

262-
def __init__(self, pin):
263-
super().__init__(True, pin, 18000)
273+
def __init__(self, pin, use_pulseio=_USE_PULSEIO):
274+
super().__init__(True, pin, 18000, use_pulseio)
264275

265276

266277
class DHT22(DHTBase):
@@ -269,5 +280,5 @@ class DHT22(DHTBase):
269280
:param ~board.Pin pin: digital pin used for communication
270281
"""
271282

272-
def __init__(self, pin):
273-
super().__init__(False, pin, 1000)
283+
def __init__(self, pin, use_pulseio=_USE_PULSEIO):
284+
super().__init__(False, pin, 1000, use_pulseio)

examples/dht_simpletest.py

+10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
# Initial the dht device, with data pin connected to:
66
dhtDevice = adafruit_dht.DHT22(board.D18)
77

8+
# you can pass DHT22 use_pulseio=False if you wouldn't like to use pulseio.
9+
# This may be necessary on a Linux single board computer like the Raspberry Pi,
10+
# but it will not work in CircuitPython.
11+
# dhtDevice = adafruit_dht.DHT22(board.D18, use_pulseio=False)
12+
813
while True:
914
try:
1015
# Print the values to the serial port
@@ -20,5 +25,10 @@
2025
except RuntimeError as error:
2126
# Errors happen fairly often, DHT's are hard to read, just keep going
2227
print(error.args[0])
28+
time.sleep(2.0)
29+
continue
30+
except Exception as error:
31+
dhtDevice.exit()
32+
raise error
2333

2434
time.sleep(2.0)

0 commit comments

Comments
 (0)