diff --git a/adafruit_dht.py b/adafruit_dht.py index fb2b188..973a9c2 100644 --- a/adafruit_dht.py +++ b/adafruit_dht.py @@ -30,6 +30,7 @@ import array import time +from os import uname from digitalio import DigitalInOut, Pull, Direction _USE_PULSEIO = False @@ -51,11 +52,12 @@ class DHTBase: __hiLevel = 51 - def __init__(self, dht11, pin, trig_wait): + def __init__(self, dht11, pin, trig_wait, use_pulseio): """ :param boolean dht11: True if device is DHT11, otherwise DHT22. :param ~board.Pin pin: digital pin used for communication :param int trig_wait: length of time to hold trigger in LOW state (microseconds) + :param boolean use_pulseio: False to force bitbang when pulseio available (only with Blinka) """ self._dht11 = dht11 self._pin = pin @@ -63,11 +65,20 @@ def __init__(self, dht11, pin, trig_wait): self._last_called = 0 self._humidity = None self._temperature = None + self._use_pulseio = use_pulseio + if "Linux" not in uname() and not self._use_pulseio: + raise Exception("Bitbanging is not supported when using CircuitPython.") # We don't use a context because linux-based systems are sluggish # and we're better off having a running process - if _USE_PULSEIO: + if self._use_pulseio: self.pulse_in = PulseIn(self._pin, 81, True) + def exit(self): + """ Cleans up the PulseIn process. Must be called explicitly """ + if self._use_pulseio: + print("De-initializing self.pulse_in") + self.pulse_in.deinit() + def _pulses_to_binary(self, pulses, start, stop): """Takes pulses, a list of transition times, and converts them to a 1's or 0's. The pulses array contains the transition times. @@ -108,7 +119,7 @@ def _get_pulses_pulseio(self): pulses will have 81 elements for the DHT11/22 type devices. """ pulses = array.array("H") - if _USE_PULSEIO: + if self._use_pulseio: # The DHT type device use a specialize 1-wire protocol # The microprocessor first sends a LOW signal for a # specific length of time. Then the device sends back a @@ -183,7 +194,7 @@ def measure(self): new_temperature = 0 new_humidity = 0 - if _USE_PULSEIO: + if self._use_pulseio: pulses = self._get_pulses_pulseio() else: pulses = self._get_pulses_bitbang() @@ -259,8 +270,8 @@ class DHT11(DHTBase): :param ~board.Pin pin: digital pin used for communication """ - def __init__(self, pin): - super().__init__(True, pin, 18000) + def __init__(self, pin, use_pulseio=_USE_PULSEIO): + super().__init__(True, pin, 18000, use_pulseio) class DHT22(DHTBase): @@ -269,5 +280,5 @@ class DHT22(DHTBase): :param ~board.Pin pin: digital pin used for communication """ - def __init__(self, pin): - super().__init__(False, pin, 1000) + def __init__(self, pin, use_pulseio=_USE_PULSEIO): + super().__init__(False, pin, 1000, use_pulseio) diff --git a/examples/dht_simpletest.py b/examples/dht_simpletest.py index 9a054d1..2b4cdec 100644 --- a/examples/dht_simpletest.py +++ b/examples/dht_simpletest.py @@ -5,6 +5,11 @@ # Initial the dht device, with data pin connected to: dhtDevice = adafruit_dht.DHT22(board.D18) +# you can pass DHT22 use_pulseio=False if you wouldn't like to use pulseio. +# This may be necessary on a Linux single board computer like the Raspberry Pi, +# but it will not work in CircuitPython. +# dhtDevice = adafruit_dht.DHT22(board.D18, use_pulseio=False) + while True: try: # Print the values to the serial port @@ -20,5 +25,10 @@ except RuntimeError as error: # Errors happen fairly often, DHT's are hard to read, just keep going print(error.args[0]) + time.sleep(2.0) + continue + except Exception as error: + dhtDevice.exit() + raise error time.sleep(2.0)