Skip to content

Commit 5c287a2

Browse files
committed
Support for DHT21/AM2301
1 parent 244a7d1 commit 5c287a2

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

adafruit_dht.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class DHTBase:
5555

5656
__hiLevel = 51
5757

58-
def __init__(self, dht11: bool, pin: Pin, trig_wait: int, use_pulseio: bool):
58+
def __init__(self, dht11: bool, pin: Pin, trig_wait: int, max_pulses: int, use_pulseio: bool):
5959
"""
6060
:param boolean dht11: True if device is DHT11, otherwise DHT22.
6161
:param ~board.Pin pin: digital pin used for communication
@@ -65,6 +65,7 @@ def __init__(self, dht11: bool, pin: Pin, trig_wait: int, use_pulseio: bool):
6565
self._dht11 = dht11
6666
self._pin = pin
6767
self._trig_wait = trig_wait
68+
self._max_pulses = max_pulses
6869
self._last_called = 0
6970
self._humidity = None
7071
self._temperature = None
@@ -74,7 +75,7 @@ def __init__(self, dht11: bool, pin: Pin, trig_wait: int, use_pulseio: bool):
7475
# We don't use a context because linux-based systems are sluggish
7576
# and we're better off having a running process
7677
if self._use_pulseio:
77-
self.pulse_in = PulseIn(self._pin, maxlen=81, idle_state=True)
78+
self.pulse_in = PulseIn(self._pin, maxlen=self._max_pulses, idle_state=True)
7879
self.pulse_in.pause()
7980

8081
def exit(self) -> None:
@@ -179,7 +180,7 @@ def _get_pulses_bitbang(self) -> array.array:
179180
transitions.append(time.monotonic()) # save the timestamp
180181
# convert transtions to microsecond delta pulses:
181182
# use last 81 pulses
182-
transition_start = max(1, len(transitions) - 81)
183+
transition_start = max(1, len(transitions) - self._max_pulses)
183184
for i in range(transition_start, len(transitions)):
184185
pulses_micro_sec = int(1000000 * (transitions[i] - transitions[i - 1]))
185186
pulses.append(min(pulses_micro_sec, 65535))
@@ -283,7 +284,7 @@ class DHT11(DHTBase):
283284
"""
284285

285286
def __init__(self, pin: Pin, use_pulseio: bool = _USE_PULSEIO):
286-
super().__init__(True, pin, 18000, use_pulseio)
287+
super().__init__(True, pin, 18000, 81, use_pulseio)
287288

288289

289290
class DHT22(DHTBase):
@@ -293,4 +294,18 @@ class DHT22(DHTBase):
293294
"""
294295

295296
def __init__(self, pin: Pin, use_pulseio: bool = _USE_PULSEIO):
296-
super().__init__(False, pin, 1000, use_pulseio)
297+
super().__init__(False, pin, 1000, 81, use_pulseio)
298+
299+
300+
class DHT21(DHTBase):
301+
"""Support for DHT21/AM2301 device.
302+
303+
:param ~board.Pin pin: digital pin used for communication
304+
"""
305+
306+
# DHT21/AM2301 is sending three more dummy bytes after the "official" protocol.
307+
# Pulseio will take only the last pulses up to maxPulses.
308+
# If that would be 81, the dummy pulses will be read and the real data would be truncated.
309+
# Hence setting maxPulses to 129, taking both real data and dummy bytes into buffer.
310+
def __init__(self, pin: Pin, use_pulseio: bool = _USE_PULSEIO):
311+
super().__init__(False, pin, 1000, 129, use_pulseio)

0 commit comments

Comments
 (0)