4. Radio Broadcast¶
The mPython Board provides 13 channeks of 2.4G wireless RF communication. Can realize simple networking communication in a certain area. Under the same channel, members can receive broadcast messages. It’s similar, like walkie talkie. Under the same channel, realize the call.
4.1. radio¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | import radio
import _thread
channel=2
radio.on()
radio.config(channel=channel) # radio通道设置
def rec_loop(): # radio接收循环
while True:
temp=radio.receive(True) # radio 接收数据,返回(msg,mac)
# temp=radio.receive() # radio 接收数据,返回msg
if temp: # 当接收到数据时打印
print(temp)
_thread.start_new_thread(rec_loop, ()) # radio接收线程
radio.send("hello mPython!")
|
First of all we need import radio
to import the wireless module. Then radio.on()
, turn on the wireless function. Configure wireless channel radio.config(channel)
, channel parameter can set 1 ~ 13 channels.
Use radio.send()
to send a broadcast message, the message type is a string. On the receiving end, on the same channel, use radio.receive()
to receive broadcast data. receive(True)
The return data type is (msg, mac).
mac is the MAC address of the network device, and the addresses are unique. For example, if you want to be a unicast application, you can filter messages sent by other MAC devices. By default, receive()
, the returned data type is msg, without MAC address.
4.2. Telegraph¶
Based on the above radio learning, we can use the mPython Board to make an unique telegraph! The two mPython Board are spread by radio and Morse code, is there a sense of spy war film? Try it now!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | import radio # 导入radio
from mpython import * # 导入mpython
import music # 导入music
CH = 1 # channel变量
radio.on()
radio.config(channel=CH) # radio通道设置
btna_stat, btnb_stat, touch_stat = [0] * 3 # 按键状态标志
def set_channel(): # radio 通道设置函数
global CH, btna_stat, btnb_stat
if button_a.value() == 0 and btna_stat == 0: # a按键,减通道
CH -= 1
if CH < 1:
CH = 13
radio.config(channel=CH) # radio通道设置
oled.DispChar("Channel: %02d" % CH, 25, 5) # 通道显示
oled.show()
btna_stat = 1
elif button_a.value() == 1:
btna_stat = 0
if button_b.value() == 0 and btnb_stat == 0: # b按键,加通道
CH += 1
if CH > 13:
CH = 1
radio.config(channel=CH) # radio通道设置
oled.DispChar("Channel: %02d" % CH, 25, 5) # 通道显示
oled.show()
btnb_stat = 1
elif button_b.value() == 1:
btnb_stat = 0
def ding(): # 叮叮响
global touch_stat
if touchPad_T.read() < 300 and touch_stat == 0: # 检测按键按下时,发出‘ding’响,并广播
music.pitch(500, 100, wait=False) # 播放"ding"
radio.send('ding') # radio 广播 "ding"
touch_stat = 1
elif touchPad_T.read() >= 300:
touch_stat = 0
oled.DispChar("Channel: %d" % CH, 25, 5) # 开机显示
oled.DispChar("电报机:触摸T", 25, 25)
oled.show()
while True:
set_channel() # 设置通道函数
ding() # 叮叮响函数
temp = radio.receive() # radio接收广播
if temp == 'ding': # 当接收到"ding"广播,发出叮响
rgb.fill((0, 10, 0)) # 指示灯
rgb.write()
music.pitch(500, 100, wait=False)
sleep_ms(50)
rgb.fill((0, 0, 0))
rgb.write()
|
The above telegraph example, A B button to select the wireless channel, touch T, send telegram. When receiving the telegram, the RGB of the mPython Board will have an indication.