ESP-NOW: STA interface, notify browser on send failure
- Activate STA interface before ESP-NOW to fix ESP_ERR_ESPNOW_IF - Notify browser on send failure: WebSocket sends error JSON; preset API returns 503 - Use exceptions for failure (not return value) to avoid false errors when send succeeds - presets.js: handle server error messages in WebSocket onmessage Made-with: Cursor
This commit is contained in:
@@ -179,14 +179,20 @@ async def send_presets(request, session):
|
|||||||
batch = test_batch
|
batch = test_batch
|
||||||
last_msg = test_msg
|
last_msg = test_msg
|
||||||
else:
|
else:
|
||||||
|
try:
|
||||||
await send_chunk(batch)
|
await send_chunk(batch)
|
||||||
|
except Exception:
|
||||||
|
return json.dumps({"error": "ESP-NOW send failed"}), 503, {'Content-Type': 'application/json'}
|
||||||
await asyncio.sleep_ms(SEND_DELAY_MS)
|
await asyncio.sleep_ms(SEND_DELAY_MS)
|
||||||
messages_sent += 1
|
messages_sent += 1
|
||||||
batch = {name: preset_obj}
|
batch = {name: preset_obj}
|
||||||
last_msg = build_message(presets=batch, save=save_flag, default=default_id)
|
last_msg = build_message(presets=batch, save=save_flag, default=default_id)
|
||||||
|
|
||||||
if batch:
|
if batch:
|
||||||
|
try:
|
||||||
await send_chunk(batch)
|
await send_chunk(batch)
|
||||||
|
except Exception:
|
||||||
|
return json.dumps({"error": "ESP-NOW send failed"}), 503, {'Content-Type': 'application/json'}
|
||||||
await asyncio.sleep_ms(SEND_DELAY_MS)
|
await asyncio.sleep_ms(SEND_DELAY_MS)
|
||||||
messages_sent += 1
|
messages_sent += 1
|
||||||
|
|
||||||
|
|||||||
@@ -99,7 +99,13 @@ async def main(port=80):
|
|||||||
print("WS received raw:", data)
|
print("WS received raw:", data)
|
||||||
|
|
||||||
# Forward raw JSON payload over ESPNow to configured peers
|
# Forward raw JSON payload over ESPNow to configured peers
|
||||||
|
try:
|
||||||
await esp.send(data)
|
await esp.send(data)
|
||||||
|
except Exception:
|
||||||
|
try:
|
||||||
|
await ws.send(json.dumps({"error": "ESP-NOW send failed"}))
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
else:
|
else:
|
||||||
break
|
break
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
import network
|
||||||
|
|
||||||
import aioespnow
|
import aioespnow
|
||||||
|
|
||||||
|
|
||||||
@@ -20,11 +22,17 @@ class ESPNow:
|
|||||||
if getattr(self, "_initialized", False):
|
if getattr(self, "_initialized", False):
|
||||||
return
|
return
|
||||||
|
|
||||||
# Initialize ESPNow once (no disk persistence)
|
# ESP-NOW requires a WiFi interface to be active (STA or AP). Activate STA
|
||||||
|
# so ESP-NOW has an interface to use; we don't need to connect to an AP.
|
||||||
|
try:
|
||||||
|
sta = network.WLAN(network.STA_IF)
|
||||||
|
sta.active(True)
|
||||||
|
except Exception as e:
|
||||||
|
print("ESPNow: STA active failed:", e)
|
||||||
|
|
||||||
self._esp = aioespnow.AIOESPNow()
|
self._esp = aioespnow.AIOESPNow()
|
||||||
self._esp.active(True)
|
self._esp.active(True)
|
||||||
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self._esp.add_peer(b"\xff\xff\xff\xff\xff\xff")
|
self._esp.add_peer(b"\xff\xff\xff\xff\xff\xff")
|
||||||
except Exception:
|
except Exception:
|
||||||
@@ -56,6 +64,6 @@ class ESPNow:
|
|||||||
try:
|
try:
|
||||||
await self._esp.asend(b"\xff\xff\xff\xff\xff\xff", payload)
|
await self._esp.asend(b"\xff\xff\xff\xff\xff\xff", payload)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
# Log send failures but don't crash the app
|
|
||||||
print("ESPNow.send error:", e)
|
print("ESPNow.send error:", e)
|
||||||
|
raise
|
||||||
|
|
||||||
|
|||||||
@@ -25,6 +25,18 @@ const getEspnowSocket = () => {
|
|||||||
espnowPendingMessages = [];
|
espnowPendingMessages = [];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
espnowSocket.onmessage = (event) => {
|
||||||
|
try {
|
||||||
|
const data = JSON.parse(event.data);
|
||||||
|
if (data && data.error) {
|
||||||
|
console.error('ESP-NOW:', data.error);
|
||||||
|
alert('ESP-NOW send failed. ' + (data.error === 'ESP-NOW send failed' ? 'Check device WiFi/interface.' : data.error));
|
||||||
|
}
|
||||||
|
} catch (_) {
|
||||||
|
// Ignore non-JSON or non-error messages
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
espnowSocket.onclose = () => {
|
espnowSocket.onclose = () => {
|
||||||
espnowSocketReady = false;
|
espnowSocketReady = false;
|
||||||
espnowSocket = null;
|
espnowSocket = null;
|
||||||
|
|||||||
Reference in New Issue
Block a user