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.
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