From 66a25e771939344781d9532e169f09e785ebaa9d Mon Sep 17 00:00:00 2001 From: Robert Borzutzki Date: Mon, 15 Feb 2021 11:44:38 +0000 Subject: [PATCH 1/2] add exception handling to convert humidity --- mqtt_publish_temp.py | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/mqtt_publish_temp.py b/mqtt_publish_temp.py index 787fb3a..29845d2 100644 --- a/mqtt_publish_temp.py +++ b/mqtt_publish_temp.py @@ -45,22 +45,29 @@ client.connect(mqtt_broker) # MQTT server address client.loop_start() while True: - # Get Temperature from DHT _, temperature = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN) - temperature = round(float(temperature), 2) + # Get Humidity from DHT humidity, _ = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN) - humidity = round(float(humidity), 2) + + #Output data to screen + print(dt_string) + print(temperature) + print(humidity) + + + temperature = round(float(temperature), 2) + + try: + humidity = round(float(humidity), 2) + break + except ValueError: + print("Not able to convert humidity to float: " + humidity) # Publish our data client.publish(temperature_topic, temperature) client.publish(humidity_topic, humidity) client.publish(time_topic, dt_string) - #Output data to screen - print(dt_string) - print(temperature) - print(humidity) - time.sleep(30) \ No newline at end of file From ec80f068405bf3dc3066a643f5b6956117f084c9 Mon Sep 17 00:00:00 2001 From: Robert Borzutzki Date: Mon, 15 Feb 2021 11:50:24 +0000 Subject: [PATCH 2/2] Update mqtt_publish_temp.py --- mqtt_publish_temp.py | 1 - 1 file changed, 1 deletion(-) diff --git a/mqtt_publish_temp.py b/mqtt_publish_temp.py index 29845d2..4727a1c 100644 --- a/mqtt_publish_temp.py +++ b/mqtt_publish_temp.py @@ -61,7 +61,6 @@ while True: try: humidity = round(float(humidity), 2) - break except ValueError: print("Not able to convert humidity to float: " + humidity)