60 lines
2.0 KiB
Python
60 lines
2.0 KiB
Python
import asyncio
|
|
import gc
|
|
import utime
|
|
|
|
from hello import broadcast_hello_udp
|
|
from wifi_sta import try_reconnect
|
|
|
|
_UDP_HELLO_ATTEMPT = 0
|
|
|
|
|
|
async def presets_loop(presets, wdt):
|
|
last_mem_log = utime.ticks_ms()
|
|
while True:
|
|
presets.tick()
|
|
wdt.feed()
|
|
if bool(getattr(presets, "debug", False)):
|
|
now = utime.ticks_ms()
|
|
if utime.ticks_diff(now, last_mem_log) >= 5000:
|
|
gc.collect()
|
|
print("mem runtime:", {"free": gc.mem_free(), "alloc": gc.mem_alloc()})
|
|
last_mem_log = now
|
|
# tick() does not await; yield so UDP hello and HTTP/WebSocket can run.
|
|
await asyncio.sleep(0)
|
|
|
|
|
|
async def udp_hello_loop_after_http_ready(sta_if, settings, wdt, runtime_state):
|
|
"""UDP hello on cadence; if STA drops, one reconnect campaign per iteration."""
|
|
global _UDP_HELLO_ATTEMPT
|
|
await asyncio.sleep(1)
|
|
started_ms = utime.ticks_ms()
|
|
while True:
|
|
try:
|
|
wifi_ok = sta_if.isconnected()
|
|
except Exception:
|
|
wifi_ok = False
|
|
if not wifi_ok:
|
|
ssid = settings.get("ssid") or ""
|
|
if ssid:
|
|
try_reconnect(sta_if, ssid, settings.get("password") or "", wdt)
|
|
try:
|
|
wifi_ok = sta_if.isconnected()
|
|
except Exception:
|
|
wifi_ok = False
|
|
if wifi_ok and runtime_state.hello:
|
|
_UDP_HELLO_ATTEMPT += 1
|
|
print("UDP hello broadcast attempt", _UDP_HELLO_ATTEMPT)
|
|
try:
|
|
broadcast_hello_udp(
|
|
sta_if,
|
|
settings.get("name", ""),
|
|
wait_reply=False,
|
|
wdt=wdt,
|
|
dual_destinations=True,
|
|
)
|
|
except Exception as ex:
|
|
print("UDP hello broadcast failed:", ex)
|
|
elapsed_ms = utime.ticks_diff(utime.ticks_ms(), started_ms)
|
|
interval_s = 10 if elapsed_ms < 120000 else 30
|
|
await asyncio.sleep(interval_s)
|