ESP-NOW is an alternative communication protocol for ESP8266 and ESP32. Basic concepts



ESP-NOW is a simplified WiFi protocol for transferring short packets between pairs of paired devices, developed and released by Espressif in 2016.07 for the ESP8266 and ESP32 microcontrollers. At the same time, additional procedures related to WiFi protocol support are not used, which speeds up the process of exchanging packets.

ESP-NOW can be used on the Internet of Things to control intelligent light sources, relays, sockets, other remote control devices, receive information from sensors and other applications.

ESP-NOW supports the following features


  • Encrypted and unencrypted communication between paired devices.
  • Mixed encrypted and unencrypted communication between paired devices.
  • Transfer up to 250 bytes of useful information.
  • Setting up a callback function to inform the application layer, in particular, about the success or failure of the transmission.


ESP-NOW also has the following features and limitations.


  • Transmission speed - no more than 1 Mbps at a frequency of 2.4 GHz, i.e. ESP-NOW operates on the same frequency and channels as your WiFi router.
  • WiFi protocol not used
  • Similar to the low-power protocol used in a 2.4 GHz wireless mouse.
  • Only initial pairing is required.
  • After pairing, the connection does not break.
  • Broadcast is not supported - only multiple distribution to paired device pairs.
  • A maximum of 20 pairs, including encrypted ones, are supported on one device, including encrypted pairs.
  • A maximum of 10 encrypted pairs are supported in Station mode.
  • Maximum 6 in SoftAP or SoftAP + Station mode.
  • Multicast encryption is not supported.

Safety


ESP-NOW adopts the IEEE802.11 Action Vendor frame technology with the IE function developed by Espressif and CCMP encryption technology, providing a secure, connection-free communication solution. A Wi-Fi device supports a primary master key (PMK) and multiple local master keys (LMK)

  • PMK is used to encrypt LMK using the AES-128 algorithm.
  • The LMK paired device is used to encrypt user information using CCMP. The maximum number of different LMKs is 6. If no LMK is set for the paired device, user data will not be encrypted.

A basic level of


At the lower level of the ESP_NOW protocol, a linked list is maintained containing information about the local device and the paired device, including MAC addresses and keys. ESP-NOW also stores frequently used data for the application layer to avoid the overhead of reprocessing the linked list. Information about devices is used to send and receive data and includes

Information about the local device:

  • PMK : 16 bytes - the main master key that is used to encrypt the key on the attached device (KOK in the API) ESP_NOW supports PMK by default, so no configuration is required. If necessary, you can verify that the PMK value matches the local device.
  • Mode : 1 byte - the local device mode that defines the transmitting WiFi interface (SoftAP or STA) ESP-NOW. The paired device mode does not affect any function, but only saves mode information for the application layer. In STA WiFi mode, only Station is applicable and SoftAP WiFi is only SoftAP.
    Local device operation modes ESP_NOW
    WiFi
    IDLE
    CONTROLLERSTA
    SLAVESoftAP

    COMBO&SoftAP


Paired device information (including frequently used information and other user information):

  • LMK : 16 bytes - a local master key that is used to encrypt the key of useful information during communication in this pair.
  • MAC-: 6 β€” , . , Station, MAC- Station.
  • : 1 β€” (SoftAP STA) ESP-NOW.
  • : 1 β€” , , . 0..255. , . . , 0 , ; 1 ~ 14 ; , .

Espressif does not recommend the use of lengthy operations in callback functions when sending / sending packets, which is presumably related to the implementation of algorithms using the interrupt mechanism. This assumption is also supported by the problems associated with dynamic memory allocation in callback functions, which is solved by the preferred use of static variables, as well as the ambiguity of using the MicroPython exception mechanism .

The implementation of the asynchrony of the start / end processes, pairing, receiving / transmitting Espressif packets is not described, which also does not facilitate the application of the asyncio MicroPython ideology .

ESP-NOW Package Format


  • MAC Header : 24 bytes.
  • Category : 1 byte indicating the category of the package creator. The value is set (127).
  • Organization ID : 3 bytes, contains a unique identifier, which is the first three bytes of the MAC address used by Espressif. Set value (0x18fe34)
  • Random value : 4 bytes, used to protect data.
  • Package Creator Data : 7-255Bytes

The package creator data contains the following fields:

  • ID : 1 byte, Set to (221).
  • Length : 1 byte, total length of organization ID, type, version, and user data.
  • Organization ID : 3 bytes, contains a unique identifier, which is the first three bytes of the MAC address used by Espressif. Set value (0x18fe34)
  • Type : 1 byte, ESP-NOW protocol. Set value (4)
  • Version : 1 byte, current version of ESP-NOW. Installed (1)
  • Content : 0-250 bytes user data.
  • FCS : 4 bytes, checksum

