feat(espnow): Pi bridge client, binary wire, and espnow-sender firmware
Replace serial/Wi-Fi driver transport paths with WebSocket bridge client, binary espnow_wire delivery, device announce registry, and restructured espnow-sender (AP + broadcast passthrough). Includes docs and tests. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
76
espnow-sender/src/main.py
Normal file
76
espnow-sender/src/main.py
Normal file
@@ -0,0 +1,76 @@
|
||||
import asyncio
|
||||
import time
|
||||
|
||||
from microdot import Microdot
|
||||
from microdot.websocket import WebSocketError, with_websocket
|
||||
|
||||
import aioespnow
|
||||
import machine
|
||||
import network
|
||||
from settings import Settings
|
||||
|
||||
|
||||
wdt = machine.WDT(timeout=10000)
|
||||
wdt.feed()
|
||||
settings = Settings()
|
||||
print(settings)
|
||||
|
||||
app = Microdot()
|
||||
|
||||
ap_if = network.WLAN(network.AP_IF)
|
||||
ap_if.active(True)
|
||||
ap_if.config(ssid=settings.get("name"), password=settings.get("ap_password"))
|
||||
print(ap_if.ifconfig())
|
||||
|
||||
sta_if = network.WLAN(network.STA_IF)
|
||||
sta_if.active(True)
|
||||
print(sta_if.config("channel"))
|
||||
|
||||
esp = aioespnow.AIOESPNow()
|
||||
esp.active(True)
|
||||
esp.add_peer(b"\xff\xff\xff\xff\xff\xff")
|
||||
|
||||
clients = set()
|
||||
|
||||
@app.route("/ws")
|
||||
@with_websocket
|
||||
async def ws(request, ws):
|
||||
clients.add(ws)
|
||||
while True:
|
||||
|
||||
try:
|
||||
raw = await ws.receive()
|
||||
except WebSocketError as err:
|
||||
print(err)
|
||||
break
|
||||
if not raw:
|
||||
break
|
||||
try:
|
||||
await esp.asend(b"\xff\xff\xff\xff\xff\xff", raw)
|
||||
print(raw)
|
||||
except Exception as err:
|
||||
print(err)
|
||||
break
|
||||
ws.close()
|
||||
clients.discard(ws)
|
||||
|
||||
async def _espnow_receive_loop():
|
||||
async for host, msg in esp.airecv():
|
||||
print(host, msg)
|
||||
for client in clients:
|
||||
await client.send(msg)
|
||||
|
||||
|
||||
async def _wdt_feed_loop():
|
||||
while True:
|
||||
await asyncio.sleep(1)
|
||||
wdt.feed()
|
||||
|
||||
async def main():
|
||||
asyncio.create_task(_wdt_feed_loop())
|
||||
asyncio.create_task(_espnow_receive_loop())
|
||||
await app.start_server(host="0.0.0.0", port=80)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
Reference in New Issue
Block a user