From 6b9d6c2dcd083d9c440098f48ca5ecf090a4a3e6 Mon Sep 17 00:00:00 2001 From: arofarn Date: Sat, 16 May 2020 14:25:17 +0200 Subject: [PATCH] Add some verification about network connection before Mqtt and NTP connection --- code/main.py | 82 +++++++++++++++++++++++++++++++++------------------- 1 file changed, 53 insertions(+), 29 deletions(-) diff --git a/code/main.py b/code/main.py index 986aa8f..7c95333 100644 --- a/code/main.py +++ b/code/main.py @@ -73,33 +73,57 @@ CURRENT_MODE = 0 PWROFF_DELAY = 0 MQTT_DELAY = 2 # Délais en sec. avant d'envoyer les données au MQTT -# Connection au broker MQTT -CLIENT = MQTTClient(client_id=config.CLIENT_ID, - server=config.MQTT_HOST, - user=config.MQTT_USERNAME, - password=config.MQTT_PASSWD, - port=config.MQTT_PORT) -CLIENT.DEBUG = True -CLIENT.connect() ############# # FUNCTIONS # ############# -def set_rtc(): +def MqttConnect(config, cb): + # Connection au broker MQTT + CLIENT = MQTTClient(client_id=config.CLIENT_ID, + server=config.MQTT_HOST, + user=config.MQTT_USERNAME, + password=config.MQTT_PASSWD, + port=config.MQTT_PORT) + CLIENT.DEBUG = True + try: + if WLAN.isconnected(): + CLIENT.reconnect() + except Exception as err: + print(err) + + if CLIENT.is_connected: + # MQTT subscription + print("MqttConnect: Set callback...") + CLIENT.set_callback(cb) + print("OK") + print("MqttConnect: Subcribe...") + CLIENT.subscribe(b"{location}/{device}/#".format(location=config.LOCATION, + device=CLIENT.client_id)) + print("OK") + + return CLIENT + +def set_rtc(retry = 3): + n = 0 while True: + n += 1 try: return ntptime.settime() except OSError as err: - print("OS error: {0}".format(err)) + print("set_rtc() : OS error: {0}".format(err)) if err.args[0] == 110: - time.sleep(2) + time.sleep(0.5) except OverflowError as err: - print("OverflowError: {0}".format(err)) - time.sleep(2) + print("set_rtc() : OverflowError: {0}".format(err)) + time.sleep(0.2) except Exception as err: - return print(err) + return print("set_rtc() : ", err) + + if n >= retry: + print("set_rtc() : retry limit reached : ", n) + break def now(clock): @@ -150,9 +174,10 @@ def mqtt_callback(topic, msg): print("Pas une valeur. Ignore...") - def power_cycle(): """ON/OFF function""" + global PWR + global BRIGHTN PWR = not PWR print("Power :", PWR) @@ -220,7 +245,10 @@ def update_conn_led(): # BOUCLE PRINCIPALE # ##################### -set_rtc() +if WLAN.isconnected(): + set_rtc() + +CLIENT = MqttConnect(config, mqtt_callback) # Envoi de l'état de départ au broker data = [["last_boot", now(rtc), True, 1], @@ -237,16 +265,6 @@ data = [["last_boot", now(rtc), True, 1], ] mqtt_multipub(data) -# MQTT subscription -print("Set callback...") -CLIENT.set_callback(mqtt_callback) -print("OK") -print("Subcribe...") -CLIENT.subscribe(b"{location}/{device}/#".format(location=config.LOCATION, - device=CLIENT.client_id)) -print("OK") -time.sleep(1) - BRIGHTN_DATA = None while True: @@ -275,11 +293,14 @@ while True: update_conn_led() - CLIENT.check_msg() + if WLAN.isconnected() and CLIENT.is_connected: + CLIENT.check_msg() # On n'envoie les données de luminosité qu'une fois stabilisée (MQTT_DELAY) - if BRIGHTN_DATA is not None and time.ticks_diff(time.ticks_ms(), MQTT_TIMER) >= 0: - mqtt_multipub(BRIGHTN_DATA) + if BRIGHTN_DATA is not None and time.ticks_diff(time.ticks_ms(), + MQTT_TIMER) >= 0: + if WLAN.isconnected() and CLIENT.is_connected: + mqtt_multipub(BRIGHTN_DATA) BRIGHTN_DATA = None time_to_test = rtc.datetime() @@ -289,3 +310,6 @@ while True: set_rtc() print("Time set: {0:04d}/{1:02d}/{2:02d}_{4:02d}:{5:02d}:{6:02d}.{7:03d}".format(*rtc.datetime())) time.sleep(1) + + if time_to_test[6] == 0 and not CLIENT.is_connected: + CLIENT = MqttConnect(config, mqtt_callback)