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.
Realize remote debugging and upload script
The previous tutorial realized the function of the robot to perform sequence actions by editing Python code offline. But this is very inconvenient. Whenever we need to modify the code, we need to unplug the WiFi module for modification, and we cannot flexibly pause and modify parameters during execution. The reason is that ESP8266 only has one serial port, and we need to use it to communicate with NyBoard. Fortunately, MicroPython uses the WiFi function provided by ESP to realize remote wireless Python debugging - WebREPL.
On the basis of official documents, combined with the characteristics of ESP8266, we wrote the following tutorials:
After connecting the device, enter import webrepl_setup
in the shell interface, and input according to the prompt information:
Enable it running on boot: E
Set password for it: your own password(e.g. 1234)
Repeat the password to confirm
Reboot the ESP8266: y
2. The script to setup webREPL
We use the demo script below, to replace the SSID and password with the network information your own around you.
After running the script, it will keep trying to connect to the WiFi network. Once connected, it will automatically start the WebREPL service of the device.
Remember this IP address (automatically assigned by router DHCP), useful when configuring WebREPL.
We are now debugging the Python script through WebREPL, and the previous serial port is used to communicate with NyBoard. So in the options, change the previous USB-COMx interface to WebREPL.
Then we fill in the IP address, port and password of WebREPL, and click OK.
When WebREPL Connected
is displayed, the connection is successful.
We can try some simple scripts, such as blink.py
.
WebREPL saves serial ports and supports wireless debugging. The disadvantage is that the speed is slow (because of network delay), and the waiting time for software reset is relatively long.
Now we can use webREPL to debug the script, but when we open the serial port monitor, we will find that whenever we run the script, the serial port will send out a series of debugging content: These massive strings will cause the NyBoard to be too late to process and crash. As shown below:
We hope that when debugging the program, the serial port only outputs the commands we want to output, not the Debug information. Open the boot.py
on the device, uncomment the line of code uos.dupterm(None, 1)
and save it , and unbind the serial port and REPL debug. Restart the module, and the serial port debugging assistant will no longer print the debug information.
As a supplement, we can output debug information through the print()
statement, which will be displayed in the Shell through WiFi.
So far, you can easily use the ESP8266 to debug robot through webREPL to edit action sequences based on MicroPython.
The tutorial of using the WiFi module as a MicroPython controller
USB Uploader (CH340C)
WiFi ESP8266
Insert the ESP8266 module into the module configuration interface of the USB uploader, and find the corresponding COM port in the Windows device manager.
Download the latest version of Thonny, an out-of-the-box Python editor that natively supports MicroPython.
Download address: https://thonny.org/
The compiled ESP8266 firmware is provided on the MicroPython official website, because our WiFi module is 4MB, please select the latest firmware with the name of ESP8266 with 2MiB+ flash, and download the bin file.
Firmware download address: https://micropython.org/download/esp8266/
There are two ways to upload the MicroPython firmware to the ESP8266 module:
Using the ESPtool download tool, you can more precisely control the partition and use of Flash.
Using Thonny's built-in tool.
For convenience, we use Thonny's built-in tool. The specific steps are as follows:
Open the Thonny, the main interface is as shown below. Thonny uses the Python interpreter in the installation directory by default.
Open Tools -> Options to enter the options page. In the first tab General, we can choose the language we need (needs to be restarted).
Open the second tab Interpreter, we replace the default Python3 interpreter with MicroPython (ESP8266) and select the corresponding port.
At this time, the ESP8266 module has not yet uploaded the MicroPython firmware. Click "Install or update firmware" in the lower right corner of the above picture to update the firmware using the built-in tool.
Select the port (COMx) where the ESP8266 module is located, and select the location where the downloaded MicroPython firmware (.bin file) is located. Check the flash mode: from image file (keep) (the speed will be slower, but it only needs to be burned once and it is not easy to make mistakes), and check the option Erase flash before installing. Press the Install button.
The progress will be displayed in the lower-left corner of the interface, erase the Flash first, and then write the firmware. When the word Done appears, it means that the programming has been completed.
The software preparation work is over, and the following display will appear after closing the download interface. The red text is garbled because ESP8266 will print a string of codes with a baud rate other than 115200 when it starts up. This code cannot be recognized by MicroPython Shell. When Python’s iconic symbol >>> appears, it means that the firmware is uploaded successfully.
After uploading the MicroPython firmware on ESP8266, we can use it run MicroPython scripts.
We can execute the python scripts directly in the interpreter.
The NyBoard WiFi module ESP8266 uses the IO2 pin to connect with a red LED to indicate the connection status. This LED is programmable. Write a simple python blink script:
Press the green start button on the toolbar, and the script will be sent to the WiFi module through the serial port, and then run after being interpreted by the built-in MicroPython interpreter of ESP8266. Because the Blink script is an endless loop when it needs to stop, press the red stop button to end the program interruption and reset.
We can click View -> File to open the file toolbar, and the file is displayed on the left side of Thonny. The upper part is the directory of the local machine, and the lower part is the files stored in the MicroPython device. By default, there is only one boot.py file, please do not delete this file, it is the startup file of the MicroPython device.
We save the script as blink.py and save it on the machine, right-click on the file and select Upload to /
:
Select the MicroPython device
in the pop-up window:
There is a blink.py
file on the device. So the file is saved on the device.
ESP8266 can send commands to NyBoard through the serial port. We only need to write a simple serial port sending script to send a series of serial port commands to NyBoard, and then the robot can execute sequence actions.
When the actSeq() function is executed, It can output a series of commands through the serial port. Using the serial monitor we can debug. Use the serial port monitor to monitor the output as follows (for the convenience of reading, please use the automatic frame break of the serial port debugger, in fact, there is no automatic line break).
After we debug the sequence action, unplug the ESP8266 and plug it into the NyBoard, the robot dog does not respond because the actSeq()
function is not running. We want to run the scripts automatically after power on. There are two methods:
Please change the file name to "main.py
" and save to the device (Recommend)
Modify the Boot.py