diff --git a/scripts/mpremote_send_ch5.sh b/scripts/mpremote_send_ch5.sh new file mode 100755 index 0000000..f67a528 --- /dev/null +++ b/scripts/mpremote_send_ch5.sh @@ -0,0 +1,24 @@ +#!/usr/bin/env bash +set -euo pipefail + +# Upload and run a device-side ESP-NOW sender script. +# Default channel is 5 and default destination is broadcast. +# +# Usage: +# scripts/mpremote_send_ch5.sh [port] [dest_mac_hex] [payload_hex] +# +# Examples: +# scripts/mpremote_send_ch5.sh /dev/ttyACM0 +# scripts/mpremote_send_ch5.sh /dev/ttyACM0 ffffffffffff 4c0501000000 + +PORT="${1:-/dev/ttyACM0}" +DEST_HEX="${2:-ffffffffffff}" +PAYLOAD_HEX="${3:-4c0501000000}" +CHANNEL=5 +DEVICE_SCRIPT="send_ch5.py" + +mpremote connect "${PORT}" fs cp "scripts/mpremote_send_ch5_device.py" ":${DEVICE_SCRIPT}" +mpremote connect "${PORT}" exec " +import ${DEVICE_SCRIPT%.*} +${DEVICE_SCRIPT%.*}.send_once('${DEST_HEX}', '${PAYLOAD_HEX}', ${CHANNEL}) +" diff --git a/scripts/mpremote_send_ch5_device.py b/scripts/mpremote_send_ch5_device.py new file mode 100644 index 0000000..8d27502 --- /dev/null +++ b/scripts/mpremote_send_ch5_device.py @@ -0,0 +1,42 @@ +"""Device-side ESP-NOW sender (MicroPython, channel 5).""" + +import espnow +import network +import ubinascii + + +CHANNEL = 5 +DEST_HEX = "ffffffffffff" +PAYLOAD_HEX = "4c0501000000" + + +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): + dest = ubinascii.unhexlify(dest_hex) + pkt = ubinascii.unhexlify(payload_hex) + _set_channel(channel) + e = espnow.ESPNow() + e.active(True) + _add_peer(e, dest, channel) + ok = e.send(dest, pkt, True) + print("sent", ok, "ch", channel, "dest", dest_hex, "len", len(pkt)) + return ok + + +if __name__ == "__main__": + send_once()