Compare commits
8 Commits
a97f6c7c2c
...
p2p
| Author | SHA1 | Date | |
|---|---|---|---|
| 3286c4002d | |||
| 68eb547ec4 | |||
| 8403df531d | |||
| 088fe161a8 | |||
| c9895df512 | |||
| 39a84696c3 | |||
| c7560b2e87 | |||
| ea21563900 |
8
bulk.sh
Executable file
8
bulk.sh
Executable file
@@ -0,0 +1,8 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
PORT="${1:-/dev/ttyACM0}"
|
||||
|
||||
while true; do
|
||||
ls "$PORT" && led-cli -p "$PORT" --erase --src --patterns && led-cli -p "$PORT" --reset -f
|
||||
sleep 0.5
|
||||
done
|
||||
@@ -68,7 +68,7 @@ def process_data(payload, settings, presets, controller_ip=None, save=False):
|
||||
set_groups = bool(data.get("set_groups"))
|
||||
groups = data.get("groups")
|
||||
if set_groups and isinstance(groups, list):
|
||||
dg.groups_replace(groups)
|
||||
dg.groups_replace(groups, settings)
|
||||
print("groups set", dg.list_groups())
|
||||
elif isinstance(groups, list) and groups:
|
||||
if not any(dg.in_group(str(g)) for g in groups):
|
||||
@@ -82,7 +82,7 @@ def process_data(payload, settings, presets, controller_ip=None, save=False):
|
||||
apply_presets(data, settings, presets)
|
||||
if "clear_presets" in data:
|
||||
apply_clear_presets(data, presets)
|
||||
elif "select" in data or "s" in data:
|
||||
if ("select" in data or "s" in data) and "presets" not in data:
|
||||
apply_select(data, settings, presets)
|
||||
if "default" in data:
|
||||
apply_default(data, settings, presets)
|
||||
@@ -96,6 +96,7 @@ def process_data(payload, settings, presets, controller_ip=None, save=False):
|
||||
settings.save()
|
||||
if "save" in data and "device_config" in data:
|
||||
settings.save()
|
||||
_flush_pending_select(settings, presets)
|
||||
|
||||
|
||||
_VALID_DEVICE_COLOR_ORDERS = frozenset({"rgb", "rbg", "grb", "gbr", "brg", "bgr"})
|
||||
@@ -196,6 +197,18 @@ def _run_select(presets, settings, preset_name, step=None):
|
||||
return False
|
||||
|
||||
|
||||
def _flush_pending_select(settings, presets):
|
||||
global _pending_select
|
||||
if _pending_select is None:
|
||||
return
|
||||
preset_name, step = _pending_select
|
||||
if preset_name not in presets.presets and preset_name not in ("on", "off"):
|
||||
return
|
||||
_pending_select = None
|
||||
if not _run_select(presets, settings, preset_name, step):
|
||||
print("select failed (pending):", preset_name)
|
||||
|
||||
|
||||
def apply_presets(data, settings, presets):
|
||||
global _pending_select
|
||||
presets_map = data["presets"]
|
||||
@@ -219,13 +232,10 @@ def apply_presets(data, settings, presets):
|
||||
pass
|
||||
presets.edit(id, preset_data)
|
||||
# Same message often carries select; apply now while presets are loaded.
|
||||
if "select" in data:
|
||||
if "select" in data or "s" in data:
|
||||
apply_select(data, settings, presets)
|
||||
elif _pending_select is not None:
|
||||
preset_name, step = _pending_select
|
||||
_pending_select = None
|
||||
if preset_name in presets.presets or preset_name in ("on", "off"):
|
||||
_run_select(presets, settings, preset_name, step)
|
||||
else:
|
||||
_flush_pending_select(settings, presets)
|
||||
|
||||
|
||||
def _select_list_for_this_device(select_val, settings):
|
||||
@@ -276,6 +286,11 @@ def apply_select(data, settings, presets):
|
||||
if not preset_name:
|
||||
return
|
||||
step = select_list[1] if len(select_list) > 1 else None
|
||||
if preset_name not in presets.presets and preset_name not in ("on", "off"):
|
||||
try:
|
||||
presets.load(settings)
|
||||
except Exception:
|
||||
pass
|
||||
if preset_name not in presets.presets and preset_name not in ("on", "off"):
|
||||
_pending_select = (preset_name, step)
|
||||
print("select deferred (preset not loaded yet):", preset_name)
|
||||
|
||||
@@ -1,11 +1,23 @@
|
||||
"""In-memory group membership for GROUP_CMD filtering."""
|
||||
"""Group membership for GROUP_CMD filtering; persisted in settings.json."""
|
||||
|
||||
_groups = []
|
||||
|
||||
|
||||
def groups_replace(group_ids):
|
||||
def load_from_settings(settings):
|
||||
global _groups
|
||||
g = settings.get("groups") if settings is not None else None
|
||||
if isinstance(g, list):
|
||||
_groups = [str(x) for x in g if str(x).strip()]
|
||||
else:
|
||||
_groups = []
|
||||
|
||||
|
||||
def groups_replace(group_ids, settings=None, *, persist=True):
|
||||
global _groups
|
||||
_groups = [str(g) for g in group_ids]
|
||||
if persist and settings is not None:
|
||||
settings["groups"] = list(_groups)
|
||||
settings.save()
|
||||
|
||||
|
||||
def in_group(group_id):
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
"""ESP-NOW receive loop and boot announce."""
|
||||
|
||||
import asyncio
|
||||
import urandom
|
||||
import ubinascii
|
||||
|
||||
import espnow
|
||||
import network
|
||||
@@ -12,38 +14,99 @@ from espnow_wire import (
|
||||
MSG_CMD,
|
||||
MSG_GROUP_CMD,
|
||||
MSG_GROUPS,
|
||||
MSG_PING_REQ,
|
||||
cmd_envelope,
|
||||
pack_announce,
|
||||
pack_ping_rsp,
|
||||
parse_group_cmd,
|
||||
parse_groups,
|
||||
parse_ping_req,
|
||||
wire_msg_type,
|
||||
)
|
||||
|
||||
from controller_messages import process_data
|
||||
from settings import WIFI_CHANNEL_DEFAULT
|
||||
|
||||
_PING_DELAY_MS_MIN = 50
|
||||
_PING_DELAY_MS_MAX = 500
|
||||
|
||||
_esp = None
|
||||
_groups_received = False
|
||||
_debug = False
|
||||
|
||||
|
||||
def _dlog(*parts):
|
||||
if _debug:
|
||||
print(*parts)
|
||||
|
||||
|
||||
def init_espnow(settings):
|
||||
global _esp
|
||||
ch = 6
|
||||
global _esp, _debug
|
||||
_debug = bool(settings.get("debug", False))
|
||||
try:
|
||||
ch = int(settings.get("wifi_channel", 1))
|
||||
ch = int(settings.get("wifi_channel", WIFI_CHANNEL_DEFAULT))
|
||||
except (TypeError, ValueError):
|
||||
pass
|
||||
ch = WIFI_CHANNEL_DEFAULT
|
||||
ch = max(1, min(11, ch))
|
||||
sta = network.WLAN(network.STA_IF)
|
||||
sta.active(True)
|
||||
sta.config(pm=network.WLAN.PM_NONE)
|
||||
try:
|
||||
sta.config(channel=ch)
|
||||
except Exception as e:
|
||||
print("espnow sta channel set failed:", e)
|
||||
_esp = espnow.ESPNow()
|
||||
_esp.active(True)
|
||||
try:
|
||||
_esp.add_peer(BROADCAST_MAC)
|
||||
_dlog("espnow add bcast ok")
|
||||
except Exception as e:
|
||||
print("espnow add bcast failed:", e)
|
||||
try:
|
||||
actual_ch = sta.config("channel")
|
||||
except Exception:
|
||||
pass
|
||||
actual_ch = "?"
|
||||
print("espnow init ch", ch, "sta_ch", actual_ch, "debug", _debug)
|
||||
return _esp
|
||||
|
||||
|
||||
def _send_ping_rsp(host, settings, ping_id, delay_ms):
|
||||
import utime
|
||||
|
||||
utime.sleep_ms(delay_ms)
|
||||
if _esp is None or not host or len(host) != 6:
|
||||
return
|
||||
pkt = pack_ping_rsp(ping_id, settings.get("name", "led"))
|
||||
try:
|
||||
try:
|
||||
_esp.add_peer(host)
|
||||
_dlog("espnow ping add_peer ok", ubinascii.hexlify(host).decode())
|
||||
except Exception as e:
|
||||
_dlog("espnow ping add_peer skip", repr(e))
|
||||
_esp.send(host, pkt)
|
||||
print("espnow ping rsp", ping_id, delay_ms, "ms", ubinascii.hexlify(host).decode())
|
||||
except Exception as e:
|
||||
print("espnow ping rsp failed:", e, "host", ubinascii.hexlify(host).decode(), "len", len(pkt))
|
||||
|
||||
|
||||
async def _send_ping_rsp_delayed(host, settings, ping_id):
|
||||
span = _PING_DELAY_MS_MAX - _PING_DELAY_MS_MIN
|
||||
delay_ms = _PING_DELAY_MS_MIN + (urandom.getrandbits(10) % (span + 1))
|
||||
await asyncio.sleep(delay_ms / 1000)
|
||||
_send_ping_rsp(host, settings, ping_id, delay_ms)
|
||||
|
||||
|
||||
def _schedule_ping_rsp(host, settings, ping_id):
|
||||
span = _PING_DELAY_MS_MAX - _PING_DELAY_MS_MIN
|
||||
delay_ms = _PING_DELAY_MS_MIN + (urandom.getrandbits(10) % (span + 1))
|
||||
try:
|
||||
import _thread
|
||||
|
||||
_thread.start_new_thread(_send_ping_rsp, (host, settings, ping_id, delay_ms))
|
||||
except ImportError:
|
||||
asyncio.create_task(_send_ping_rsp_delayed(host, settings, ping_id))
|
||||
|
||||
|
||||
def send_boot_announce(settings):
|
||||
if _esp is None:
|
||||
return
|
||||
@@ -61,13 +124,13 @@ def send_boot_announce(settings):
|
||||
print("espnow announce failed:", e)
|
||||
|
||||
|
||||
def _handle_packet(pkt, settings, presets):
|
||||
def _handle_packet(host, pkt, settings, presets):
|
||||
global _groups_received
|
||||
mt = wire_msg_type(pkt)
|
||||
if mt == MSG_GROUPS:
|
||||
ids = parse_groups(pkt)
|
||||
if ids is not None:
|
||||
dg.groups_replace(ids)
|
||||
dg.groups_replace(ids, settings)
|
||||
_groups_received = True
|
||||
print("groups", ids)
|
||||
return
|
||||
@@ -91,6 +154,11 @@ def _handle_packet(pkt, settings, presets):
|
||||
if env:
|
||||
process_data(env, settings, presets, save=save)
|
||||
return
|
||||
if mt == MSG_PING_REQ:
|
||||
ping_id = parse_ping_req(pkt)
|
||||
if ping_id is not None and host and len(host) == 6:
|
||||
_schedule_ping_rsp(host, settings, ping_id)
|
||||
return
|
||||
if mt == MSG_ANNOUNCE:
|
||||
return
|
||||
|
||||
@@ -112,7 +180,7 @@ async def espnow_receive_loop(settings, presets, wdt=None):
|
||||
wdt.feed()
|
||||
continue
|
||||
try:
|
||||
_handle_packet(msg, settings, presets)
|
||||
_handle_packet(host, msg, settings, presets)
|
||||
except Exception as e:
|
||||
print("espnow rx error:", e)
|
||||
if wdt:
|
||||
|
||||
@@ -9,6 +9,8 @@ MSG_ANNOUNCE = 0x01
|
||||
MSG_GROUPS = 0x02
|
||||
MSG_CMD = 0x03
|
||||
MSG_GROUP_CMD = 0x04
|
||||
MSG_PING_REQ = 0x05
|
||||
MSG_PING_RSP = 0x06
|
||||
|
||||
BROADCAST_MAC = b"\xff\xff\xff\xff\xff\xff"
|
||||
|
||||
@@ -104,6 +106,29 @@ def cmd_envelope(payload):
|
||||
return env[:need], save
|
||||
|
||||
|
||||
def pack_ping_req(ping_id):
|
||||
body = struct.pack("<I", int(ping_id) & 0xFFFFFFFF)
|
||||
return _pack_header(MSG_PING_REQ, body)
|
||||
|
||||
|
||||
def parse_ping_req(payload):
|
||||
if len(payload) >= 2 and payload[0] == WIRE_MAGIC:
|
||||
if payload[1] != MSG_PING_REQ:
|
||||
return None
|
||||
body = payload[2:]
|
||||
else:
|
||||
body = payload
|
||||
if len(body) < 4:
|
||||
return None
|
||||
return struct.unpack("<I", body[:4])[0]
|
||||
|
||||
|
||||
def pack_ping_rsp(ping_id, name):
|
||||
name_b = name.encode("utf-8")
|
||||
body = struct.pack("<I", int(ping_id) & 0xFFFFFFFF) + bytes([len(name_b)]) + name_b
|
||||
return _pack_header(MSG_PING_RSP, body)
|
||||
|
||||
|
||||
def wire_msg_type(payload):
|
||||
if len(payload) >= 2 and payload[0] == WIRE_MAGIC:
|
||||
return payload[1]
|
||||
|
||||
22
src/main.py
22
src/main.py
@@ -6,6 +6,7 @@ import gc
|
||||
import json
|
||||
import network
|
||||
import espnow
|
||||
import device_groups as dg
|
||||
from presets import Presets
|
||||
from controller_messages import apply_startup_pattern, process_data
|
||||
from espnow_transport import _handle_packet, init_espnow
|
||||
@@ -17,6 +18,7 @@ wdt.feed()
|
||||
machine.freq(160000000)
|
||||
|
||||
settings = Settings()
|
||||
dg.load_from_settings(settings)
|
||||
print(settings)
|
||||
gc.collect()
|
||||
|
||||
@@ -42,31 +44,27 @@ esp.send(BROADCAST_MAC, hello)
|
||||
print("espnow hello", len(hello), "B")
|
||||
|
||||
|
||||
def _on_espnow_message(msg):
|
||||
def _on_espnow_message(host, msg):
|
||||
if not msg:
|
||||
return
|
||||
if msg[0] == WIRE_MAGIC:
|
||||
_handle_packet(msg, settings, presets)
|
||||
_handle_packet(host, msg, settings, presets)
|
||||
return
|
||||
if msg[0:1] == b"{":
|
||||
process_data(msg, settings, presets)
|
||||
|
||||
|
||||
async def main():
|
||||
|
||||
while True:
|
||||
presets.tick()
|
||||
wdt.feed()
|
||||
if esp.any():
|
||||
while esp.any():
|
||||
host, msg = esp.recv(0)
|
||||
if host and msg:
|
||||
if not host or not msg:
|
||||
continue
|
||||
print(host, len(msg), "B")
|
||||
try:
|
||||
_on_espnow_message(msg)
|
||||
_on_espnow_message(host, msg)
|
||||
print(msg)
|
||||
except Exception as e:
|
||||
print("espnow rx error:", e)
|
||||
await asyncio.sleep(0)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
presets.tick()
|
||||
|
||||
@@ -201,15 +201,12 @@ class Presets:
|
||||
self.fill((0, 0, 0))
|
||||
self.selected = preset_name
|
||||
return True
|
||||
# Manual single-shot patterns: if this select arrives before the main loop has
|
||||
# tick()'d the previous frame, completing it first keeps step in sync with beats.
|
||||
# If re-selecting the same preset before the main loop has tick()'d the
|
||||
# previous frame, run one pending tick so step stays in sync.
|
||||
if (
|
||||
preset_name == self.selected
|
||||
and not preset.a
|
||||
and preset.p in ("chase", "pulse")
|
||||
and self.generator is not None
|
||||
):
|
||||
while self.generator is not None:
|
||||
self.tick()
|
||||
# Set step value if explicitly provided
|
||||
if step is not None:
|
||||
@@ -274,11 +271,22 @@ class Presets:
|
||||
colors = preset.c if preset.c else [(255, 255, 255)]
|
||||
bg_color = self.apply_brightness(preset.background_or(colors), preset.b)
|
||||
color_index = 0
|
||||
state = True
|
||||
last_update = utime.ticks_ms()
|
||||
delay_ms = max(1, int(preset.d))
|
||||
period = delay_ms * 2
|
||||
now = utime.ticks_ms()
|
||||
# Phase-lock to wall time so group identify (broadcast select) stays in sync even
|
||||
# when devices process the packet on different main-loop iterations.
|
||||
phase = now % period if period else 0
|
||||
state = phase < delay_ms
|
||||
last_update = utime.ticks_add(now, -phase)
|
||||
if state:
|
||||
base = colors[color_index % len(colors)]
|
||||
self.fill(self.apply_brightness(base, preset.b))
|
||||
color_index += 1
|
||||
else:
|
||||
self.fill(bg_color)
|
||||
while True:
|
||||
now = utime.ticks_ms()
|
||||
delay_ms = max(1, int(preset.d))
|
||||
if utime.ticks_diff(now, last_update) >= delay_ms:
|
||||
if state:
|
||||
base = colors[color_index % len(colors)]
|
||||
|
||||
@@ -3,6 +3,8 @@ import ubinascii
|
||||
import machine
|
||||
import network
|
||||
|
||||
WIFI_CHANNEL_DEFAULT = 5
|
||||
|
||||
class Settings(dict):
|
||||
SETTINGS_FILE = "/settings.json"
|
||||
|
||||
@@ -14,9 +16,9 @@ class Settings(dict):
|
||||
def set_defaults(self):
|
||||
|
||||
self["led_pin"] = 10
|
||||
self["num_leds"] = 119
|
||||
self["num_leds"] = 200
|
||||
|
||||
self["color_order"] = "rgb"
|
||||
self["color_order"] = "grb"
|
||||
|
||||
sta = network.WLAN(network.STA_IF)
|
||||
sta.active(True)
|
||||
@@ -32,7 +34,8 @@ class Settings(dict):
|
||||
self["startup_mode"] = "default"
|
||||
self["brightness"] = 32
|
||||
|
||||
self["wifi_channel"] = 1
|
||||
self["wifi_channel"] = WIFI_CHANNEL_DEFAULT
|
||||
self["groups"] = []
|
||||
|
||||
|
||||
def save(self):
|
||||
|
||||
116
tests/device_test_radio_diag.py
Normal file
116
tests/device_test_radio_diag.py
Normal file
@@ -0,0 +1,116 @@
|
||||
"""Device-side radio diagnostic test (MicroPython).
|
||||
|
||||
Checks:
|
||||
1) STA/AP bring-up on channel 5
|
||||
2) ESP-NOW init and broadcast peer add
|
||||
3) Broadcast TX test packet send
|
||||
4) RX wait window to see any incoming ESP-NOW frames
|
||||
"""
|
||||
|
||||
import espnow
|
||||
import machine
|
||||
import network
|
||||
import time
|
||||
import ubinascii
|
||||
|
||||
|
||||
CHANNEL = 5
|
||||
RX_WINDOW_MS = 3000
|
||||
WDT_TIMEOUT_MS = 10000
|
||||
BROADCAST_MAC = b"\xff\xff\xff\xff\xff\xff"
|
||||
TEST_PAYLOAD = b"\x4c\x05\x01\x00\x00\x00"
|
||||
|
||||
|
||||
def _mac_hex(mac_bytes):
|
||||
try:
|
||||
return ubinascii.hexlify(mac_bytes).decode()
|
||||
except Exception:
|
||||
return "?"
|
||||
|
||||
|
||||
def run_diag(channel=CHANNEL, rx_window_ms=RX_WINDOW_MS):
|
||||
wdt = machine.WDT(timeout=WDT_TIMEOUT_MS)
|
||||
wdt.feed()
|
||||
|
||||
print("diag start")
|
||||
print("cpu freq", machine.freq())
|
||||
|
||||
sta = network.WLAN(network.STA_IF)
|
||||
ap = network.WLAN(network.AP_IF)
|
||||
|
||||
# Clean start
|
||||
try:
|
||||
sta.active(False)
|
||||
ap.active(False)
|
||||
time.sleep_ms(100)
|
||||
except Exception as e:
|
||||
print("wifi reset failed", repr(e))
|
||||
|
||||
# STA setup
|
||||
try:
|
||||
sta.active(True)
|
||||
sta.config(pm=network.WLAN.PM_NONE)
|
||||
sta.config(channel=channel)
|
||||
print("sta ok ch", sta.config("channel"), "mac", _mac_hex(sta.config("mac")))
|
||||
except Exception as e:
|
||||
print("sta setup failed", repr(e))
|
||||
|
||||
# AP setup
|
||||
try:
|
||||
ap.active(True)
|
||||
try:
|
||||
ap.config(essid="diag-ap", channel=channel, hidden=True)
|
||||
except TypeError:
|
||||
ap.config(essid="diag-ap", channel=channel)
|
||||
print("ap ok ch", ap.config("channel"), "mac", _mac_hex(ap.config("mac")))
|
||||
except Exception as e:
|
||||
print("ap setup failed", repr(e))
|
||||
|
||||
wdt.feed()
|
||||
|
||||
# ESP-NOW setup
|
||||
try:
|
||||
e = espnow.ESPNow()
|
||||
e.active(True)
|
||||
print("espnow active ok")
|
||||
except Exception as e_err:
|
||||
print("espnow init failed", repr(e_err))
|
||||
return
|
||||
|
||||
# Add broadcast peer
|
||||
try:
|
||||
e.add_peer(BROADCAST_MAC, channel=channel)
|
||||
print("add bcast peer ok")
|
||||
except TypeError:
|
||||
try:
|
||||
e.add_peer(BROADCAST_MAC)
|
||||
print("add bcast peer ok (no channel arg)")
|
||||
except Exception as e_err:
|
||||
print("add bcast peer failed", repr(e_err))
|
||||
except Exception as e_err:
|
||||
print("add bcast peer failed", repr(e_err))
|
||||
|
||||
# TX test
|
||||
try:
|
||||
ok = e.send(BROADCAST_MAC, TEST_PAYLOAD, True)
|
||||
print("tx bcast", ok, "len", len(TEST_PAYLOAD))
|
||||
except Exception as e_err:
|
||||
print("tx bcast failed", repr(e_err))
|
||||
|
||||
# RX window
|
||||
print("rx window ms", rx_window_ms)
|
||||
t_end = time.ticks_add(time.ticks_ms(), rx_window_ms)
|
||||
rx_count = 0
|
||||
while time.ticks_diff(t_end, time.ticks_ms()) > 0:
|
||||
wdt.feed()
|
||||
host, msg = e.recv(100)
|
||||
if host:
|
||||
rx_count += 1
|
||||
print("rx", rx_count, _mac_hex(host), "len", len(msg))
|
||||
time.sleep_ms(5)
|
||||
|
||||
print("diag done rx_count", rx_count)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
run_diag()
|
||||
40
tests/device_test_recv_ch5.py
Normal file
40
tests/device_test_recv_ch5.py
Normal file
@@ -0,0 +1,40 @@
|
||||
"""Device test: receive ESP-NOW packets on channel 5 (MicroPython)."""
|
||||
|
||||
import espnow
|
||||
import machine
|
||||
import network
|
||||
import ubinascii
|
||||
import time
|
||||
|
||||
|
||||
CHANNEL = 5
|
||||
TIMEOUT_MS = 1000
|
||||
WDT_TIMEOUT_MS = 10000
|
||||
|
||||
|
||||
def _set_channel(channel):
|
||||
sta = network.WLAN(network.STA_IF)
|
||||
sta.active(True)
|
||||
sta.config(pm=network.WLAN.PM_NONE)
|
||||
sta.config(channel=channel)
|
||||
|
||||
|
||||
def recv_loop(channel=CHANNEL, timeout_ms=TIMEOUT_MS):
|
||||
wdt = machine.WDT(timeout=WDT_TIMEOUT_MS)
|
||||
_set_channel(channel)
|
||||
e = espnow.ESPNow()
|
||||
e.active(True)
|
||||
print("recv ready ch", channel)
|
||||
while True:
|
||||
wdt.feed()
|
||||
host, msg = e.recv(timeout_ms)
|
||||
if host:
|
||||
mac_hex = ubinascii.hexlify(host).decode()
|
||||
print("rx", mac_hex, "len", len(msg), "hex", ubinascii.hexlify(msg).decode())
|
||||
else:
|
||||
print("rx timeout")
|
||||
time.sleep_ms(10)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
recv_loop()
|
||||
47
tests/device_test_send_ch5.py
Normal file
47
tests/device_test_send_ch5.py
Normal file
@@ -0,0 +1,47 @@
|
||||
"""Device test: send one ESP-NOW packet on channel 5 (MicroPython)."""
|
||||
|
||||
import espnow
|
||||
import machine
|
||||
import network
|
||||
import ubinascii
|
||||
|
||||
|
||||
CHANNEL = 5
|
||||
DEST_HEX = "ffffffffffff"
|
||||
PAYLOAD_HEX = "4c0501000000"
|
||||
WDT_TIMEOUT_MS = 10000
|
||||
|
||||
|
||||
def _set_channel(channel):
|
||||
sta = network.WLAN(network.STA_IF)
|
||||
sta.active(True)
|
||||
sta.config(pm=network.WLAN.PM_NONE)
|
||||
sta.config(channel=channel)
|
||||
|
||||
|
||||
def _add_peer(esp, dest, channel):
|
||||
try:
|
||||
esp.add_peer(dest, channel=channel)
|
||||
except TypeError:
|
||||
esp.add_peer(dest)
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
|
||||
def send_once(dest_hex=DEST_HEX, payload_hex=PAYLOAD_HEX, channel=CHANNEL):
|
||||
wdt = machine.WDT(timeout=WDT_TIMEOUT_MS)
|
||||
wdt.feed()
|
||||
dest = ubinascii.unhexlify(dest_hex)
|
||||
pkt = ubinascii.unhexlify(payload_hex)
|
||||
_set_channel(channel)
|
||||
e = espnow.ESPNow()
|
||||
e.active(True)
|
||||
_add_peer(e, dest, channel)
|
||||
wdt.feed()
|
||||
ok = e.send(dest, pkt, True)
|
||||
print("sent", ok, "ch", channel, "dest", dest_hex, "len", len(pkt))
|
||||
return ok
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
send_once()
|
||||
Reference in New Issue
Block a user