Language: EN

como-usar-esp-now-en-esp32

Basic Configuration of ESP-NOW with ESP32

We have already seen what ESP-NOW is, a technology from Espressif for wirelessly communicating ESP32 (without needing Wi-Fi).

Now we are going to start looking at how to use it with this first example. For now, we will see how to send a single primitive value (a float) between two devices.

esp32-espnow-one-to-one

A sender and a receiver

Configuring the ESP32

Before we can use ESP-NOW, we need to add the necessary libraries.

#include <WiFi.h>
#include <esp_now.h>

Next, we need to configure the ESP32 to work in the appropriate mode. For this, we will use the following code in the setup():

void setup() {
  WiFi.mode(WIFI_STA);
  if (esp_now_init() != ESP_OK) {
    Serial.println("Error initializing ESP-NOW");
    return;
  }
  esp_now_register_send_cb(onDataSent);
  esp_now_register_recv_cb(onDataReceived);
}
  • WiFi.mode(WIFI_STA): Configures the ESP32 in Wi-Fi client mode so it can connect to a Wi-Fi access point.
  • esp_now_init(): Initializes the ESP-NOW library.
  • esp_now_register_send_cb(): Registers a callback function that will be executed when data is sent to another ESP32 device.
  • esp_now_register_recv_cb(): Registers a function that will be executed when data is received from another ESP32 device.

Sending data

For one ESP32 to send information to another, we first need to “register” the MAC address of the device we want to communicate with. We would do it like this:

void static RegisterPeeks()
{
	esp_now_peer_info_t peerInfo;
	memcpy(peerInfo.peer_addr, RECEIVER_MAC_ADDRESS, 6);
	peerInfo.channel = 0;
	peerInfo.encrypt = false;

	if(esp_now_add_peer(&peerInfo) != ESP_OK)
	{
		Serial.println("Failed to add peer");
	}
	else
	{
		Serial.print("Registered peer ");
	}
}

Now, we can send through ESP-NOW anywhere in our code. To send data to another ESP32 device, we use the esp_now_send() function:

uint8_t dataToSend = 42; // Data we want to send
esp_now_send(otherDeviceMacAddress, &dataToSend, sizeof(dataToSend));
  • otherDeviceMacAddress: The MAC address of the other ESP32 device to which we want to send the data.
  • &dataToSend: Pointer to the data we want to send.
  • sizeof(dataToSend): The size of the data we are sending.

Receiving data

To receive data, we need to define the callback functions onDataSent() and onDataReceived() that will be executed when data is sent or received through ESP-NOW:

void onDataSent(uint8_t *mac_addr, uint8_t sendStatus) {
  // Code to execute when data is sent to another ESP32 device
}

void onDataReceived(uint8_t *mac_addr, uint8_t *data, uint8_t len) {
  // Code to execute when data is received from another ESP32 device
}

Complete Example

Let’s put everything together in a complete example of how to set up and use ESP-NOW communication between two ESP32 devices.

I have separated it into small files so you can have it organized and ready to use in your project (you will need to replace the MAC addresses in the constants file).

// sender
#include <esp_now.h>
#include <WiFi.h>

#include "const.h"

void OnDataSent(const uint8_t* mac_addr, esp_now_send_status_t status)
{
	Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
}

void SendMessage()
{	
	float myFloat = 123.456;
	uint8_t payload[sizeof(float)];
	memcpy(payload, &myFloat, sizeof(float));

	esp_err_t result = esp_now_send(MAC_RECEIVER_1, (uint8_t*)&payload, sizeof(float));

	if(result == ESP_OK)
	{
		Serial.println("Sent with success");
	}
	else
	{
		Serial.println("Error sending the data");
	}
}

void static RegisterPeeks()
{
	esp_now_peer_info_t peerInfo;
	memcpy(peerInfo.peer_addr, MAC_RECEIVER_1, 6);
	peerInfo.channel = 0;
	peerInfo.encrypt = false;

	if(esp_now_add_peer(&peerInfo) != ESP_OK)
	{
		Serial.println("Failed to add peer");
	}
	else
	{
		Serial.print("Registered peer ");
	}
}

void static InitEspNow()
{
	if(esp_now_init() != ESP_OK)
	{
		Serial.println("Error initializing ESP-NOW");
	}
	else
	{
		esp_now_register_send_cb(OnDataSent);

		RegisterPeeks();
	}
}


void setup()
{
	Serial.begin(115200);
	delay(2000);
	WiFi.mode(WIFI_STA);

	InitEspNow();
}


void loop()
{
	SendMessage();

	delayMicroseconds(200);
}
// receiver
#include <esp_now.h>
#include <WiFi.h>

#include "const.h"

void OnMessageReceived(const uint8_t* mac, const uint8_t* data, int len)
{
    float receivedFloat;
    memcpy(&receivedFloat, incomingData, sizeof(float));
    
	Serial.println(receivedFloat);
}

void initEspNow()
{
	if(esp_now_init() != ESP_OK)
	{
		Serial.println("Error initializing ESP-NOW");
		return;
	}
	else
	{
		esp_now_register_recv_cb(OnMessageReceived);
	}
}

void setup()
{
	Serial.begin(115200);

	WiFi.mode(WIFI_STA);

	initEspNow();
}

void loop()
{

}
const uint8_t MAC_SENDER_1[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
const uint8_t MAC_RECEIVER_1[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };