【FireBeetle 2 ESP32 C6】espnow 测试2
<div class='showpostmsg'><p>将程序修改一下,按下按钮后发送信号,接收到信号时闪LED。</p><p> </p>
<p>对于不同的板子,修改LED和SW对应引脚即可。</p>
<p> </p>
<pre>
<code class="language-python">import network
import espnow
import time
from machine import Pin
import os
r = os.uname()
if 'ESP32C6' in r.machine:
LED= Pin(15, Pin.OUT)
SW = Pin(9, Pin.IN)
else:
LED= Pin(12, Pin.OUT)
SW = Pin(9, Pin.IN)
# WLAN接口必须处于活动状态才能 send()/recv()
sta = network.WLAN(network.STA_IF) # 或 network.AP_IF
sta.active(True)
sta.disconnect() # 对于 ESP8266
e = espnow.ESPNow()
e.active(True)
peer = b'\xbb\xbb\xbb\xbb\xbb\xbb' # 配对设备wifi接口的MAC地址
e.add_peer(peer) # 发送前必须 add_peer()
def isr_sw(t):
e.send(peer, str(time.ticks_ms()))
def erecv():
while True:
host, msg = e.recv()
if msg:
LED(not LED())
print(host, msg)
SW.irq(trigger=Pin.IRQ_FALLING, handler=isr_sw)
erecv()
</code></pre>
<p> </p>
</div><script> var loginstr = '<div class="locked">查看本帖全部内容,请<a href="javascript:;" style="color:#e60000" class="loginf">登录</a>或者<a href="https://bbs.eeworld.com.cn/member.php?mod=register_eeworld.php&action=wechat" style="color:#e60000" target="_blank">注册</a></div>';
if(parseInt(discuz_uid)==0){
(function($){
var postHeight = getTextHeight(400);
$(".showpostmsg").html($(".showpostmsg").html());
$(".showpostmsg").after(loginstr);
$(".showpostmsg").css({height:postHeight,overflow:"hidden"});
})(jQuery);
} </script><script type="text/javascript">(function(d,c){var a=d.createElement("script"),m=d.getElementsByTagName("script"),eewurl="//counter.eeworld.com.cn/pv/count/";a.src=eewurl+c;m.parentNode.insertBefore(a,m)})(document,523)</script>
页:
[1]