Retry when DHT temp sensor read fails
This commit was merged in pull request #455.
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
import sys
|
||||
import time
|
||||
import adafruit_dht
|
||||
|
||||
|
||||
# Parse command line parameters.
|
||||
sensor_args = {
|
||||
sensor_args = {
|
||||
'11': adafruit_dht.DHT11,
|
||||
'22': adafruit_dht.DHT22,
|
||||
'2302': adafruit_dht.DHT22
|
||||
@@ -16,12 +17,29 @@ else:
|
||||
sys.exit(1)
|
||||
|
||||
dht_dev = sensor(pin)
|
||||
humidity = dht_dev.humidity
|
||||
temperature = dht_dev.temperature
|
||||
|
||||
if humidity is not None and temperature is not None:
|
||||
print('{0:0.1f} | {1:0.1f}'.format(temperature, humidity))
|
||||
else:
|
||||
print('-1 | -1')
|
||||
# DHT sensor read fails quite often, causing enclosure plugin to report value of 0.
|
||||
# If this happens, retry as suggested in the adafruit_dht docs.
|
||||
max_retries = 3
|
||||
retry_count = 0
|
||||
while retry_count <= max_retries:
|
||||
try:
|
||||
humidity = dht_dev.humidity
|
||||
temperature = dht_dev.temperature
|
||||
|
||||
sys.exit(1)
|
||||
if humidity is not None and temperature is not None:
|
||||
print('{0:0.1f} | {1:0.1f}'.format(temperature, humidity))
|
||||
sys.exit(1)
|
||||
except RuntimeError as e:
|
||||
time.sleep(2)
|
||||
retry_count += 1
|
||||
continue
|
||||
except Exception as e:
|
||||
dht_dev.exit()
|
||||
raise e
|
||||
|
||||
time.sleep(1)
|
||||
retry_count += 1
|
||||
|
||||
print('-1 | -1')
|
||||
sys.exit(1)
|
||||
Reference in New Issue
Block a user