Reconnect when WiFi lacks a valid IP address
This commit is contained in:
parent
b5b9b492f2
commit
db97b8eb69
1 changed files with 15 additions and 6 deletions
21
main.py
21
main.py
|
@ -20,6 +20,7 @@ VOLUME_MAX = const(128)
|
||||||
|
|
||||||
MQTT_KEEPALIVE = const(60)
|
MQTT_KEEPALIVE = const(60)
|
||||||
MQTT_UPDATE_INTERVAL = const(60)
|
MQTT_UPDATE_INTERVAL = const(60)
|
||||||
|
MQTT_RECONNECT_INTERVAL = const(60)
|
||||||
|
|
||||||
channels = ["LINE 1", "LINE 2", "LINE 3", "PHONO"]
|
channels = ["LINE 1", "LINE 2", "LINE 3", "PHONO"]
|
||||||
state = StateTree(
|
state = StateTree(
|
||||||
|
@ -72,6 +73,7 @@ mqtt = None
|
||||||
mqtt_client_id = ubinascii.hexlify(machine.unique_id())
|
mqtt_client_id = ubinascii.hexlify(machine.unique_id())
|
||||||
mqtt_broker = settings["mqtt"]["broker"]
|
mqtt_broker = settings["mqtt"]["broker"]
|
||||||
mqtt_prefix = settings["mqtt"]["prefix"]
|
mqtt_prefix = settings["mqtt"]["prefix"]
|
||||||
|
last_mqtt_attempt = 0
|
||||||
|
|
||||||
|
|
||||||
def mqtt_init():
|
def mqtt_init():
|
||||||
|
@ -212,7 +214,8 @@ def on_message(topic, msg):
|
||||||
|
|
||||||
|
|
||||||
def loop():
|
def loop():
|
||||||
global mqtt, state, last_update, rotary, rotary_button, rotary_value
|
global mqtt, state, last_update, last_mqtt_attempt
|
||||||
|
global rotary, rotary_button, rotary_value
|
||||||
|
|
||||||
rotary_button.update()
|
rotary_button.update()
|
||||||
if rotary_button.was_clicked():
|
if rotary_button.was_clicked():
|
||||||
|
@ -250,17 +253,23 @@ def loop():
|
||||||
if sta_if.active() and not sta_if.isconnected():
|
if sta_if.active() and not sta_if.isconnected():
|
||||||
state["network"] = "ACT"
|
state["network"] = "ACT"
|
||||||
if sta_if.isconnected():
|
if sta_if.isconnected():
|
||||||
if state["network"] != "OK":
|
ip, _, _, _ = sta_if.ifconfig()
|
||||||
ip, _, _, _ = sta_if.ifconfig()
|
if ip == "0.0.0.0":
|
||||||
|
# Something went wrong, try to reconnect
|
||||||
|
print("IP invalid, retrying WiFi connection")
|
||||||
|
state["network"] = "ACT"
|
||||||
|
sta_if.active(True)
|
||||||
|
sta_if.connect(settings["wifi"]["ssid"], settings["wifi"]["password"])
|
||||||
|
elif state["network"] != "OK":
|
||||||
print(f"WIFI Connected to {sta_if.config('ssid')}")
|
print(f"WIFI Connected to {sta_if.config('ssid')}")
|
||||||
print(f"IP Address: {ip}")
|
print(f"IP Address: {ip}")
|
||||||
state["network"] = "OK"
|
state["network"] = "OK"
|
||||||
if mqtt is None:
|
if not mqtt and utime.time() - last_mqtt_attempt >= MQTT_RECONNECT_INTERVAL:
|
||||||
|
last_mqtt_attempt = utime.time()
|
||||||
try:
|
try:
|
||||||
mqtt = mqtt_init()
|
mqtt = mqtt_init()
|
||||||
except OSError as e:
|
except OSError as e:
|
||||||
print(f"Failed to connect to MQTT: {e}")
|
print(f"Failed to connect to MQTT ({mqtt_broker}): {e}")
|
||||||
mqtt = False
|
|
||||||
if mqtt:
|
if mqtt:
|
||||||
if state.changed or utime.time() - last_update >= MQTT_UPDATE_INTERVAL:
|
if state.changed or utime.time() - last_update >= MQTT_UPDATE_INTERVAL:
|
||||||
topic = f"{mqtt_prefix}/state".encode()
|
topic = f"{mqtt_prefix}/state".encode()
|
||||||
|
|
Loading…
Reference in a new issue