Since ESP-NOW does not use WiFi, the MAC header is slightly different from the standard packet header. The FromDS and ToDS bits of the FrameControl field are 0. The destination address is specified in the first address field. The second address field shows the source address. The third address field is set as the broadcast address (0xff: 0xff: 0xff: 0xff: 0xff: 0xff).

Basic application algorithm


Start and end


Before using ESP-NOW, it is recommended that you set the Wi-Fi interface in the desired mode. Typically, the Station interface is set for CONTROLLER , the SoftAP interface for SLAVE and COMBO . It is also advisable to stop Wi-Fi after using ESP-NOW.

To start ESP-NOW, call esp_now_init () and esp_now_deinit () to complete. When esp_now_deinit () is called , all information about paired devices is deleted.

Binding Callback Functions


The call processing function when sending the packet esp_now_register_send_cb () can be used to inform the application level of the sending party in a pair about the success or failure of the transmission, for example, if the information on the MAC sublayer is transmitted successfully.

When using esp_now_register_send_cb () , consider the following:

In a linked pair:

  • If the application layer did not receive the packet, but the callback function returned "success", the reason could be:
    - attacks from a fraudulent device
    - encrypted key installation errors
    - packet loss at the Espressif application level
  • If the application layer received the packet, but the callback function returns an error, the cause may be:
    - The channel is busy and the ACK is not received.

With multiple communication with all pairs of the local device:

  • If the callback returns β€œsuccess”, it means the packet was sent successfully.
  • If the callback function returns an error, it means that the packet was not sent successfully.

The call processing function upon receipt of the packet esp_now_register_receive_cb () returns information including the MAC address of the sending device in a pair and useful information. It can also be used to inform the application layer of the sending device paired with the receiver that the packet was successfully received.

Adding a pair of paired devices


Before sending data, you must add the device to the list of pairs of paired devices by calling esp_now_add_peer () . Before sending data to a group of established pairs, you must add a device with a multicast MAC address. The channel range of paired devices is from 0 to 14. If the channel is set to 0, data will be sent on the current channel. Otherwise, the channel must be set as the channel on which the local device is located.

Safety


If the key needs to be encrypted, you can call esp_now_set_pmk () to configure it to set PMK. If PMK is not installed, PMK will be used by default and select the same key for all devices. Also set LMK for selected pairs.

Sending Data with ESP-NOW Packages


Use esp_now_send () to send ESP-NOW data . At the same time, the function set earlier in esp_now_register_send_cb () will return ESP_NOW_SEND_SUCCESS when sending a callback function if the data was successfully received at the MAC level. Otherwise, ESP_NOW_SEND_FAIL will return . Several reasons may cause ESP-NOW to not be able to send data. In particular,

  • target device does not exist;
  • device channels do not match;
  • data is lost during transmission.

It is not guaranteed that the application layer will necessarily accept data. If necessary, you can send a confirmation when you receive ESP-NOW data. If a confirmation timeout occurs, the ESP-NOW data transmission should be repeated. A sequence number can also be assigned to ESP-NOW data to remove duplicate data.

When sending ESP-NOW data via esp_now_send (), please note that no more than 250 bytes of information can be sent at a time.

ATTENTION! Too short an interval between sending two ESP-NOW packets can lead to errors in the execution of the callback function, therefore it is recommended to send the next data packet ESP-NOW after the callback function has successfully completed the processing of the previous sending. The callback function is sent from a high priority Wi-Fi task. Therefore, it is not recommended to perform lengthy operations in the callback function. Instead, you can place the necessary data in a static queue and process it from a process with a lower priority.

If the send function returns a MAC address, then it will be sent to the device with this MAC address. If the send function returns NULL, then the packet will be sent to all devices attached to the sending one, which may lead to transmission failure or delay due to network congestion.

ESP-NOW Receive Data


The callback function also starts from a Wi-Fi task. Therefore, it is not recommended to perform lengthy operations in the callback function. Instead, you can place the necessary data in a static queue and process it with a lower priority process.

In conclusion, the foregoing


My experience in building the Espressif IDE and MicroPython with ESP-NOW, describing the errors that I encountered during the assembly and going to fix them after I achieve a stable result. Then I’ll make a description of the ESP-NOW library on MicroPython with the errors detected, and ways to resolve them. Unfortunately, due to the fact that the source code of ESP-NOW is closed and distributed only in binary form, the understanding of the algorithms of the ESP-NOW protocol is empirical and there are a number of problems that have already been identified, for which there are not always options for overcoming them logically, but in general ESP -NOW is successfully used in the community of both C-users and Pythonists based on more than 300 applications presented on GitHub.

Description of the ESP-NOW C Library by Espressif

MicroPython ESP-NOW Description and Open Sources

All Articles