fix(espnow): handle binary and JSON RX in simplified main
Use init_espnow for channel alignment; route wire CMD/GROUPS and JSON v1 payloads to process_data from the poll loop. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
48
src/main.py
48
src/main.py
@@ -3,14 +3,13 @@ from settings import Settings
|
||||
import machine
|
||||
import asyncio
|
||||
import gc
|
||||
import json
|
||||
import network
|
||||
import espnow
|
||||
from presets import Presets
|
||||
from controller_messages import apply_startup_pattern
|
||||
from background_tasks import presets_loop
|
||||
from espnow_transport import espnow_receive_loop, init_espnow, send_boot_announce
|
||||
from mem_stats import print_mem
|
||||
import json
|
||||
from controller_messages import apply_startup_pattern, process_data
|
||||
from espnow_transport import _handle_packet, init_espnow
|
||||
from espnow_wire import BROADCAST_MAC, WIRE_MAGIC
|
||||
|
||||
wdt = machine.WDT(timeout=10000)
|
||||
wdt.feed()
|
||||
@@ -28,22 +27,29 @@ gc.collect()
|
||||
|
||||
apply_startup_pattern(settings, presets)
|
||||
|
||||
sta_if = network.WLAN(network.STA_IF)
|
||||
sta_if.active(True)
|
||||
print(sta_if.ifconfig())
|
||||
print(sta_if.config("channel"))
|
||||
|
||||
esp = espnow.ESPNow()
|
||||
esp.active(True)
|
||||
esp.add_peer(b"\xff\xff\xff\xff\xff\xff")
|
||||
esp = init_espnow(settings)
|
||||
print(network.WLAN(network.STA_IF).config("channel"))
|
||||
|
||||
hello = json.dumps({
|
||||
"v": "1",
|
||||
"settings": settings,
|
||||
"name": settings.get("name", "led"),
|
||||
"type": "led",
|
||||
})
|
||||
esp.send(b"\xff\xff\xff\xff\xff\xff", hello)
|
||||
print(hello)
|
||||
try:
|
||||
esp.send(BROADCAST_MAC, hello)
|
||||
print("espnow hello", len(hello), "B")
|
||||
except Exception as e:
|
||||
print("espnow hello failed:", e)
|
||||
|
||||
|
||||
def _on_espnow_message(msg):
|
||||
if not msg:
|
||||
return
|
||||
if msg[0] == WIRE_MAGIC:
|
||||
_handle_packet(msg, settings, presets)
|
||||
return
|
||||
if msg[0:1] == b"{":
|
||||
process_data(msg, settings, presets)
|
||||
|
||||
|
||||
async def main():
|
||||
@@ -52,8 +58,14 @@ async def main():
|
||||
wdt.feed()
|
||||
if esp.any():
|
||||
host, msg = esp.recv(0)
|
||||
print(host, msg)
|
||||
if host and msg:
|
||||
print(host, len(msg), "B")
|
||||
try:
|
||||
_on_espnow_message(msg)
|
||||
except Exception as e:
|
||||
print("espnow rx error:", e)
|
||||
await asyncio.sleep(0)
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
|
||||
Reference in New Issue
Block a user