From 5f182f8732dc72f9f5f6046985c2e6ccd8b17b8a Mon Sep 17 00:00:00 2001 From: Shaun Setlock Date: Tue, 6 Dec 2022 21:17:34 -0500 Subject: [PATCH] Updated main.py to be more failsafe. Now using umqtt.robust too. --- main.py | 150 +++++++++++++++++++++++++++++++++----------------------- 1 file changed, 89 insertions(+), 61 deletions(-) diff --git a/main.py b/main.py index c58408d..cd47a7f 100644 --- a/main.py +++ b/main.py @@ -4,7 +4,7 @@ import time import json import machine from machine import Pin -from umqtt.simple import MQTTClient +from umqtt.robust import MQTTClient import dht import urequests import wifi_config @@ -115,7 +115,10 @@ def main(): wlan = network.WLAN(network.STA_IF) # Create MQTT Client - mqtt_client = MQTTClient(mqtt_config.MQTT_CLIENT_ID, mqtt_config.MQTT_HOST_NAME) + mqtt_client = MQTTClient( + client_id = mqtt_config.MQTT_CLIENT_ID, + server = mqtt_config.MQTT_HOST_NAME, + ) # Create DHT22 sensor = dht.DHT22(Pin(DHT_22_GPIO_PIN)) @@ -132,73 +135,98 @@ def main(): if DEBUG: print(f'\nStarting loop #{count}... \nWiFI Status is {wlan.status()}.') + # Create Local Flags for Control + wifi_ready = False + mqtt_ready = False + dht22_ready = False + + # DHT22 and CPU Reading. + try: + # CPU Reading. + cpu_temp = read_cpu_temp() + + # DHT22 Reading. + dht22_reading = read_dht_22(sensor) + #debug_str = "None" + if dht22_reading is not None: + temp,hum = dht22_reading + temp = temp * 9/5. + 32.0 + dht22_ready = True + except: + continue + # WiFi Connection. - if wlan.status() != 3: - ifconfig = wlan_up(wlan) - if ifconfig is None: - if DEBUG: - print("Trouble to connecting WiFi: {}".format(e)) - led_error_code(led, 3) - time.sleep(10) - continue + try: + if wlan.status() != 3: + ifconfig = wlan_up(wlan) + if ifconfig is None: + if DEBUG: + print("Trouble to connecting WiFi: {}".format(e)) + led_error_code(led, 3) + time.sleep(10) + continue + else: + wifi_ready = True + if DEBUG: + print("Connected to WiFi: {}".format(ifconfig)) else: + wifi_ready = True + ifconfig = wlan.ifconfig() if DEBUG: - print("Connected to WiFi: {}".format(ifconfig)) - else: - ifconfig = wlan.ifconfig() - if DEBUG: - print(f"Skipping WiFi Activation since WiFi Status is {wlan.status()} \n{ifconfig}.") + print(f"Skipping WiFi Activation since WiFi Status is {wlan.status()} \n{ifconfig}.") + except: + continue # MQTT Conneciton. try: - mqtt_client.connect() - if DEBUG: - print(f'MQTT Connected.') - - except Exception as e: - if DEBUG: - print("Trouble to connecting to MQTT: {}".format(e)) - led_error_code(led, 2) - time.sleep(5) - continue # Start back at the top of the While Loop - - # DHT22 Reading. - dht22_reading = read_dht_22(sensor) - #debug_str = "None" - if dht22_reading is not None: - temp,hum = dht22_reading - temp = temp * 9/5. + 32.0 + try: + mqtt_client.connect(clean_session = False) + mqtt_ready = True + if DEBUG: + print(f'MQTT Connected.') - # CPU Reading. - cpu_temp = read_cpu_temp() - - # Timestamp - time_now = time.localtime() - timestamp = "{}/{}/{} {}:{}:{}".format(time_now[1],time_now[2],time_now[0],time_now[3],time_now[4],time_now[5]) - - # Build JSON Payloads - dht_data = { - 'Location':mqtt_config.MQTT_ZONE_ID, - 'Temperature':temp, - 'Relative Humidity':hum, - } - dht_payload = json.dumps(dht_data) - - hw_data = { - 'Timestamp':timestamp, - 'CPU Temperature':cpu_temp, - 'Device':mqtt_config.MQTT_HW_ID, - 'WiFi Information':ifconfig, - } - hw_payload = json.dumps(hw_data) - - # Publish - mqtt_client.publish("home/{}".format(mqtt_config.MQTT_ZONE_ID),dht_payload, retain=True) - mqtt_client.publish("hw/{}".format(mqtt_config.MQTT_HW_ID),hw_payload, retain=True) + except Exception as e: + if DEBUG: + print("Trouble to connecting to MQTT: {}".format(e)) + led_error_code(led, 2) + time.sleep(5) + continue # Start back at the top of the While Loop + except: + continue - mqtt_client.disconnect() - if DEBUG: - print(f'MQTT Disconnected.') + # Ready to Publish? + try: + if wifi_ready and mqtt_ready and dht22_ready: + # Timestamp + time_now = time.localtime() + timestamp = "{}/{}/{} {}:{}:{}".format(time_now[1],time_now[2],time_now[0],time_now[3],time_now[4],time_now[5]) + + # Build JSON Payloads + dht_data = { + 'Location':mqtt_config.MQTT_ZONE_ID, + 'Temperature':temp, + 'Relative Humidity':hum, + } + dht_payload = json.dumps(dht_data) + + hw_data = { + 'Timestamp':timestamp, + 'CPU Temperature':cpu_temp, + 'Device':mqtt_config.MQTT_HW_ID, + 'WiFi Information':ifconfig, + } + hw_payload = json.dumps(hw_data) + + # Publish + mqtt_client.publish("home/{}".format(mqtt_config.MQTT_ZONE_ID),dht_payload, retain=True) + mqtt_client.publish("hw/{}".format(mqtt_config.MQTT_HW_ID),hw_payload, retain=True) + + mqtt_client.disconnect() + if DEBUG: + print(f'MQTT Disconnected.') + + except: + continue # Sleep for a bit. if DEBUG: