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.

../../../../../_images/radio1.png

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-14 integer
 9espnow_0 = MPythonESPNow(wifi_ch=0) 
10
11#MPythonESPNow.get_mac(mode=0) mode: 0 STA address / 1 AP address, hex string MAC address
12oled.fill(0)
13oled.DispChar(str(espnow_0.get_mac(mode=0)), 0, 0, 1)
14oled.show()
15
16print(espnow_0.get_mac(mode=0)) # sta 
17print(espnow_0.get_mac(mode=1)) # ap
18
19while True:
20    msg = random.randint(0, 99999)
21    #MPythonESPNow.broadcast_data(msg='') msg: string data
22    espnow_0.broadcast_data(msg=msg) 
23    print(str(msg))
24    time.sleep(0.5)
25
Example of sending data to a device with a specific mac address
 1'''Broadcast'''
 2'''Send data to device with 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 hex string, registers the provided MAC address as peer 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 message'''
 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 message
 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 Receive messageCallback 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 the channel 0-14

class MPythonESPNow
get_mac(mode)

Get the mac address of its own network card

Parameters:

mode (int) – 0 sta address / 1 ap address

Returns:

hexadecimal string mac address

Return type:

str

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
send_msg(peer_mac, msg)

Send wireless broadcast data, private send

Parameters:
  • peer_mac (str) – mac address, hexadecimal string

  • msg (str) – wireless broadcast data

class MPythonESPNow
set_irq_callback(espnow_recv_callback)

Set the callback handler function for receiving messages

Parameters:

espnow_recv_callback (function) – callback function

espnow_recv_callback(espnow_obj)

espnow_mac, espnow_data, espnow_rssi = espnow_obj.recv_msg() receive message object

Variables:
  • espnow_mac (str) – mac address of the device broadcasting the message

  • espnow_data (str) – received message

  • espnow_rssi (str) – broadcast signal strength dBm