WiFi changes again
This commit is contained in:
44
gauge.py
44
gauge.py
@@ -524,46 +524,62 @@ _DEVICE = {
|
||||
_wifi_check_interval_ms = 30000
|
||||
_last_wifi_check = 0
|
||||
_wifi_sta = None
|
||||
_WIFI_CONNECT_ATTEMPTS = 3
|
||||
|
||||
|
||||
def connect_wifi(ssid, password, timeout_s=15, force_reconnect=False):
|
||||
def _reset_wifi_interface():
|
||||
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)
|
||||
utime.sleep_ms(500)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
_wifi_sta.active(True)
|
||||
utime.sleep_ms(500)
|
||||
|
||||
|
||||
def connect_wifi(ssid, password, timeout_s=15, force_reconnect=False):
|
||||
global _wifi_sta
|
||||
_wifi_sta = network.WLAN(network.STA_IF)
|
||||
|
||||
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}' ...")
|
||||
|
||||
last_error = None
|
||||
for attempt in range(_WIFI_CONNECT_ATTEMPTS):
|
||||
info(f"WiFi connecting to '{ssid}' (attempt {attempt + 1}/{_WIFI_CONNECT_ATTEMPTS}) ...")
|
||||
_reset_wifi_interface()
|
||||
try:
|
||||
_wifi_sta.connect(ssid, password)
|
||||
deadline = utime.time() + timeout_s
|
||||
while not _wifi_sta.isconnected():
|
||||
if utime.time() > deadline:
|
||||
log_err(f"WiFi connect timeout after {timeout_s}s")
|
||||
raise OSError("WiFi connect timeout")
|
||||
utime.sleep_ms(200)
|
||||
utime.sleep_ms(250)
|
||||
|
||||
ip, mask, gw, dns = _wifi_sta.ifconfig()
|
||||
mac = ":".join(f"{b:02x}" for b in _wifi_sta.config("mac"))
|
||||
info("WiFi connected!")
|
||||
info(f" SSID : {ssid}")
|
||||
info(f" MAC : {mac}")
|
||||
info(f" IP : {ip} mask:{mask} gw:{gw} dns:{dns}")
|
||||
utime.sleep_ms(250)
|
||||
utime.sleep_ms(500)
|
||||
return ip
|
||||
except Exception as e:
|
||||
last_error = e
|
||||
log_err(f"WiFi connect attempt {attempt + 1} failed: {e}")
|
||||
utime.sleep_ms(1000)
|
||||
|
||||
raise last_error
|
||||
|
||||
|
||||
def check_wifi():
|
||||
@@ -581,15 +597,7 @@ def check_wifi():
|
||||
|
||||
log_err("WiFi lost connection — attempting reconnect...")
|
||||
try:
|
||||
_wifi_sta.active(True)
|
||||
_wifi_sta.connect(WIFI_SSID, WIFI_PASSWORD)
|
||||
deadline = utime.time() + 15
|
||||
while not _wifi_sta.isconnected():
|
||||
if utime.time() > deadline:
|
||||
log_err("WiFi reconnect timeout")
|
||||
return
|
||||
utime.sleep_ms(200)
|
||||
ip, mask, gw, dns = _wifi_sta.ifconfig()
|
||||
ip = connect_wifi(WIFI_SSID, WIFI_PASSWORD, timeout_s=15, force_reconnect=True)
|
||||
info(f"WiFi reconnected! IP:{ip}")
|
||||
except Exception as e:
|
||||
log_err(f"WiFi reconnect failed: {e}")
|
||||
|
||||
Reference in New Issue
Block a user