@@ -180,51 +180,58 @@ def measure(self):
180
180
):
181
181
self ._last_called = time .monotonic ()
182
182
183
+ new_temperature = 0
184
+ new_humidity = 0
185
+
183
186
if _USE_PULSEIO :
184
187
pulses = self ._get_pulses_pulseio ()
185
188
else :
186
189
pulses = self ._get_pulses_bitbang ()
187
190
# print(len(pulses), "pulses:", [x for x in pulses])
188
191
189
- if len (pulses ) >= 80 :
190
- buf = array .array ("B" )
191
- for byte_start in range (0 , 80 , 16 ):
192
- buf .append (
193
- self ._pulses_to_binary (pulses , byte_start , byte_start + 16 )
194
- )
195
-
196
- if self ._dht11 :
197
- # humidity is 1 byte
198
- self ._humidity = buf [0 ]
199
- # temperature is 1 byte
200
- self ._temperature = buf [2 ]
201
- else :
202
- # humidity is 2 bytes
203
- self ._humidity = ((buf [0 ] << 8 ) | buf [1 ]) / 10
204
- # temperature is 2 bytes
205
- # MSB is sign, bits 0-14 are magnitude)
206
- raw_temperature = (((buf [2 ] & 0x7F ) << 8 ) | buf [3 ]) / 10
207
- # set sign
208
- if buf [2 ] & 0x80 :
209
- raw_temperature = - raw_temperature
210
- self ._temperature = raw_temperature
211
- # calc checksum
212
- chk_sum = 0
213
- for b in buf [0 :4 ]:
214
- chk_sum += b
215
-
216
- # checksum is the last byte
217
- if chk_sum & 0xFF != buf [4 ]:
218
- # check sum failed to validate
219
- raise RuntimeError ("Checksum did not validate. Try again." )
220
-
221
- elif len (pulses ) >= 10 :
222
- # We got *some* data just not 81 bits
223
- raise RuntimeError ("A full buffer was not returned. Try again." )
224
- else :
192
+ if len (pulses ) < 10 :
225
193
# Probably a connection issue!
226
194
raise RuntimeError ("DHT sensor not found, check wiring" )
227
195
196
+ if len (pulses ) < 80 :
197
+ # We got *some* data just not 81 bits
198
+ raise RuntimeError ("A full buffer was not returned. Try again." )
199
+
200
+ buf = array .array ("B" )
201
+ for byte_start in range (0 , 80 , 16 ):
202
+ buf .append (self ._pulses_to_binary (pulses , byte_start , byte_start + 16 ))
203
+
204
+ if self ._dht11 :
205
+ # humidity is 1 byte
206
+ new_humidity = buf [0 ]
207
+ # temperature is 1 byte
208
+ new_temperature = buf [2 ]
209
+ else :
210
+ # humidity is 2 bytes
211
+ new_humidity = ((buf [0 ] << 8 ) | buf [1 ]) / 10
212
+ # temperature is 2 bytes
213
+ # MSB is sign, bits 0-14 are magnitude)
214
+ new_temperature = (((buf [2 ] & 0x7F ) << 8 ) | buf [3 ]) / 10
215
+ # set sign
216
+ if buf [2 ] & 0x80 :
217
+ new_temperature = - new_temperature
218
+ # calc checksum
219
+ chk_sum = 0
220
+ for b in buf [0 :4 ]:
221
+ chk_sum += b
222
+
223
+ # checksum is the last byte
224
+ if chk_sum & 0xFF != buf [4 ]:
225
+ # check sum failed to validate
226
+ raise RuntimeError ("Checksum did not validate. Try again." )
227
+
228
+ if new_humidity < 0 or new_humidity > 100 :
229
+ # We received unplausible data
230
+ raise RuntimeError ("Received unplausible data. Try again." )
231
+
232
+ self ._temperature = new_temperature
233
+ self ._humidity = new_humidity
234
+
228
235
@property
229
236
def temperature (self ):
230
237
""" temperature current reading. It makes sure a reading is available
0 commit comments