Skip to main content

ESP-NOW Control

ESP-NOW Settings

This tutorial aims to guide users in configuring ESP-NOW communication for the robot, enabling one-to-one and one-to-many wireless control functionality based on JSON command format.

ESP-NOW Communication Protocol

ESP-NOW is a fast and efficient wireless communication protocol developed by Espressif Systems, designed to provide a low-power, high-performance Local Area Network (LAN) communication solution. It was originally designed for Espressif's Wi-Fi chips such as ESP8266 and ESP32. The features of ESP-NOW are as follows:

  1. Low latency: The ESP-NOW protocol does not require TCP/IP stack processing, thus offering very low latency, suitable for applications with high real-time requirements.

  2. High throughput: ESP-NOW uses frame broadcasting to send data, resulting in high transmission efficiency, allowing the same information to be sent to multiple nodes simultaneously.

  3. Low power consumption: ESP-NOW consumes very low power during data transmission, making it suitable for battery-powered applications.

  4. Simple and easy to use: ESP-NOW configuration is straightforward; wireless communication can be achieved by simply defining basic parameters such as channel, encryption key, and data structure.

  5. Supports multicast: ESP-NOW supports one-to-many or one-to-all node communication, enhancing network flexibility.

  6. Scalability: ESP-NOW supports the transmission of various data types, and more complex communication functions can be implemented through custom data structures.

ESP-NOW Functionality

The HexArth comes with built-in ESP-NOW functionality. The product defaults to Follower mode after factory power-on. In this mode, the robot can be controlled by other devices and can also send ESP-NOW messages to control other devices.

ESP-NOW Communication Data Structure

Before explaining the meanings of each function command, we first introduce the ESP-NOW communication data structure of the HexArth, as follows:

struct Vec3 {
double x;
double y;
double z;
};
typedef struct struct_message {
byte devCode;
Vec3 ESPInBase[6];
byte cmd;
char message[50];
} struct_message;

The specific assignments to variables in this section will be set in some JSON commands. Here we explain the meaning of the variables:

  • devCode: The device number of this device. This variable is not used in our usage examples, so you can choose not to set this number (always set to 0). In situations where there are many devices to be controlled and multicast control cannot be used, requiring broadcast control, this variable can be used to distinguish JSON command control devices. This is an interface reserved for custom functions; modify the OnDataRecv() function in esp_now_ctrl.h in the lower-level program according to your needs.
  • ESPInBase: An array of Vec3 structures containing the coordinate values of each leg, in mm.
  • cmd: Type of ESP-NOW control information. For specific values, refer to the explanation of JSON command meanings below.
  • message: A string. When the ESP-NOW control message type (cmd) is 1, this string is used as a JSON format command, but the command content cannot be one that causes long-term blocking. Next, let's look at the specific meanings of JSON commands related to ESP-NOW configuration.

Set Broadcast Follower Mode

{"T":300,"mode":1}

{"T":300,"mode":0,"mac":"00:00:00:00:00:00"}
  • 300: This command is CMD_BROADCAST_FOLLOWER, used to set the broadcast follower mode of this device.
  • mode:
    • 1 - [Factory default] This device can be controlled by broadcast signals from other devices.
    • 0 - This device cannot be controlled by broadcast signals from other devices.
  • mac: When the mode value is 0, this is the MAC address of the unique Leader control device.
    • Given a MAC address, this device can only be controlled by ESP-NOW messages sent from the device with that MAC address;
    • If this device does not need to be controlled by any device, you can enter any fabricated MAC address, e.g., 00:00:00:00:00:00.

Set ESP-NOW Working Mode

{ "T": 301, "mode": 3 }
  • 301: This command is CMD_ESP_NOW_CONFIG, used to set the ESP-NOW working mode of this device.

  • mode (byte): Code for the ESP-NOW working mode.

    • 0 - Off;
    • 1 - Stream multicast Leader, continuously sends its own coordinate position information to devices in peerList;
    • 2 - Stream unicast/stream broadcast Leader, continuously sends its own coordinate position information to a device with a specific MAC address. If the MAC address is set to FF:FF:FF:FF:FF:FF, it becomes stream broadcast control;
    • 3 - [Factory default] Follower. When the robot is in this mode, it can receive ESP-NOW information from other Leader devices and can also act as a Leader device to send ESP-NOW information to other devices.
    warning

    Only in stream multicast or stream unicast/stream broadcast mode will the master control device continuously send its own coordinate position information to the controlled device. Only then will the controlled device follow and perform the same movements when the master control device is manually moved.

Get the MAC Address of This Device

{"T":302}
  • 302: This command is CMD_GET_MAC_ADDRESS, used to obtain the MAC address of this device. After inputting this command, the return value is shown below:
    44:17:93:EE:FD:70
    Each device has a unique MAC address. When using ESP-NOW features, except for broadcast control, you need to obtain the MAC address of the controlled device. By default, each robot displays its own MAC address on the OLED screen.

