Wireless Broadcast
The mPython Board provides 2.4G wireless RF communication, with a total of 13 channels. It can achieve simple networked communication within a certain area. On the same channel, members can receive broadcast messages. It is similar to a walkie-talkie: on the same channel, communication is achieved.
Walkie-talkie
radio
Example of sending a broadcast message
1'''Broadcast'''
2'''Broadcast data'''
3import time
4import random
5from mpython import *
6from radio import *
7
8#MPythonESPNow(wifi_ch=0) wifi_ch: channel 0-11 integer
9espnow_0 = MPythonESPNow(wifi_ch=11)
10
11#MPythonESPNow.get_mac(mode=0) mode: 0 sta address / 1 ap address, hexadecimal string mac address
12print(str(espnow_0.get_mac(mode=0)), 0, 0, 1)
13
14
15print(espnow_0.get_mac(mode=0)) # sta
16print(espnow_0.get_mac(mode=1)) # ap
17
18while True:
19 msg = random.randint(0, 99999)
20 #MPythonESPNow.broadcast_data(msg='') msg: string data
21 espnow_0.broadcast_data(msg=msg)
22 print(str(msg))
23 time.sleep(0.5)
24
Example of sending data to a device with a specific mac address
1'''Broadcast'''
2'''Send data to a device with a specific mac address'''
3import time
4import random
5from mpython import *
6from radio import *
7
8espnow_0 = MPythonESPNow(0)
9
10#MPythonESPNow.set_add_peer(peer_mac='') peer_mac: mac address, hexadecimal string, registers the provided mac address as a peer address 7CDFA1F20AF4
11espnow_0.set_add_peer(peer_mac='7CDFA1F20ADC')
12
13print(espnow_0.get_mac(0))
14print(espnow_0.get_mac(1))
15
16while True:
17 msg = random.randint(0, 9999999)
18 print(str(msg), 0, 32, 1)
19 #MPythonESPNow.send_msg(peer_mac='',msg='') msg: string data
20 espnow_0.send_msg(peer_mac='7CDFA1F20ADC', msg=msg)
21 time.sleep(0.3)
22
Example of receiving a message
1'''Receive messages'''
2import time
3import random
4from mpython import *
5from radio import *
6
7def espnow_recv_callback(espnow_obj):
8 espnow_mac, espnow_data, espnow_rssi = espnow_obj.recv_msg() # Receive messages
9 print(espnow_data)
10 print(f"MAC: {espnow_mac}, RSSI: {espnow_rssi} dBm, Data: {espnow_data}")
11
12espnow_1 = MPythonESPNow(0)
13# MPythonESPNow.set_irq_callback(callback) callback: message receive callback function
14espnow_1.set_irq_callback(espnow_recv_callback)
15
16print(espnow_1.get_mac(0))
17print(espnow_1.get_mac(1))
- class MPythonESPNow(wifi_ch=0)
Instantiate Radio
- Parameters:
wifi_ch (int) – Set channel 0-11
- class MPythonESPNow
- class MPythonESPNow
- broadcast_data(msg)
Send wireless broadcast data, group send
- Parameters:
msg (str) – wireless broadcast data
- class MPythonESPNow
- set_add_peer(peer_mac)
Register the provided mac address as a peer address. Registration is required for private sending, not for group sending
- Parameters:
peer_mac (str) – mac address, hexadecimal string
- class MPythonESPNow
- class MPythonESPNow
- set_irq_callback(espnow_recv_callback)
Set the callback handler function for receiving messages
- Parameters:
espnow_recv_callback (function) – callback function