Files
led-driver/tests/udp_client.py
2026-04-11 15:19:07 +12:00

72 lines
1.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
"""UDP discovery test — runs on MicroPython (ESP32).
Brings up Wi-Fi from settings (test harness only), then **`hello.discover_controller_udp(device_name, wdt)`**.
`hello` does not use Settings or connect WiFi.
In firmware, **`main.py`** discovers the controller IP in RAM for HTTP; it is not written to settings.
Deploy `src` (including `hello.py`), then from host with cwd `led-driver`:
mpremote connect PORT run tests/udp_client.py
"""
import time
import network
import utime
from machine import WDT
from hello import discover_controller_udp
from settings import Settings
CONNECT_WAIT_S = 45
WDT_MS = 10000
def _wait_wifi(sta, timeout_s, wdt):
deadline = utime.ticks_add(utime.ticks_ms(), int(timeout_s * 1000))
while not sta.isconnected():
wdt.feed()
if utime.ticks_diff(deadline, utime.ticks_ms()) <= 0:
return False
print("WiFi status:", sta.status())
wdt.feed()
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("udp_client: set ssid/password in settings.json (test harness Wi-Fi).")
raise SystemExit(1)
sta = network.WLAN(network.STA_IF)
sta.active(True)
try:
sta.config(pm=network.WLAN.PM_NONE)
except (AttributeError, ValueError, TypeError):
pass
wdt = WDT(timeout=WDT_MS)
wdt.feed()
print("udp_client: connecting to", repr(ssid))
sta.connect(ssid, password)
wdt.feed()
if not _wait_wifi(sta, CONNECT_WAIT_S, wdt):
print("WiFi timeout, status=", sta.status())
raise SystemExit(1)
ip = discover_controller_udp(settings.get("name", ""), wdt=wdt)
if not ip:
raise SystemExit(1)
if __name__ == "__main__":
main()