Skip to content

Commit 5c1da73

Browse files
committed
Deinitialize pulsein object on exit, gave option to choose whether or not to use pulseio
1 parent 35cc6b4 commit 5c1da73

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

adafruit_dht.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class DHTBase:
5151

5252
__hiLevel = 51
5353

54-
def __init__(self, dht11, pin, trig_wait):
54+
def __init__(self, dht11, pin, trig_wait, use_pulseio):
5555
"""
5656
:param boolean dht11: True if device is DHT11, otherwise DHT22.
5757
:param ~board.Pin pin: digital pin used for communication
@@ -63,11 +63,17 @@ def __init__(self, dht11, pin, trig_wait):
6363
self._last_called = 0
6464
self._humidity = None
6565
self._temperature = None
66+
self._use_pulseio = use_pulseio
6667
# We don't use a context because linux-based systems are sluggish
6768
# and we're better off having a running process
68-
if _USE_PULSEIO:
69+
if self._use_pulseio:
6970
self.pulse_in = PulseIn(self._pin, 81, True)
7071

72+
def exit(self):
73+
if self._use_pulseio:
74+
print("De-initializing self.pulse_in")
75+
self.pulse_in.deinit()
76+
7177
def _pulses_to_binary(self, pulses, start, stop):
7278
"""Takes pulses, a list of transition times, and converts
7379
them to a 1's or 0's. The pulses array contains the transition times.
@@ -108,7 +114,7 @@ def _get_pulses_pulseio(self):
108114
pulses will have 81 elements for the DHT11/22 type devices.
109115
"""
110116
pulses = array.array("H")
111-
if _USE_PULSEIO:
117+
if self._use_pulseio:
112118
# The DHT type device use a specialize 1-wire protocol
113119
# The microprocessor first sends a LOW signal for a
114120
# specific length of time. Then the device sends back a
@@ -183,7 +189,7 @@ def measure(self):
183189
new_temperature = 0
184190
new_humidity = 0
185191

186-
if _USE_PULSEIO:
192+
if self._use_pulseio:
187193
pulses = self._get_pulses_pulseio()
188194
else:
189195
pulses = self._get_pulses_bitbang()
@@ -259,8 +265,8 @@ class DHT11(DHTBase):
259265
:param ~board.Pin pin: digital pin used for communication
260266
"""
261267

262-
def __init__(self, pin):
263-
super().__init__(True, pin, 18000)
268+
def __init__(self, pin, use_pulseio=_USE_PULSEIO):
269+
super().__init__(True, pin, 18000, use_pulseio)
264270

265271

266272
class DHT22(DHTBase):
@@ -269,5 +275,5 @@ class DHT22(DHTBase):
269275
:param ~board.Pin pin: digital pin used for communication
270276
"""
271277

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

examples/dht_simpletest.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import adafruit_dht
44

55
# Initial the dht device, with data pin connected to:
6-
dhtDevice = adafruit_dht.DHT22(board.D18)
6+
dhtDevice = adafruit_dht.DHT22(board.D18, use_pulseio=False)
77

88
while True:
99
try:
@@ -20,5 +20,10 @@
2020
except RuntimeError as error:
2121
# Errors happen fairly often, DHT's are hard to read, just keep going
2222
print(error.args[0])
23+
time.sleep(2.0)
24+
continue
25+
except Exception as error:
26+
dhtDevice.exit()
27+
raise error
2328

2429
time.sleep(2.0)

0 commit comments

Comments
 (0)