Pi port: serial transport, addressed ESP-NOW bridge, port 80
- Run app on Raspberry Pi: serial to ESP32 bridge at 912000 baud, /dev/ttyS0 - Remove ESP-NOW/MicroPython-only code from src (espnow, p2p, wifi, machine/Pin) - Transport: always send 6-byte MAC + payload; optional to/destination_mac in API and WebSocket - Settings and model DB use project paths (no root); fix sys.print_exception for CPython - Preset/settings controllers use get_current_sender(); template paths for cwd=src - Pipfile: run from src, PORT from env; scripts for port 80 (setcap) and test - ESP32 bridge: receive 6-byte addr + payload, LRU peer management (20 max), handle ESP_ERR_ESPNOW_EXIST - Add esp32/main.py, esp32/benchmark_peers.py, scripts/setup-port80.sh, scripts/test-port80.sh Made-with: Cursor
This commit is contained in:
1 parent
0fdc11c0b0
commit
ac9fca8d4b
19 files changed
+656
-500
No files matched your search
+16
-16
@@ -2,7 +2,7 @@ from microdot import Microdot
|
||||
from microdot.session import with_session
|
||||
from models.preset import Preset
|
||||
from models.profile import Profile
|
||||
from models.espnow import ESPNow
|
||||
from models.transport import get_current_sender
|
||||
from util.espnow_message import build_message, build_preset_dict
|
||||
import asyncio
|
||||
import json
|
||||
@@ -110,16 +110,13 @@ async def delete_preset(request, id, session):
|
||||
@with_session
|
||||
async def send_presets(request, session):
|
||||
"""
|
||||
Send one or more presets over ESPNow.
|
||||
Send one or more presets to the LED driver (via serial transport).
|
||||
|
||||
Body JSON:
|
||||
{"preset_ids": ["1", "2", ...]} or {"ids": ["1", "2", ...]}
|
||||
|
||||
The controller:
|
||||
- looks up each preset in the Preset model
|
||||
- converts them to API-compliant format
|
||||
- splits into <= 240-byte ESPNow messages
|
||||
- sends each message to all configured ESPNow peers.
|
||||
The controller looks up each preset, converts to API format, chunks into
|
||||
<= 240-byte messages, and sends them over the configured transport.
|
||||
"""
|
||||
try:
|
||||
data = request.json or {}
|
||||
@@ -132,6 +129,8 @@ async def send_presets(request, session):
|
||||
save_flag = data.get('save', True)
|
||||
save_flag = bool(save_flag)
|
||||
default_id = data.get('default')
|
||||
# Optional 12-char hex MAC to send to one device; omit for default (e.g. broadcast).
|
||||
destination_mac = data.get('destination_mac') or data.get('to')
|
||||
|
||||
# Build API-compliant preset map keyed by preset ID, include name
|
||||
current_profile_id = get_current_profile_id(session)
|
||||
@@ -153,16 +152,17 @@ async def send_presets(request, session):
|
||||
if default_id is not None and str(default_id) not in presets_by_name:
|
||||
default_id = None
|
||||
|
||||
# Use shared ESPNow singleton
|
||||
esp = ESPNow()
|
||||
sender = get_current_sender()
|
||||
if not sender:
|
||||
return json.dumps({"error": "Transport not configured"}), 503, {'Content-Type': 'application/json'}
|
||||
|
||||
async def send_chunk(chunk_presets):
|
||||
# Include save flag so the led-driver can persist when desired.
|
||||
msg = build_message(presets=chunk_presets, save=save_flag, default=default_id)
|
||||
await esp.send(msg)
|
||||
await sender.send(msg, addr=destination_mac)
|
||||
|
||||
MAX_BYTES = 240
|
||||
SEND_DELAY_MS = 100
|
||||
send_delay_s = 0.1
|
||||
entries = list(presets_by_name.items())
|
||||
total_presets = len(entries)
|
||||
messages_sent = 0
|
||||
@@ -182,8 +182,8 @@ async def send_presets(request, session):
|
||||
try:
|
||||
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)
|
||||
return json.dumps({"error": "Send failed"}), 503, {'Content-Type': 'application/json'}
|
||||
await asyncio.sleep(send_delay_s)
|
||||
messages_sent += 1
|
||||
batch = {name: preset_obj}
|
||||
last_msg = build_message(presets=batch, save=save_flag, default=default_id)
|
||||
@@ -192,12 +192,12 @@ async def send_presets(request, session):
|
||||
try:
|
||||
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)
|
||||
return json.dumps({"error": "Send failed"}), 503, {'Content-Type': 'application/json'}
|
||||
await asyncio.sleep(send_delay_s)
|
||||
messages_sent += 1
|
||||
|
||||
return json.dumps({
|
||||
"message": "Presets sent via ESPNow",
|
||||
"message": "Presets sent",
|
||||
"presets_sent": total_presets,
|
||||
"messages_sent": messages_sent
|
||||
}), 200, {'Content-Type': 'application/json'}
|
||||
|
||||
+10
-16
@@ -1,6 +1,5 @@
|
||||
from microdot import Microdot, send_file
|
||||
from settings import Settings
|
||||
import util.wifi as wifi
|
||||
import json
|
||||
|
||||
controller = Microdot()
|
||||
@@ -15,19 +14,18 @@ async def get_settings(request):
|
||||
|
||||
@controller.get('/wifi/ap')
|
||||
async def get_ap_config(request):
|
||||
"""Get Access Point configuration."""
|
||||
config = wifi.get_ap_config()
|
||||
if config:
|
||||
# Also get saved settings
|
||||
config['saved_ssid'] = settings.get('wifi_ap_ssid')
|
||||
config['saved_password'] = settings.get('wifi_ap_password')
|
||||
config['saved_channel'] = settings.get('wifi_ap_channel')
|
||||
return json.dumps(config), 200, {'Content-Type': 'application/json'}
|
||||
return json.dumps({"error": "Failed to get AP config"}), 500
|
||||
"""Get saved AP configuration (Pi: no in-device AP)."""
|
||||
config = {
|
||||
'saved_ssid': settings.get('wifi_ap_ssid'),
|
||||
'saved_password': settings.get('wifi_ap_password'),
|
||||
'saved_channel': settings.get('wifi_ap_channel'),
|
||||
'active': False,
|
||||
}
|
||||
return json.dumps(config), 200, {'Content-Type': 'application/json'}
|
||||
|
||||
@controller.post('/wifi/ap')
|
||||
async def configure_ap(request):
|
||||
"""Configure Access Point."""
|
||||
"""Save AP configuration to settings (Pi: no in-device AP)."""
|
||||
try:
|
||||
data = request.json
|
||||
ssid = data.get('ssid')
|
||||
@@ -43,18 +41,14 @@ async def configure_ap(request):
|
||||
if channel < 1 or channel > 11:
|
||||
return json.dumps({"error": "Channel must be between 1 and 11"}), 400
|
||||
|
||||
# Save to settings
|
||||
settings['wifi_ap_ssid'] = ssid
|
||||
settings['wifi_ap_password'] = password
|
||||
if channel is not None:
|
||||
settings['wifi_ap_channel'] = channel
|
||||
settings.save()
|
||||
|
||||
# Configure AP
|
||||
wifi.ap(ssid, password, channel)
|
||||
|
||||
return json.dumps({
|
||||
"message": "AP configured successfully",
|
||||
"message": "AP settings saved",
|
||||
"ssid": ssid,
|
||||
"channel": channel
|
||||
}), 200, {'Content-Type': 'application/json'}
|
||||
|
||||
Reference in new issue
Block a user