radio — radio wireless broadcast functions

The radio module implements wireless broadcast functionality based on the ESP-NOW connectionless Wi-Fi communication protocol. It supports 13 channels. Within the same channel, it can receive broadcast messages sent by members, making it suitable for multi-board networking communication within a range of 30 meters.

Functions

radio.on()

Turn on the wireless function

radio.off()

Turn off the wireless function

radio.config(channel)

Configure wireless parameters

  • channel (int): The wireless channel, range 1~13

radio.receive()

Receive wireless broadcast messages, returned as a string. It can receive up to 250 bytes of data. If no message is received, it returns None. When the parameter of receive is True, i.e. receive(True), it returns a (msg, mac) tuple. The default is receive(False), i.e. it returns only msg.

radio.receive_bytes()

Receive wireless broadcast messages, returned as bytes. Everything else is the same as radio.receive().

radio.send()

Send a wireless broadcast message; the data type sent is a string. It returns True after a successful send, otherwise False.

radio.send_bytes()

Send a wireless broadcast message; the data type sent is bytes. It returns True after a successful send, otherwise False.

Wireless broadcast example
 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