@@ -55,7 +55,7 @@ class DHTBase:
55
55
56
56
__hiLevel = 51
57
57
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 ):
59
59
"""
60
60
:param boolean dht11: True if device is DHT11, otherwise DHT22.
61
61
: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):
65
65
self ._dht11 = dht11
66
66
self ._pin = pin
67
67
self ._trig_wait = trig_wait
68
+ self ._max_pulses = max_pulses
68
69
self ._last_called = 0
69
70
self ._humidity = None
70
71
self ._temperature = None
@@ -74,7 +75,7 @@ def __init__(self, dht11: bool, pin: Pin, trig_wait: int, use_pulseio: bool):
74
75
# We don't use a context because linux-based systems are sluggish
75
76
# and we're better off having a running process
76
77
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 )
78
79
self .pulse_in .pause ()
79
80
80
81
def exit (self ) -> None :
@@ -179,7 +180,7 @@ def _get_pulses_bitbang(self) -> array.array:
179
180
transitions .append (time .monotonic ()) # save the timestamp
180
181
# convert transtions to microsecond delta pulses:
181
182
# use last 81 pulses
182
- transition_start = max (1 , len (transitions ) - 81 )
183
+ transition_start = max (1 , len (transitions ) - self . _max_pulses )
183
184
for i in range (transition_start , len (transitions )):
184
185
pulses_micro_sec = int (1000000 * (transitions [i ] - transitions [i - 1 ]))
185
186
pulses .append (min (pulses_micro_sec , 65535 ))
@@ -283,7 +284,7 @@ class DHT11(DHTBase):
283
284
"""
284
285
285
286
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 )
287
288
288
289
289
290
class DHT22 (DHTBase ):
@@ -293,4 +294,18 @@ class DHT22(DHTBase):
293
294
"""
294
295
295
296
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