refactor(led-driver): simplify websocket runtime and test layout
This commit is contained in:
102
tests/test_wifi.py
Normal file
102
tests/test_wifi.py
Normal file
@@ -0,0 +1,102 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Wi-Fi connection smoke test for MicroPython on ESP32.
|
||||
|
||||
Runs on-device via mpremote and uses /settings.json credentials.
|
||||
|
||||
Usage:
|
||||
mpremote connect /dev/ttyACM0 run tests/test_wifi.py
|
||||
"""
|
||||
|
||||
import time
|
||||
import utime
|
||||
import network
|
||||
from machine import WDT
|
||||
|
||||
from settings import Settings
|
||||
|
||||
CONNECT_TIMEOUT_S = 30
|
||||
RETRY_DELAY_S = 2
|
||||
WDT_TIMEOUT_MS = 10000
|
||||
|
||||
|
||||
def _wifi_status_label(code):
|
||||
names = {
|
||||
getattr(network, "STAT_IDLE", 0): "idle",
|
||||
getattr(network, "STAT_CONNECTING", 1): "connecting",
|
||||
getattr(network, "STAT_WRONG_PASSWORD", -3): "wrong_password",
|
||||
getattr(network, "STAT_NO_AP_FOUND", -2): "no_ap_found",
|
||||
getattr(network, "STAT_CONNECT_FAIL", -1): "connect_fail",
|
||||
getattr(network, "STAT_GOT_IP", 3): "got_ip",
|
||||
}
|
||||
return names.get(code, str(code))
|
||||
|
||||
|
||||
def connect_wifi_with_wdt(sta, ssid, password, wdt):
|
||||
attempt = 0
|
||||
while not sta.isconnected():
|
||||
attempt += 1
|
||||
print("[wifi-test] attempt", attempt, "ssid=", repr(ssid))
|
||||
try:
|
||||
sta.disconnect()
|
||||
except Exception:
|
||||
pass
|
||||
sta.connect(ssid, password)
|
||||
|
||||
start = utime.time()
|
||||
last_status = None
|
||||
while not sta.isconnected():
|
||||
status = sta.status()
|
||||
if status != last_status:
|
||||
print("[wifi-test] status:", status, _wifi_status_label(status))
|
||||
last_status = status
|
||||
if status in (
|
||||
getattr(network, "STAT_WRONG_PASSWORD", -3),
|
||||
getattr(network, "STAT_NO_AP_FOUND", -2),
|
||||
getattr(network, "STAT_CONNECT_FAIL", -1),
|
||||
):
|
||||
break
|
||||
if utime.time() - start >= CONNECT_TIMEOUT_S:
|
||||
print("[wifi-test] timeout after", CONNECT_TIMEOUT_S, "seconds")
|
||||
break
|
||||
time.sleep(1)
|
||||
wdt.feed()
|
||||
|
||||
if sta.isconnected():
|
||||
return True
|
||||
|
||||
print("[wifi-test] retry in", RETRY_DELAY_S, "seconds")
|
||||
for _ in range(RETRY_DELAY_S):
|
||||
time.sleep(1)
|
||||
wdt.feed()
|
||||
return True
|
||||
|
||||
|
||||
def main():
|
||||
settings = Settings()
|
||||
ssid = settings.get("ssid") or ""
|
||||
password = settings.get("password") or ""
|
||||
|
||||
if not ssid:
|
||||
print("[wifi-test] skipped: settings.ssid is empty")
|
||||
raise SystemExit(0)
|
||||
|
||||
wdt = WDT(timeout=WDT_TIMEOUT_MS)
|
||||
wdt.feed()
|
||||
|
||||
sta = network.WLAN(network.STA_IF)
|
||||
sta.active(True)
|
||||
try:
|
||||
sta.config(pm=network.WLAN.PM_NONE)
|
||||
except (AttributeError, ValueError, TypeError):
|
||||
pass
|
||||
|
||||
ok = connect_wifi_with_wdt(sta, ssid, password, wdt)
|
||||
if not ok or not sta.isconnected():
|
||||
print("[wifi-test] FAILED: not connected")
|
||||
raise SystemExit(1)
|
||||
|
||||
print("[wifi-test] OK:", sta.ifconfig())
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user