Fix error where sensor dies not read data & exits

This commit is contained in:
2022-12-06 08:30:19 -05:00
parent 8b630b8ddc
commit b1de1a6d67

View File

@@ -39,6 +39,9 @@ def connect_mqtt():
def getValues(sensor): def getValues(sensor):
#While loop to try sensor again if it fails, seems to happen occasionally
result = None
while result is None:
try: try:
temp = sensor.temperature temp = sensor.temperature
humidity = sensor.humidity humidity = sensor.humidity
@@ -48,12 +51,10 @@ def getValues(sensor):
#Round the decimals #Round the decimals
temp = round(temp, 2) temp = round(temp, 2)
#print("Temperature: {}*F Humidity: {}% ".format(temp, humidity)) #print("Temperature: {}*F Humidity: {}% ".format(temp, humidity))
result = 1
except RuntimeError as error: except RuntimeError as error:
print(error.args[0]) print(error.args[0])
time.sleep(2.0) time.sleep(2.0)
except Exception as error:
sensor.exit()
raise error
return temp, humidity return temp, humidity
@@ -85,7 +86,7 @@ def publish(client, temp, humidity):
while True: while True:
client = connect_mqtt() client = connect_mqtt()
client.loop_start() client.loop_start()
#temp, humidity = getValues(sensor) temp, humidity = getValues(sensor)
getValues(sensor) getValues(sensor)
publish(client, temp, humidity) publish(client, temp, humidity)
time.sleep (60.0) time.sleep (60.0)