Petoi Group Control Solution
ESP-NOW is another wireless communication protocol developed by Espressif that enables multiple devices to communicate without or using Wi-Fi. This protocol is similar to the low-power 2.4GHz wireless connection commonly found in wireless mice—the devices are paired before they can communicate. After pairing, the connection between the devices is continuous, peer-to-peer, and does not require a handshake protocol. It is a fast communication technology with short data transmission and no connection, which allows low-power controllers to directly control all smart devices without connecting to a router. It is suitable for scenarios such as smart lights, remote control, and sensor data return.
After using ESP-NOW communication, if a certain device suddenly loses power, as long as it restarts, it will automatically connect to the corresponding node to resume communication.
The communication modes supported by ESP-NOW are as follows:
one-to-one communication
one-to-many communication
many-to-one communication
many-to-many communication
ESP-NOW supports the following features:
Unicast packet encryption or unicast packet unencrypted communication;
Mixed use of encrypted paired devices and non-encrypted paired devices;
Can carry payload data up to 250 bytes;
Support setting sending callback function to notify the application layer of frame sending failure or success.
At the same time, ESP-NOW also has some limitations:
Broadcast packets are not supported temporarily;
There are restrictions on encrypted paired devices
In Station mode,a maximum of 10 encrypted paired devices are supported;
In SoftAP or SoftAP + Station mixed mode, a maximum of 6 encrypted paired devices are supported;
The number of non-encrypted paired devices is supported, and the total number of encrypted devices does not exceed 20;
Valid payloads are limited to 250 bytes.
Petoi group control can use the ESP-NOW communication function of the ESP8266.
In this case, two Bittles (equipped with ESP8266) and one computer connected with ESP8266 are prepared.
See below for program uploading and MAC address acquisition of the module in the figure.
Install Thonny on the computer to facilitate the debugging of MicroPython of the ESP8266 module. When using the ESP-NOW protocol, special MicroPython firmware is required (see Github). Because the normal version of the 8266-MicroPython firmware will prompt that the library cannot be found.
Open Thonny and use the USB uploader to connect the ESP8266 module, enter in the shelll interface:
If there is an error prompt such as "cannot find espnow module", it means that there is a problem with the firmware uploading; if there is no prompt, it means that the firmware uploading is successful.
If after burning the ESP-NOW firmware, the symbolic >>>
symbol of Python does not appear in the shell interface, it means that the firmware burning failed. You can try to use the Flash burning tool NodeMCU-PyFlasher.exe, and the burning configuration is shown in the figure below :
The group control code is divided into 3 parts:
Query the MAC address of the module
Transmitter program
receiver program
The MAC address is an address used to confirm the location of a network device, and is responsible for the second layer (data link layer) of the OSI network model. The MAC address is also called the physical address and the hardware address. It is burned into the non-volatile memory (such as EEPROM) of the network card when it is produced by the network equipment manufacturer.
The length of the MAC address is 48 bits (6 bytes), usually expressed as 12 hexadecimal numbers. The first 3 bytes represent the serial number of the network hardware manufacturer, which is assigned by IEEE (Institute of Electrical and Electronics Engineers), and the last 3 bytes represent the serial number of a certain network product (such as a network card) manufactured by the manufacturer. As long as you don't change your MAC address, the MAC address is unique in the world. Visually speaking, the MAC address is like the ID number on the ID card, which is unique.
The easiest way to use ESPNOW is to send it by MAC address. We use a small program to query the MAC address of the module.
After running in Thonny, print out the MAC address in the terminal. At this time, you can use a self-adhesive sticker to write the MAC address of the module and paste it on the module.
The transmitter program consists of the following parts:
Enable the WiFi function of the module
Configure the ESP-NOW protocol and enable it
Add a node (peer) that needs to communicate
Send a message
The specific code is as follows:
The receiver program is mainly composed of the following parts:
Enable the WiFi function of the module
Configure the ESP-NOW protocol and enable it
Add a node (peer) that needs to communicate
Receive and decode the message, and send commands to NyBoard through the serial port
The specific code is as follows:
This code is encapsulated in a function named espnow_rx()
for the convenience of automatically starting the program after power-on.
There are two ways to realize automatic startup after power-on:
Rename the code file to main.py
;
Modify the boot.py
;
For beginners, we recommend the first method.
Writing the serial command conversion at the receiving end will make the program too complicated and difficult to maintain. We can create a new function in which to perform instruction conversion and output commands.