WiFi made more resilient
This commit is contained in:
77
gauge.py
77
gauge.py
@@ -526,14 +526,27 @@ _last_wifi_check = 0
|
|||||||
_wifi_sta = None
|
_wifi_sta = None
|
||||||
|
|
||||||
|
|
||||||
def connect_wifi(ssid, password, timeout_s=15):
|
def connect_wifi(ssid, password, timeout_s=15, force_reconnect=False):
|
||||||
global _wifi_sta
|
global _wifi_sta
|
||||||
_wifi_sta = network.WLAN(network.STA_IF)
|
_wifi_sta = network.WLAN(network.STA_IF)
|
||||||
|
|
||||||
|
if force_reconnect:
|
||||||
|
try:
|
||||||
|
_wifi_sta.disconnect()
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
try:
|
||||||
|
_wifi_sta.active(False)
|
||||||
|
utime.sleep_ms(250)
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
_wifi_sta.active(True)
|
_wifi_sta.active(True)
|
||||||
if _wifi_sta.isconnected():
|
if _wifi_sta.isconnected() and not force_reconnect:
|
||||||
ip, mask, gw, dns = _wifi_sta.ifconfig()
|
ip, mask, gw, dns = _wifi_sta.ifconfig()
|
||||||
info("WiFi already connected")
|
info("WiFi already connected")
|
||||||
info(f" IP:{ip} mask:{mask} gw:{gw} dns:{dns}")
|
info(f" IP:{ip} mask:{mask} gw:{gw} dns:{dns}")
|
||||||
|
utime.sleep_ms(250)
|
||||||
return ip
|
return ip
|
||||||
info(f"WiFi connecting to '{ssid}' ...")
|
info(f"WiFi connecting to '{ssid}' ...")
|
||||||
_wifi_sta.connect(ssid, password)
|
_wifi_sta.connect(ssid, password)
|
||||||
@@ -549,6 +562,7 @@ def connect_wifi(ssid, password, timeout_s=15):
|
|||||||
info(f" SSID : {ssid}")
|
info(f" SSID : {ssid}")
|
||||||
info(f" MAC : {mac}")
|
info(f" MAC : {mac}")
|
||||||
info(f" IP : {ip} mask:{mask} gw:{gw} dns:{dns}")
|
info(f" IP : {ip} mask:{mask} gw:{gw} dns:{dns}")
|
||||||
|
utime.sleep_ms(250)
|
||||||
return ip
|
return ip
|
||||||
|
|
||||||
|
|
||||||
@@ -858,19 +872,36 @@ def _subscribe_all(c):
|
|||||||
def connect_mqtt():
|
def connect_mqtt():
|
||||||
global client_ref, _mqtt_connected
|
global client_ref, _mqtt_connected
|
||||||
info(f"Connecting to MQTT broker {MQTT_BROKER}:{MQTT_PORT} ...")
|
info(f"Connecting to MQTT broker {MQTT_BROKER}:{MQTT_PORT} ...")
|
||||||
client = MQTTClient(
|
last_error = None
|
||||||
client_id=MQTT_CLIENT_ID,
|
for attempt in range(3):
|
||||||
server=MQTT_BROKER,
|
try:
|
||||||
port=MQTT_PORT,
|
if client_ref is not None:
|
||||||
user=MQTT_USER,
|
try:
|
||||||
password=MQTT_PASSWORD,
|
client_ref.disconnect()
|
||||||
keepalive=30,
|
except Exception:
|
||||||
)
|
pass
|
||||||
client.set_callback(on_message)
|
|
||||||
client.connect()
|
client = MQTTClient(
|
||||||
client_ref = client
|
client_id=MQTT_CLIENT_ID,
|
||||||
_mqtt_connected = True
|
server=MQTT_BROKER,
|
||||||
info(f"MQTT connected client_id={MQTT_CLIENT_ID}")
|
port=MQTT_PORT,
|
||||||
|
user=MQTT_USER,
|
||||||
|
password=MQTT_PASSWORD,
|
||||||
|
keepalive=30,
|
||||||
|
)
|
||||||
|
client.set_callback(on_message)
|
||||||
|
client.connect()
|
||||||
|
client_ref = client
|
||||||
|
_mqtt_connected = True
|
||||||
|
info(f"MQTT connected client_id={MQTT_CLIENT_ID}")
|
||||||
|
return
|
||||||
|
except Exception as e:
|
||||||
|
last_error = e
|
||||||
|
log_err(f"MQTT connect attempt {attempt + 1} failed: {e}")
|
||||||
|
utime.sleep_ms(1000)
|
||||||
|
|
||||||
|
_mqtt_connected = False
|
||||||
|
raise last_error
|
||||||
|
|
||||||
|
|
||||||
_mqtt_check_interval_ms = 30000
|
_mqtt_check_interval_ms = 30000
|
||||||
@@ -1124,10 +1155,7 @@ def _append_vfd_discovery(entries, dev_ref):
|
|||||||
"cmd_t": vfd_topics["set"],
|
"cmd_t": vfd_topics["set"],
|
||||||
"stat_t": vfd_topics["state"],
|
"stat_t": vfd_topics["state"],
|
||||||
"avty_t": gauge_topics[0]["status"],
|
"avty_t": gauge_topics[0]["status"],
|
||||||
"max": 4,
|
|
||||||
"mode": "text",
|
|
||||||
"icon": "mdi:alpha-box",
|
"icon": "mdi:alpha-box",
|
||||||
"pattern": "^[0-9A-Fa-f-]{0,4}$",
|
|
||||||
"dev": dev_ref,
|
"dev": dev_ref,
|
||||||
},
|
},
|
||||||
"Discovery: VFD text",
|
"Discovery: VFD text",
|
||||||
@@ -1206,10 +1234,13 @@ def service_discovery():
|
|||||||
return
|
return
|
||||||
|
|
||||||
topic, payload, log_msg = _discovery_queue[_discovery_idx]
|
topic, payload, log_msg = _discovery_queue[_discovery_idx]
|
||||||
if isinstance(payload, bytes):
|
try:
|
||||||
client_ref.publish(topic, payload, retain=True)
|
if isinstance(payload, bytes):
|
||||||
else:
|
client_ref.publish(topic, payload, retain=True)
|
||||||
_publish_discovery_entity(client_ref, topic, payload, log_msg)
|
else:
|
||||||
|
_publish_discovery_entity(client_ref, topic, payload, log_msg)
|
||||||
|
except Exception as e:
|
||||||
|
log_err(f"Discovery publish failed for {topic}: {e}")
|
||||||
_discovery_idx += 1
|
_discovery_idx += 1
|
||||||
_last_discovery_ms = utime.ticks_ms()
|
_last_discovery_ms = utime.ticks_ms()
|
||||||
if (_discovery_idx & 3) == 0:
|
if (_discovery_idx & 3) == 0:
|
||||||
@@ -1247,7 +1278,7 @@ def main():
|
|||||||
info("Gauge MQTT controller starting")
|
info("Gauge MQTT controller starting")
|
||||||
info("=" * 48)
|
info("=" * 48)
|
||||||
|
|
||||||
connect_wifi(WIFI_SSID, WIFI_PASSWORD)
|
connect_wifi(WIFI_SSID, WIFI_PASSWORD, force_reconnect=True)
|
||||||
|
|
||||||
connect_mqtt()
|
connect_mqtt()
|
||||||
_subscribe_all(client_ref)
|
_subscribe_all(client_ref)
|
||||||
|
|||||||
Reference in New Issue
Block a user