Add some verification about network connection
before Mqtt and NTP connection
This commit is contained in:
parent
c97b1ab2fa
commit
6b9d6c2dcd
68
code/main.py
68
code/main.py
@ -73,6 +73,13 @@ CURRENT_MODE = 0
|
|||||||
PWROFF_DELAY = 0
|
PWROFF_DELAY = 0
|
||||||
MQTT_DELAY = 2 # Délais en sec. avant d'envoyer les données au MQTT
|
MQTT_DELAY = 2 # Délais en sec. avant d'envoyer les données au MQTT
|
||||||
|
|
||||||
|
|
||||||
|
#############
|
||||||
|
# FUNCTIONS #
|
||||||
|
#############
|
||||||
|
|
||||||
|
|
||||||
|
def MqttConnect(config, cb):
|
||||||
# Connection au broker MQTT
|
# Connection au broker MQTT
|
||||||
CLIENT = MQTTClient(client_id=config.CLIENT_ID,
|
CLIENT = MQTTClient(client_id=config.CLIENT_ID,
|
||||||
server=config.MQTT_HOST,
|
server=config.MQTT_HOST,
|
||||||
@ -80,26 +87,43 @@ CLIENT = MQTTClient(client_id=config.CLIENT_ID,
|
|||||||
password=config.MQTT_PASSWD,
|
password=config.MQTT_PASSWD,
|
||||||
port=config.MQTT_PORT)
|
port=config.MQTT_PORT)
|
||||||
CLIENT.DEBUG = True
|
CLIENT.DEBUG = True
|
||||||
CLIENT.connect()
|
try:
|
||||||
|
if WLAN.isconnected():
|
||||||
|
CLIENT.reconnect()
|
||||||
|
except Exception as err:
|
||||||
|
print(err)
|
||||||
|
|
||||||
#############
|
if CLIENT.is_connected:
|
||||||
# FUNCTIONS #
|
# 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():
|
def set_rtc(retry = 3):
|
||||||
|
n = 0
|
||||||
while True:
|
while True:
|
||||||
|
n += 1
|
||||||
try:
|
try:
|
||||||
return ntptime.settime()
|
return ntptime.settime()
|
||||||
except OSError as err:
|
except OSError as err:
|
||||||
print("OS error: {0}".format(err))
|
print("set_rtc() : OS error: {0}".format(err))
|
||||||
if err.args[0] == 110:
|
if err.args[0] == 110:
|
||||||
time.sleep(2)
|
time.sleep(0.5)
|
||||||
except OverflowError as err:
|
except OverflowError as err:
|
||||||
print("OverflowError: {0}".format(err))
|
print("set_rtc() : OverflowError: {0}".format(err))
|
||||||
time.sleep(2)
|
time.sleep(0.2)
|
||||||
except Exception as err:
|
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):
|
def now(clock):
|
||||||
@ -150,9 +174,10 @@ def mqtt_callback(topic, msg):
|
|||||||
print("Pas une valeur. Ignore...")
|
print("Pas une valeur. Ignore...")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def power_cycle():
|
def power_cycle():
|
||||||
"""ON/OFF function"""
|
"""ON/OFF function"""
|
||||||
|
global PWR
|
||||||
|
global BRIGHTN
|
||||||
|
|
||||||
PWR = not PWR
|
PWR = not PWR
|
||||||
print("Power :", PWR)
|
print("Power :", PWR)
|
||||||
@ -220,8 +245,11 @@ def update_conn_led():
|
|||||||
# BOUCLE PRINCIPALE #
|
# BOUCLE PRINCIPALE #
|
||||||
#####################
|
#####################
|
||||||
|
|
||||||
|
if WLAN.isconnected():
|
||||||
set_rtc()
|
set_rtc()
|
||||||
|
|
||||||
|
CLIENT = MqttConnect(config, mqtt_callback)
|
||||||
|
|
||||||
# Envoi de l'état de départ au broker
|
# Envoi de l'état de départ au broker
|
||||||
data = [["last_boot", now(rtc), True, 1],
|
data = [["last_boot", now(rtc), True, 1],
|
||||||
["location", config.LOCATION, True, 1],
|
["location", config.LOCATION, True, 1],
|
||||||
@ -237,16 +265,6 @@ data = [["last_boot", now(rtc), True, 1],
|
|||||||
]
|
]
|
||||||
mqtt_multipub(data)
|
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
|
BRIGHTN_DATA = None
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
@ -275,10 +293,13 @@ while True:
|
|||||||
|
|
||||||
update_conn_led()
|
update_conn_led()
|
||||||
|
|
||||||
|
if WLAN.isconnected() and CLIENT.is_connected:
|
||||||
CLIENT.check_msg()
|
CLIENT.check_msg()
|
||||||
|
|
||||||
# On n'envoie les données de luminosité qu'une fois stabilisée (MQTT_DELAY)
|
# 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:
|
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)
|
mqtt_multipub(BRIGHTN_DATA)
|
||||||
BRIGHTN_DATA = None
|
BRIGHTN_DATA = None
|
||||||
|
|
||||||
@ -289,3 +310,6 @@ while True:
|
|||||||
set_rtc()
|
set_rtc()
|
||||||
print("Time set: {0:04d}/{1:02d}/{2:02d}_{4:02d}:{5:02d}:{6:02d}.{7:03d}".format(*rtc.datetime()))
|
print("Time set: {0:04d}/{1:02d}/{2:02d}_{4:02d}:{5:02d}:{6:02d}.{7:03d}".format(*rtc.datetime()))
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
|
|
||||||
|
if time_to_test[6] == 0 and not CLIENT.is_connected:
|
||||||
|
CLIENT = MqttConnect(config, mqtt_callback)
|
||||||
|
Loading…
Reference in New Issue
Block a user