MicroPython动手做(22)——掌控板之无线广播
1、掌控板无线广播主要函数radio 模块提供无线广播功能,支持13 Channel,在相同的 Channel 内能接收到成员发出的广播消息,适合10米范围内的多板组网的通讯。
radio.on()
描述: 开启无线功能
radio.off()
描述: 关闭无线功能
radio.config(channel)
描述: 配置无线参数
参数:
channel (int) - 无线通道,范围1~13
radio.receive()
描述: 接收无线广播消息,消息以字符串形式返回。最大可接收250字节数据。如果没有接收到消息,则返回 None 。当 receive 内参数为 True ,即 receive(True) ,返回 (msg,mac) 的二元组。默认缺省 receive(False) ,即只返回 msg
radio.receive_bytes()
描述: 接收无线广播消息,消息以字节形式返回。其他同 radio.receive() 相同
radio.send()
描述: 发送无线广播消息,发送数据类型为字符串。当发送成功后返回 True,否则返回 False
radio.send_bytes()
描述: 发送无线广播消息,发送数据类型为字节。当发送成功后返回 True,否则返回 False
5、自控行人过马路交通信号灯
#MicroPython动手做(22)——掌控板之无线广播
#自控行人过马路交通信号灯(行人控制端)
行人端红绿灯:
①行人方向的灯是红色的,等待行人按下A键,
②当行人需要过马路的时候,按下按钮,同时给行车方向的红绿灯发射信号,等待缓冲时间,红灯转绿灯。
③行人走完之后(绿灯亮一段时间),黄灯闪烁3次,再变为红灯。
#MicroPython动手做(22)——掌控板之无线广播
#自控行人过马路交通信号灯(行人控制端)
import radio
from mpython import *
import time
import framebuf
import font.digiface_44
def Flash():
global i, j, k, m, n, x
for m in range(1, 4):
rgb.fill((int(255), int(255), int(0)))
rgb.write()
time.sleep_ms(1)
time.sleep_ms(500)
rgb.fill( (0, 0, 0) )
rgb.write()
time.sleep_ms(1)
time.sleep_ms(500)
def _E4_BF_A1_E6_81_AF_E6_8F_90_E7_A4_BA():
global i, j, k, m, n, x
oled.fill(0)
oled.DispChar("行人如需过马路", 0, 16, 1)
oled.DispChar("请按下A键", 0, 32, 1)
oled.show()
def upRange(start, stop, step):
while start <= stop:
yield start
start += abs(step)
def downRange(start, stop, step):
while start >= stop:
yield start
start -= abs(step)
def _E6_97_B6_E9_97_B4_E6_8F_90_E7_A4_BA(x):
global i, j, k, m, n
for n in (1 <= int(x)) and upRange(1, int(x), 1) or downRange(1, int(x), 1):
oled.fill(0)
display_font(font.digiface_44, (str((x + 1) - n)), 32, 8, False, 2)
oled.show()
time.sleep_ms(1000)
def on_button_a_down(_):
global i, j, k, m, n, x
time.sleep_ms(10)
if button_a.value() == 1: return
radio.send("1")
_E6_97_B6_E9_97_B4_E6_8F_90_E7_A4_BA(10)
rgb.fill((int(51), int(255), int(51)))
rgb.write()
time.sleep_ms(1)
_E6_97_B6_E9_97_B4_E6_8F_90_E7_A4_BA(12)
Flash()
rgb.fill((int(255), int(0), int(0)))
rgb.write()
time.sleep_ms(1)
_E4_BF_A1_E6_81_AF_E6_8F_90_E7_A4_BA()
def display_font(_font, _str, _x, _y, _wrap, _z=0):
_start = _x
for _c in _str:
_d = _font.get_ch(_c)
if _wrap and _x > 128 - _d: _x = _start; _y += _d
if _c == '1' and _z > 0: oled.fill_rect(_x, _y, _d, _d, 0)
oled.blit(framebuf.FrameBuffer(bytearray(_d), _d, _d,
framebuf.MONO_HLSB), (_x+int(_d/_z)) if _c=='1' and _z>0 else _x, _y)
_x += _d
button_a.irq(trigger=Pin.IRQ_FALLING, handler=on_button_a_down)
radio.on()
radio.config(channel=11)
rgb.fill((int(255), int(0), int(0)))
rgb.write()
time.sleep_ms(1)
_E4_BF_A1_E6_81_AF_E6_8F_90_E7_A4_BA() #MicroPython动手做(22)——掌控板之无线广播
#自控行人过马路交通信号灯(行车同步端)
行车端红绿灯:
①亮绿灯,等待接收广播指令;
②当接收到广播指令,等待缓冲时间,黄灯闪亮3次,红灯亮,等待15秒,行人通过后,恢复绿灯。
#MicroPython动手做(22)——掌控板之无线广播
#自控行人过马路交通信号灯(行车同步端)
from mpython import *
from machine import Timer
import radio
import ubinascii
_radio_msg_list = []
def radio_callback(_msg):
global _radio_msg_list
try: radio_recv(_msg)
except: pass
if _msg in _radio_msg_list:
eval('radio_recv_' + bytes.decode(ubinascii.hexlify(_msg)) + '()')
tim13 = Timer(13)
def timer13_tick(_):
_msg = radio.receive()
if not _msg: return
radio_callback(_msg)
tim13.init(period=20, mode=Timer.PERIODIC, callback=timer13_tick)
import time
import framebuf
import font.digiface_44
def upRange(start, stop, step):
while start <= stop:
yield start
start += abs(step)
def downRange(start, stop, step):
while start >= stop:
yield start
start -= abs(step)
def _E6_97_B6_E9_97_B4_E6_8F_90_E7_A4_BA(x):
global k, j
for k in (1 <= int(x)) and upRange(1, int(x), 1) or downRange(1, int(x), 1):
oled.fill(0)
display_font(font.digiface_44, (str((x + 1) - k)), 32, 8, False, 2)
oled.show()
time.sleep(1)
oled.fill(0)
oled.show()
def flash():
global x, k, j
for j in range(1, 4):
rgb.fill((int(255), int(255), int(0)))
rgb.write()
time.sleep_ms(1)
time.sleep_ms(500)
rgb.fill( (0, 0, 0) )
rgb.write()
time.sleep_ms(1)
time.sleep_ms(500)
_radio_msg_list.append('1')
def radio_recv_31():
global i, x, y
_E6_97_B6_E9_97_B4_E6_8F_90_E7_A4_BA(7)
flash()
rgb.fill((int(255), int(0), int(0)))
rgb.write()
time.sleep_ms(1)
_E6_97_B6_E9_97_B4_E6_8F_90_E7_A4_BA(15)
rgb.fill((int(0), int(153), int(0)))
rgb.write()
time.sleep_ms(1)
def display_font(_font, _str, _x, _y, _wrap, _z=0):
_start = _x
for _c in _str:
_d = _font.get_ch(_c)
if _wrap and _x > 128 - _d: _x = _start; _y += _d
if _c == '1' and _z > 0: oled.fill_rect(_x, _y, _d, _d, 0)
oled.blit(framebuf.FrameBuffer(bytearray(_d), _d, _d,
framebuf.MONO_HLSB), (_x+int(_d/_z)) if _c=='1' and _z>0 else _x, _y)
_x += _d
radio.on()
radio.config(channel=11)
rgb.fill((int(255), int(0), int(0)))
rgb.write()
time.sleep_ms(1) #MicroPython动手做(22)——掌控板之无线广播
#A键打开绿灯,B键打开红灯——接收端
#MicroPython动手做(22)——掌控板之无线广播
#A键打开绿灯,B键打开红灯——接收端
import radio
from mpython import *
import time
import music
while True:
radio.on()
radio.config(channel=6)
oled.fill(0)
oled.show()
rgb.fill( (0, 0, 0) )
rgb.write()
time.sleep_ms(1)
if radio.receive() == "on":
rgb.fill((int(0), int(102), int(0)))
rgb.write()
time.sleep_ms(1)
music.pitch(440, 500)
oled.fill(0)
oled.DispChar("on 绿灯", 40, 22, 1)
oled.show()
time.sleep(1)
if radio.receive() == "off":
rgb.fill((int(102), int(0), int(0)))
rgb.write()
time.sleep_ms(1)
music.pitch(294, 500)
oled.fill(0)
oled.DispChar("off 红灯", 40, 22, 1)
oled.show()
time.sleep(1)
2、无线广播的简单实验(需要二块掌控板)
#MicroPython动手做(22)——掌控板之无线广播
#无线广播发射端程序
#MicroPython动手做(22)——掌控板之无线广播
#无线广播发射端程序
from mpython import *
import time
import radio
oled.fill(0)
oled.DispChar(' 13频道广播哦', 0, 16, 1)
oled.show()
rgb = (int(51), int(204), int(0))
rgb.write()
time.sleep_ms(1)
while True:
radio.on()
radio.config(channel=13)
radio.send('msg') #MicroPython动手做(22)——掌控板之无线广播
#无线广播接收端程序
#MicroPython动手做(22)——掌控板之无线广播
#无线广播接收端程序
import radio
from mpython import *
import time
from machine import Timer
import ubinascii
def radio_recv(_msg):
rgb = (int(255), int(0), int(0))
rgb.write()
time.sleep_ms(1)
oled.fill(0)
oled.DispChar(" 收到13频道广播", 0, 16, 1)
oled.show()
_radio_msg_list = []
def radio_callback(_msg):
global _radio_msg_list
try: radio_recv(_msg)
except: pass
if _msg in _radio_msg_list:
eval('radio_recv_' + bytes.decode(ubinascii.hexlify(_msg)) + '()')
tim13 = Timer(13)
def timer13_tick(_):
_msg = radio.receive()
if not _msg: return
radio_callback(_msg)
tim13.init(period=20, mode=Timer.PERIODIC, callback=timer13_tick)
while True:
radio.on()
radio.config(channel=13)
oled.fill(0)
oled.DispChar(" 接收状态", 0, 16, 1)
oled.show()
rgb.fill( (0, 0, 0) )
rgb.write()
time.sleep_ms(1)
无线广播发射端程序(mPython 图形编程)
无线广播接收端程序(mPthon X 图形编程)
3、AB键组网控制RGB灯
#MicroPython动手做(22)——掌控板之无线广播
# AB键组网控制RGB灯——控制端
from mpython import *
import time
import radio
rgb = (int(0), int(102), int(0))
rgb.write()
time.sleep_ms(1)
while True:
radio.on()
radio.config(channel=6)
if button_a.value() == 0:
radio.send('on')
if button_b.value() == 0:
radio.send('off') 掌控板之无线广播工作时(发射)的串口信息如下
无线广播发射端程序(mPython 图形编程)
#MicroPython动手做(22)——掌控板之无线广播
# AB键组网控制RGB灯——接收端
#MicroPython动手做(22)——掌控板之无线广播
# AB键组网控制RGB灯——接收端
import radio
from mpython import *
import time
while True:
radio.on()
radio.config(channel=6)
rgb.fill( (0, 0, 0) )
rgb.write()
time.sleep_ms(1)
if radio.receive():
rgb.fill((int(102), int(0), int(0)))
rgb.write()
time.sleep_ms(1)
else:
rgb = (int(0), int(102), int(0))
rgb.write()
time.sleep_ms(1)
无线广播接收端程序(mPthon X 图形编程)
4、A键打开绿灯,B键打开红灯
#MicroPython动手做(22)——掌控板之无线广播
#A键打开绿灯,B键打开红灯——控制端
from mpython import *
import time
import radio
rgb = (int(0), int(102), int(0))
rgb.write()
time.sleep_ms(1)
while True:
radio.on()
radio.config(channel=6)
if button_a.value() == 0:
radio.send('on')
if button_b.value() == 0:
radio.send('off') 无线广播发射端程序(mPython 图形编程)
无线广播接收端程序(mPthon X 图形编程)
视频:A键打开绿灯,B键打开红灯
https://v.youku.com/v_show/id_XNDY1NzMxODYyNA==.html?spm=a2h0c.8166622.PhoneSokuUgc_1.dscreenshot
https://v.youku.com/v_show/id_XNDY1NzMxODYyNA==.html?spm=a2h0c.8166622.PhoneSokuUgc_1.dscreenshot
行人控制端(mPthon X 图形编程)
行车同步端(mPython 图形编程)
页:
[1]
2