Add the Controlled Device's MAC Address to peerList

{ "T": 303, "mac": "44:17:93:EE:FD:70" }
  • 303: This command is CMD_ESP_NOW_ADD_FOLLOWER, used to add the MAC address of the controlled device to peerList. peerList is used to store MAC addresses.
  • mac: The MAC address of the controlled device to be added. Up to a dozen MAC addresses can be added to peerList for multicast control. However, when using multicast control, the MAC address for broadcast control (FF:FF:FF:FF:FF:FF) must not be stored in peerList.

Remove the Controlled Device's MAC Address from peerList

{ "T": 304, "mac": "44:17:93:EE:FD:70" }
  • 304: This command is CMD_ESP_NOW_REMOVE_FOLLOWER, used to remove the controlled device's MAC address from peerList.
  • mac: The MAC address of the controlled device to be removed.

Multicast Control

//Multicast control: Move the end coordinates of each leg of the devices in peerList to the specified positions
{"T":305,"dev":0,"x1":156.24,"y1":-109.69,"z1":-80.83,"x2":0.18,"y2":-197.19,"z2":-80.83,"x3":-156.06,"y3":-110,"z3":-80.83,"x4":156.06,"y4":110,"z4":-80.83,"x5":-0.18,"y5":197.19,"z5":-80.83,"x6":-156.24,"y6":109.69,"z6":-80.83,"cmd":0,"megs":"hello!"}

//Multicast control: Send a JSON command to turn on the LED light to devices in peerList
{"T":305,"dev":0,"x1":156.24,"y1":-109.69,"z1":-80.83,"x2":0.18,"y2":-197.19,"z2":-80.83,"x3":-156.06,"y3":-110,"z3":-80.83,"x4":156.06,"y4":110,"z4":-80.83,"x5":-0.18,"y5":197.19,"z5":-80.83,"x6":-156.24,"y6":109.69,"z6":-80.83,"cmd":1,"megs":"{\"T\":114,\"led\":255}"}
  • 305: This command is CMD_ESP_NOW_GROUP_CTRL. Use this command to send information via ESP-NOW multicast to all devices in peerList. Refer to the ESP-NOW Communication Data Structure section. The mapping of each key in this JSON command to the structure variables is as follows, with additional notes:
  • dev: Corresponds to the variable devCode.
  • x, y, z: Correspond to x, y, z in the ESPINBase array respectively.
  • cmd: Type of ESP-NOW control information.
    • When cmd is 0, each leg of the controlled robot will move to the given coordinate positions;
    • When cmd is 1, x, y, z are invalid. Enter a non-blocking JSON command in megs, and the robot will execute the function corresponding to the JSON command.
  • megs: Corresponds to the variable message.

Unicast/Broadcast Control

//Unicast control: Send a coordinate control command to a specific device
{"T":306,"mac":"44:17:93:EE:FD:70","dev":0,"x1":156.24,"y1":-109.69,"z1":-80.83,"x2":0.18,"y2":-197.19,"z2":-80.83,"x3":-156.06,"y3":-110,"z3":-80.83,"x4":156.06,"y4":110,"z4":-80.83,"x5":-0.18,"y5":197.19,"z5":-80.83,"x6":-156.24,"y6":109.69,"z6":-80.83,"cmd":0,"megs":"hello!"}

//Broadcast control: Send a coordinate control command to all devices
{"T":306,"mac":"FF:FF:FF:FF:FF:FF","dev":0,"x1":156.24,"y1":-109.69,"z1":-80.83,"x2":0.18,"y2":-197.19,"z2":-80.83,"x3":-156.06,"y3":-110,"z3":-80.83,"x4":156.06,"y4":110,"z4":-80.83,"x5":-0.18,"y5":197.19,"z5":-80.83,"x6":-156.24,"y6":109.69,"z6":-80.83,"cmd":0,"megs":"hello!"}
  • 306: This command is CMD_ESP_NOW_SINGLE. Use this command to unicast or broadcast control devices in peerList.
  • mac: The MAC address of the controlled device. When the MAC address is FF:FF:FF:FF:FF:FF, this command is a broadcast signal and will be sent to all devices. Refer to the ESP-NOW Communication Data Structure section. The mapping of each key in this JSON command to the structure variables is as follows, with additional notes:
  • dev: Corresponds to the variable devCode.
  • x, y, z: Correspond to x, y, z in the ESPINBase array respectively.
  • cmd: Type of ESP-NOW control information.
    • When cmd is 0, each leg of the controlled robot will move to the given coordinate positions;
    • When cmd is 1, x, y, z are invalid. Enter a non-blocking JSON command in megs, and the robot will execute the function corresponding to the JSON command.
  • megs: Corresponds to the variable message.
    warning

    Whether for multicast control or unicast/broadcast control, you need to add the MAC address of the controlled device to peerList in advance.