Files
2026-07-31 23:46:37 -07:00

76 lines
2.0 KiB
Python

"""MicroMesh example for the Seeed Studio XIAO RP2040.
Wiring (cross TX and RX):
XIAO D6 / GPIO0 / TX -> Meshtastic radio RX
XIAO D7 / GPIO1 / RX <- Meshtastic radio TX
XIAO GND --- Meshtastic radio GND
"""
from machine import Pin, UART
from time import sleep_ms, ticks_diff, ticks_ms
from micromesh import PortNum, SerialInterface
def on_packet(packet):
if packet.WhichOneof("payload_variant") != "decoded":
return
if packet.decoded.portnum == PortNum.TEXT_MESSAGE_APP:
try:
text = packet.decoded.payload.decode("utf-8")
except UnicodeError:
text = repr(packet.decoded.payload)
print("from !%08x: %s" % (packet.from_, text))
def on_receive(message):
variant = message.WhichOneof("payload_variant")
print("radio message:", variant or "unknown")
def on_error(error, payload):
print("skipping undecodable radio frame:", error)
print("frame bytes:", payload.hex())
print("MicroMesh starting on XIAO RP2040")
print("UART0: D6/GPIO0 TX -> radio RX")
print("UART0: D7/GPIO1 RX <- radio TX")
print("UART0: 115200 baud; common GND required")
uart = UART(
0,
baudrate=115200,
tx=Pin(0), # XIAO D6
rx=Pin(1), # XIAO D7
timeout=0,
rxbuf=1024,
)
mesh = SerialInterface(
uart,
on_receive=on_receive,
on_packet=on_packet,
on_log=lambda line: print("radio log:", line),
on_error=on_error,
)
mesh.connect()
print("configuration requested; id=%08x" % mesh.config_id)
hello_sent = False
last_status = ticks_ms()
while True:
mesh.poll()
if mesh.config_complete and not hello_sent:
print("radio configuration complete; sending greeting")
mesh.sendText("hello from XIAO RP2040")
hello_sent = True
now = ticks_ms()
if ticks_diff(now, last_status) >= 5000:
if mesh.config_complete:
print("connected; %d nodes known" % len(mesh.nodes))
else:
print("waiting for radio data; check TX/RX, GND, baud, and PROTO mode")
last_status = now
sleep_ms(10)