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
|
||||
|
||||
|
||||
def connect_wifi(ssid, password, timeout_s=15):
|
||||
def connect_wifi(ssid, password, timeout_s=15, force_reconnect=False):
|
||||
global _wifi_sta
|
||||
_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)
|
||||
if _wifi_sta.isconnected():
|
||||
if _wifi_sta.isconnected() and not force_reconnect:
|
||||
ip, mask, gw, dns = _wifi_sta.ifconfig()
|
||||
info("WiFi already connected")
|
||||
info(f" IP:{ip} mask:{mask} gw:{gw} dns:{dns}")
|
||||
utime.sleep_ms(250)
|
||||
return ip
|
||||
info(f"WiFi connecting to '{ssid}' ...")
|
||||
_wifi_sta.connect(ssid, password)
|
||||
@@ -549,6 +562,7 @@ def connect_wifi(ssid, password, timeout_s=15):
|
||||
info(f" SSID : {ssid}")
|
||||
info(f" MAC : {mac}")
|
||||
info(f" IP : {ip} mask:{mask} gw:{gw} dns:{dns}")
|
||||
utime.sleep_ms(250)
|
||||
return ip
|
||||
|
||||
|
||||
@@ -858,19 +872,36 @@ def _subscribe_all(c):
|
||||
def connect_mqtt():
|
||||
global client_ref, _mqtt_connected
|
||||
info(f"Connecting to MQTT broker {MQTT_BROKER}:{MQTT_PORT} ...")
|
||||
client = MQTTClient(
|
||||
client_id=MQTT_CLIENT_ID,
|
||||
server=MQTT_BROKER,
|
||||
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}")
|
||||
last_error = None
|
||||
for attempt in range(3):
|
||||
try:
|
||||
if client_ref is not None:
|
||||
try:
|
||||
client_ref.disconnect()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
client = MQTTClient(
|
||||
client_id=MQTT_CLIENT_ID,
|
||||
server=MQTT_BROKER,
|
||||
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
|
||||
@@ -1124,10 +1155,7 @@ def _append_vfd_discovery(entries, dev_ref):
|
||||
"cmd_t": vfd_topics["set"],
|
||||
"stat_t": vfd_topics["state"],
|
||||
"avty_t": gauge_topics[0]["status"],
|
||||
"max": 4,
|
||||
"mode": "text",
|
||||
"icon": "mdi:alpha-box",
|
||||
"pattern": "^[0-9A-Fa-f-]{0,4}$",
|
||||
"dev": dev_ref,
|
||||
},
|
||||
"Discovery: VFD text",
|
||||
@@ -1206,10 +1234,13 @@ def service_discovery():
|
||||
return
|
||||
|
||||
topic, payload, log_msg = _discovery_queue[_discovery_idx]
|
||||
if isinstance(payload, bytes):
|
||||
client_ref.publish(topic, payload, retain=True)
|
||||
else:
|
||||
_publish_discovery_entity(client_ref, topic, payload, log_msg)
|
||||
try:
|
||||
if isinstance(payload, bytes):
|
||||
client_ref.publish(topic, payload, retain=True)
|
||||
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
|
||||
_last_discovery_ms = utime.ticks_ms()
|
||||
if (_discovery_idx & 3) == 0:
|
||||
@@ -1247,7 +1278,7 @@ def main():
|
||||
info("Gauge MQTT controller starting")
|
||||
info("=" * 48)
|
||||
|
||||
connect_wifi(WIFI_SSID, WIFI_PASSWORD)
|
||||
connect_wifi(WIFI_SSID, WIFI_PASSWORD, force_reconnect=True)
|
||||
|
||||
connect_mqtt()
|
||||
_subscribe_all(client_ref)
|
||||
|
||||
Reference in New Issue
Block a user