import time
from radio import *
r = Radio()
print('channel', r.channel)
r.channel = 12
while True:
time.sleep(1)
a = r.read()
if a:
print('get', a)
from radio import *
def onmsg(msg, mac):
print('get', msg, 'from', mac)
r = Radio(onmsg)
print('channel', r.channel)
r.channel = 12
r.send("hello world")
# 1. 啟動WiFi
import network
w0 = network.WLAN(network.STA_IF)
w0.active(True)
# 2. 取得自機的MAC地址
mac = w0.config('mac')
>>> mac
b'\xc4O3"\xdb\x89'
# 3. 導入espnow
from esp import espnow
e = espnow.ESPNow()
e.init()
# 4. 加入對方的MAC地址
# 注意: 不是自己的MAC地址
e.add_peer(b'\x8c\xaa\xb5\xb9\xf8\xf0')
# 5. 信息觸發
def rxcb(mac, msg):
print("Recv:", mac, msg)
e.on_recv(rxcb)
# 6. 發佈信息
e.send(b'\x8c\xaa\xb5\xb9\xf8\xf0', 'hello world')
# 在接收端可以看到
# Recv: b'\xc4O3"\xdb\x89' b'hello world'