ESP-NOW introduction
ESP-NOW is a protocol developed by Espressif, which allows multiple devices to communicate with each other without using Wi Fi. The protocol is similar to a low-power 2.4GHz wireless connection. Pairing between devices needs to be completed before communication. After pairing, the connection is secure, point-to-point, and no handshake is required. This means that after the devices are paired with each other, the connection is persistent. In other words, if a board suddenly loses power or resets, when it restarts, it will automatically connect to its channel to continue communication
ESP-NOW supports the following features
- Mixed encrypted and unencrypted peer devices
- Encrypted and unencrypted unicast communication
- It can carry a payload of up to 250 bytes (small data transmission);
- Send callback function, which can be set to notify the application layer of transmission success or failure;
ESP-NOW technology also has the following limitations
- Limited encryption. The Station mode supports up to 10 encrypted peers; The maximum number of "soft dial" or "soft dial + workstation" modes is 6
- Multiple unencrypted peers are supported, including encrypted peers. The total number cannot exceed 20
3. The maximum message length is limited to 250 bytes
Get the MAC address of ESP32
Before using ESP-NOW protocol, you need to know the MAC address of ESP32
#include "WiFi.h" void setup(){ Serial.begin(115200); WiFi.mode(WIFI_MODE_STA); Serial.println(WiFi.macAddress()); } void loop(){ }
Open the serial port monitor on the serial port to get the MAC address of the board, such as
You'd better take a small note and write it down
ESP-NOW one-way communication
One ESP32 acts as the sender and the other ESP32 acts as the receiver
Sender program
#include <esp_now.h> #include <WiFi.h> // MAC address of receiving end uint8_t broadcastAddress[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; // Send structure type typedef struct struct_message { char a[32]; int b; float c; bool d; } struct_message; // Create a structure variable struct_message myData; // Callback function, which will be executed when sending a message. This function tells us whether the message was successfully sent; void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) { Serial.print("\r\nLast Packet Send Status:\t"); Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail"); } void setup() { // Initialize serial baud rate Serial.begin(115200); // Set WIFI mode to STA mode, i.e. wireless terminal WiFi.mode(WIFI_STA); // Initialize ESP-NOW if (esp_now_init() != ESP_OK) { Serial.println("Error initializing ESP-NOW"); return; } //Registering Callbacks esp_now_register_send_cb(OnDataSent); // Memory Copy esp_now_peer_info_t peerInfo; memcpy(peerInfo.peer_addr, broadcastAddress, 6); peerInfo.channel = 0; peerInfo.encrypt = false; if (esp_now_add_peer(&peerInfo) != ESP_OK){ Serial.println("Failed to add peer"); return; } } void loop() { //Set the value to send strcpy(myData.a, "THIS IS A CHAR"); myData.b = random(1,20); myData.c = 1.2; myData.d = false; //Send information to the specified ESP32 esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &myData, sizeof(myData)); //Judge whether the transmission is successful if (result == ESP_OK) { Serial.println("Sent with success"); } else { Serial.println("Error sending the data"); } delay(2000); }
Receiver program
#include <esp_now.h> #include <WiFi.h> // Create a structure to receive data typedef struct struct_message { char a[32]; int b; float c; bool d; } struct_message; // Create a structure variable struct_message myData; // Callback function, which will be called when a message is received void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) { memcpy(&myData, incomingData, sizeof(myData)); Serial.print("Bytes received: "); Serial.println(len); Serial.print("Char: "); Serial.println(myData.a); Serial.print("Int: "); Serial.println(myData.b); Serial.print("Float: "); Serial.println(myData.c); Serial.print("Bool: "); Serial.println(myData.d); Serial.println(); } void setup() { // Initialize serial baud rate Serial.begin(115200); // Set wifi mode WiFi.mode(WIFI_STA); // Initialize ESP now if (esp_now_init() != ESP_OK) { Serial.println("Error initializing ESP-NOW"); return; } //Register callback function to receive information esp_now_register_recv_cb(OnDataRecv); } void loop() { }
Send the long-distance data to the two esp32s respectively, and turn on the serial port monitor
Sender:
Receiving end:
Two way communication between ESP32 boards
To be written
One to many communication (one transmitter, multiple receivers)
To be written
One to many communication (multiple sending and one receiving)
To be written