ESP-NOW with ESP32: Send Data to Multiple Boards (one-to-many)

In this tutorial you’ll learn how to use ESP-NOW communication protocol to send data from one ESP32 to multiple ESP32 or ESP8266 boards (one-to-many configuration). The boards will be programmed using Arduino IDE.

ESP-NOW with ESP32: Send Data to Multiple Boards one-to-many ESP8266 NodeMCU

To get started with ESP-NOW on the ESP32 or ESP8266, read the following ESP-NOW guides first:

Project Overview

This tutorial shows how to send data from one ESP32 to multiple ESP32 or ESP8266 boards using ESP-NOW (one-to-many configuration).

ESP-NOW with ESP32: Send Data to Multiple Boards (one-to-many) Project Overview
  • One ESP32 acts as a sender;
  • Multiple ESP32 or ESP8266 boards act as receivers. We tested this setup with two ESP32 boards and one ESP8266 board simultaneously. You should be able to add more boards to your setup;
  • The ESP32 sender receives an acknowledge message if the messages are successfully delivered. You know which boards received the message and which boards didn’t;
  • You need to upload a slightly different receiver code depending if you’re using an ESP32 or ESP8266;
  • As an example, we’ll exchange random values between the boards. You should modify this example to send commands or sensor readings (exchange sensor readings using ESP-NOW).

This tutorial covers these two scenarios:

  • sending the same message to all boards;
  • sending a different message to each board.

You may also like reading: ESP-NOW Two-Way Communication Between ESP32 Boards.

Prerequisites

We’ll program the ESP32/ESP8266 boards using Arduino IDE, so before proceeding with this tutorial, make sure you have these boards installed in your Arduino IDE.

Parts Required

To follow this tutorial, you need multiple ESP32 boards and/or ESP8266 boards.

You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!

Getting the Boards MAC Address

To send messages via ESP-NOW, you need to know the receiver boards’ MAC address. Each board has a unique MAC address (learn how to Get and Change the ESP32 MAC Address).

Upload the following code to each of your receiver boards to get its MAC address.

// Complete Instructions to Get and Change ESP MAC Address: https://RandomNerdTutorials.com/get-change-esp32-esp8266-mac-address-arduino/

#ifdef ESP32
  #include <WiFi.h>
#else
  #include <ESP8266WiFi.h>
#endif

void setup(){
  Serial.begin(115200);
  Serial.println();
  Serial.print("ESP Board MAC Address:  ");
  Serial.println(WiFi.macAddress());
}
 
void loop(){

}

View raw code

After uploading the code, press the RST/EN button, and the MAC address should be displayed on the Serial Monitor.

ESP32 board MAC Address with Arduino IDE Serial Monitor

You can write down the boards’ MAC address on a label to clearly identify each board.

Identify ESP32 Board MAC Address

ESP32 Sender Code (ESP-NOW)

The following code sends data to multiple (three) ESP boards via ESP-NOW. You should modify the code with your receiver boards’ MAC address. You should also add or delete lines of code depending on the number of receiver boards.

/*********
  Rui Santos
  Complete project details at https://RandomNerdTutorials.com/esp-now-one-to-many-esp32-esp8266/
  
  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files.
  
  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.
*********/

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

// REPLACE WITH YOUR ESP RECEIVER'S MAC ADDRESS
uint8_t broadcastAddress1[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
uint8_t broadcastAddress2[] = {0xFF, , , , , };
uint8_t broadcastAddress3[] = {0xFF, , , , , };

typedef struct test_struct {
  int x;
  int y;
} test_struct;

test_struct test;

// callback when data is sent
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
  char macStr[18];
  Serial.print("Packet to: ");
  // Copies the sender mac address to a string
  snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x",
           mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
  Serial.print(macStr);
  Serial.print(" send status:\t");
  Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
}
 
void setup() {
  Serial.begin(115200);
 
  WiFi.mode(WIFI_STA);
 
  if (esp_now_init() != ESP_OK) {
    Serial.println("Error initializing ESP-NOW");
    return;
  }
  
  esp_now_register_send_cb(OnDataSent);
   
  // register peer
  esp_now_peer_info_t peerInfo;
  peerInfo.channel = 0;  
  peerInfo.encrypt = false;
  // register first peer  
  memcpy(peerInfo.peer_addr, broadcastAddress1, 6);
  if (esp_now_add_peer(&peerInfo) != ESP_OK){
    Serial.println("Failed to add peer");
    return;
  }
  // register second peer  
  memcpy(peerInfo.peer_addr, broadcastAddress2, 6);
  if (esp_now_add_peer(&peerInfo) != ESP_OK){
    Serial.println("Failed to add peer");
    return;
  }
  /// register third peer
  memcpy(peerInfo.peer_addr, broadcastAddress3, 6);
  if (esp_now_add_peer(&peerInfo) != ESP_OK){
    Serial.println("Failed to add peer");
    return;
  }
}
 
void loop() {
  test.x = random(0,20);
  test.y = random(0,20);
 
  esp_err_t result = esp_now_send(0, (uint8_t *) &test, sizeof(test_struct));
   
  if (result == ESP_OK) {
    Serial.println("Sent with success");
  }
  else {
    Serial.println("Error sending the data");
  }
  delay(2000);
}

View raw code

How the code works

First, include the esp_now.h and WiFi.h libraries.

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

Receivers’ MAC Address

Insert the receivers’ MAC address. In our example, we’re sending data to three boards.

uint8_t broadcastAddress1[] = {0x3C, 0x71, 0xBF, 0xC3, 0xBF, 0xB0};
uint8_t broadcastAddress2[] = {0x24, 0x0A, 0xC4, 0xAE, 0xAE, 0x44};
uint8_t broadcastAddress3[] = {0x80, 0x7D, 0x3A, 0x58, 0xB4, 0xB0};

Then, create a structure that contains the data we want to send. We called this structure test_struct and it contains two integer variables. You can change this to send whatever variable types you want.

typedef struct test_struct {
  int x;
  int y;
} test_struct;

Create a new variable of type test_struct that is called test that will store the variables values.

test_struct test;

OnDataSent() callback function

Next, define the OnDataSent() function. This is a callback function that will be executed when a message is sent. In this case, this function prints if the message was successfully delivered or not and for which MAC address. So, you know which boards received the message or and which boards didn’t.

void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
  char macStr[18];
  Serial.print("Packet from: ");
  // Copies the sender mac address to a string
  snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x",
           mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
  Serial.print(macStr);
  Serial.print(" send status:\t");
  Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
}

setup()

In the setup(), initialize the serial monitor for debugging purposes:

Serial.begin(115200);

Set the device as a Wi-Fi station:

WiFi.mode(WIFI_STA);

Initialize ESP-NOW:

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

After successfully initializing ESP-NOW, register the callback function that will be called when a message is sent. In this case, register for the OnDataSent() function created previously.

esp_now_register_send_cb(OnDataSent);

Add peers

After that, we need to pair with other ESP-NOW devices to send data. That’s what we do in the next lines – register peers:

  // register peer
  esp_now_peer_info_t peerInfo;
  peerInfo.channel = 0;
  peerInfo.encrypt = false;
  // register first peer
  memcpy(peerInfo.peer_addr, broadcastAddress1, 6);
  if (esp_now_add_peer(&peerInfo) != ESP_OK){
    Serial.println("Failed to add peer");
    return;
  }
  // register second peer
  memcpy(peerInfo.peer_addr, broadcastAddress2, 6);
  if (esp_now_add_peer(&peerInfo) != ESP_OK){
    Serial.println("Failed to add peer");
    return;
  }
  /// register third peer
  memcpy(peerInfo.peer_addr, broadcastAddress3, 6);
  if (esp_now_add_peer(&peerInfo) != ESP_OK){
    Serial.println("Failed to add peer");
    return;
  }

If you want to add more peers you just need to duplicate these lines and pass the peer MAC address:

memcpy(peerInfo.peer_addr, broadcastAddress, 6);
if (esp_now_add_peer(&peerInfo) != ESP_OK){
  Serial.println("Failed to add peer");
  return;
}

loop()

In the loop(), we’ll send a message via ESP-NOW every 2 seconds (you can change this delay time).

Assign a value to each variable:

test.x = random(0,20);
test.y = random(0,20);

Remember that test is a structure. Here assign the values that you want to send inside the structure. In this case, we’re just sending random values. In a practical application these should be replaced with commands or sensor readings, for example.

Send the same data to multiple boards

Finally, send the message as follows:

esp_err_t result = esp_now_send(0, (uint8_t *) &test, sizeof(test_struct));

The first argument of the esp_now_send() function is the receiver’s MAC address. If you pass 0 as an argument, it will send the same message to all registered peers. If you want to send a different message to each peer, follow the next section.

Check if the message was successfully sent:

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

The loop() is executed every 2000 milliseconds (2 seconds).

delay(2000);

Send different data to each board

The code to send a different message to each board is very similar with the previous one. So, we’ll just take a look at the differences.

If you want to send a different message to each board, you need to create a data structure for each of your boards, for example:

test_struct test;
test_struct test2;
test_struct test3;

In this case we’re sending the same structure type (test_struct). You can send a different structure type as long as the receiver code is prepared to receive that type of structure.

Then, assign different values to the variables of each structure. In this example, we’re just setting them to random numbers.

test.x = random(0,20);
test.y = random(0,20);
test2.x = random(0,20);
test2.y = random(0,20);
test3.x = random(0,20);
test3.y = random(0,20);

Finally, you need to call the esp_now_send() function for each receiver.

For example, send the test structure to the board whose MAC address is broadcastAddress1.

esp_err_t result1 = esp_now_send(
  broadcastAddress1, 
  (uint8_t *) &test,
  sizeof(test_struct));
   
if (result1 == ESP_OK) {
  Serial.println("Sent with success");
}
else {
  Serial.println("Error sending the data");
}

Do the same for the other boards. For the second board send the test2 structure:

esp_err_t result2 = esp_now_send(
  broadcastAddress2, 
  (uint8_t *) &test2,
  sizeof(test_struct));

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

And finally, for the third board, send the test3 structure:

esp_err_t result3 = esp_now_send(
  broadcastAddress3, 
  (uint8_t *) &test3,
  sizeof(test_struct));

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

Here’s the complete code that sends a different message to each board.

/*********
  Rui Santos
  Complete project details at https://RandomNerdTutorials.com/esp-now-one-to-many-esp32-esp8266/
  
  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files.
  
  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.
*********/

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

// REPLACE WITH YOUR ESP RECEIVER'S MAC ADDRESS
uint8_t broadcastAddress1[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
uint8_t broadcastAddress2[] = {0xFF, , , , , };
uint8_t broadcastAddress3[] = {0xFF, , , , , };

typedef struct test_struct {
  int x;
  int y;
} test_struct;

void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
  char macStr[18];
  Serial.print("Packet to: ");
  // Copies the sender mac address to a string
  snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x",
           mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
  Serial.print(macStr);
  Serial.print(" send status:\t");
  Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
}
 
void setup() {
 
  Serial.begin(115200);
 
  WiFi.mode(WIFI_STA);
 
  if (esp_now_init() != ESP_OK) {
    Serial.println("Error initializing ESP-NOW");
    return;
  }

  esp_now_register_send_cb(OnDataSent);

  // register peer
  esp_now_peer_info_t peerInfo;
  peerInfo.channel = 0;  
  peerInfo.encrypt = false;
    
  memcpy(peerInfo.peer_addr, broadcastAddress1, 6);
  if (esp_now_add_peer(&peerInfo) != ESP_OK){
    Serial.println("Failed to add peer");
    return;
  }
  
  memcpy(peerInfo.peer_addr, broadcastAddress2, 6);
  if (esp_now_add_peer(&peerInfo) != ESP_OK){
    Serial.println("Failed to add peer");
    return;
  }
  memcpy(peerInfo.peer_addr, broadcastAddress3, 6);
  if (esp_now_add_peer(&peerInfo) != ESP_OK){
    Serial.println("Failed to add peer");
    return;
  }
}
 
void loop() {
  test_struct test;
  test_struct test2;
  test_struct test3;
  test.x = random(0,20);
  test.y = random(0,20);
  test2.x = random(0,20);
  test2.y = random(0,20);
  test3.x = random(0,20);
  test3.y = random(0,20);
 
  esp_err_t result1 = esp_now_send(
    broadcastAddress1, 
    (uint8_t *) &test,
    sizeof(test_struct));
   
  if (result1 == ESP_OK) {
    Serial.println("Sent with success");
  }
  else {
    Serial.println("Error sending the data");
  }
  delay(500);
  esp_err_t result2 = esp_now_send(
    broadcastAddress2, 
    (uint8_t *) &test2,
    sizeof(test_struct));

  if (result2 == ESP_OK) {
    Serial.println("Sent with success");
  }
  else {
    Serial.println("Error sending the data");
  }
  
  delay(500);  
  esp_err_t result3 = esp_now_send(
    broadcastAddress3, 
    (uint8_t *) &test3,
    sizeof(test_struct));
   
  if (result3 == ESP_OK) {
    Serial.println("Sent with success");
  }
  else {
    Serial.println("Error sending the data");
  }
  delay(2000);
}

View raw code

ESP32 Receiver Code (ESP-NOW)

Upload the next code to the receiver boards (in our example, we’ve used three receiver boards).

/*********
  Rui Santos
  Complete project details at https://RandomNerdTutorials.com/esp-now-one-to-many-esp32-esp8266/
  
  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files.
  
  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.
*********/

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

//Structure example to receive data
//Must match the sender structure
typedef struct test_struct {
  int x;
  int y;
} test_struct;

//Create a struct_message called myData
test_struct myData;

//callback function that will be executed when data 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("x: ");
  Serial.println(myData.x);
  Serial.print("y: ");
  Serial.println(myData.y);
  Serial.println();
}
 
void setup() {
  //Initialize Serial Monitor
  Serial.begin(115200);
  
  //Set device as a Wi-Fi Station
  WiFi.mode(WIFI_STA);

  //Init ESP-NOW
  if (esp_now_init() != ESP_OK) {
    Serial.println("Error initializing ESP-NOW");
    return;
  }
  
  // Once ESPNow is successfully Init, we will register for recv CB to
  // get recv packer info
  esp_now_register_recv_cb(OnDataRecv);
}
 
void loop() {

}

View raw code

How the code works

Similarly to the sender, start by including the libraries:

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

Create a structure to receive the data. This structure should be the same defined in the sender sketch.

typedef struct test_struct {
  int x;
  int y;
} test_struct;

Create a test_struct variable called myData.

test_struct myData;

Create a callback function that is called when the ESP32 receives the data via ESP-NOW. The function is called onDataRecv() and should accept several parameters as follows:

void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {

Copy the content of the incomingData data variable into the myData variable.

memcpy(&myData, incomingData, sizeof(myData));

Now, the myData structure contains several variables inside with the values sent by the sender ESP32. To access variable x, for example, call myData.x.

In this example, we print the received data, but in a practical application you can print the data on a OLED display, for example.

Serial.print("Bytes received: ");
Serial.println(len);
Serial.print("x: ");
Serial.println(myData.x);
Serial.print("y: ");
Serial.println(myData.y);
Serial.println();

In the setup(), intialize the Serial Monitor.

Serial.begin(115200);

Set the device as a Wi-Fi Station.

WiFi.mode(WIFI_STA);

Initialize ESP-NOW:

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

Register for a callback function that will be called when data is received. In this case, we register for the OnDataRecv() function that was created previously.

esp_now_register_recv_cb(OnDataRecv);

ESP8266 Receiver Code (ESP-NOW)

If you’re using an ESP8266 board as a receiver, upload the following code instead.

/*********
  Rui Santos
  Complete project details at https://RandomNerdTutorials.com/esp-now-one-to-many-esp32-esp8266/
  
  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files.
  
  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.
*********/

#include <ESP8266WiFi.h>
#include <espnow.h>

//Structure example to receive data
//Must match the sender structure
typedef struct test_struct {
  int x;
  int y;
} test_struct;

//Create a struct_message called myData
test_struct myData;

//callback function that will be executed when data is received
void OnDataRecv(uint8_t * mac, uint8_t *incomingData, uint8_t len) {
  memcpy(&myData, incomingData, sizeof(myData));
  Serial.print("Bytes received: ");
  Serial.println(len);
  Serial.print("x: ");
  Serial.println(myData.x);
  Serial.print("y: ");
  Serial.println(myData.y);
  Serial.println();
}
 
void setup() {
  //Initialize Serial Monitor
  Serial.begin(115200);
  
  //Set device as a Wi-Fi Station
  WiFi.mode(WIFI_STA);

  //Init ESP-NOW
  if (esp_now_init() != 0) {
    Serial.println("Error initializing ESP-NOW");
    return;
  }
  
  // Once ESPNow is successfully Init, we will register for recv CB to
  // get recv packer info
  esp_now_set_self_role(ESP_NOW_ROLE_SLAVE);
  esp_now_register_recv_cb(OnDataRecv);
}
 
void loop() {

}

View raw code

Apart from small details, this code is very similar with the ESP32 receiver code. So, we won’t explain how it works. To learn more you can read our ESP-NOW Getting Started Guide with ESP8266 NodeMCU.

Demonstration

Having all your boards powered on, open the Arduino IDE Serial Monitor for the COM port the sender is connected to.

You should start receiving “Delivery Success” messages with the corresponding receiver’s MAC address in the Serial Monitor.

ESP32 ESP-NOW Send Data To Multiple Boards Delivery Status Serial Monitor

If you remove power from one of the boards, you’ll receive a “Delivery Fail” message for that specific board. So, you can quickly identify which board didn’t receive the message.

ESP32 ESP-NOW Send Data To Multiple Boards Delivery Status Failed Serial Monitor

If you want to check if the boards are actually receiving the messages, you can open the Serial Monitor for the COM port they are connected to, or you can use PuTTY to establish a serial communication with your boards.

If you’re using PuTTY, select Serial communication, write the COM port number and the baud rate (115200) as shown below and click Open.

Serial Communication ESP Boards PuTTY

Then, you should see the messages being received.

ESP32 ESP8266 NodeMCU ESP-NOW Send Data To Multiple Boards Delivery Success Demonstration

Open a serial communication for each of your boards and check that they are receiving the messages.

Wrapping Up

In this tutorial you’ve learned how to send data to multiple ESP32 or ESP8266 boards from a single ESP32 using ESP-NOW (one-to-many communication).

We hope you’ve found this tutorial useful. We have other ESP-NOW tutorials that you may like:

Learn more about the ESP32 with our resources:

Thanks for reading.



Learn how to program and build projects with the ESP32 and ESP8266 using MicroPython firmware DOWNLOAD »

Learn how to program and build projects with the ESP32 and ESP8266 using MicroPython firmware DOWNLOAD »


Enjoyed this project? Stay updated by subscribing our weekly newsletter!

25 thoughts on “ESP-NOW with ESP32: Send Data to Multiple Boards (one-to-many)”

  1. Hi Rui and Sara, another great project!!!

    Please please make a project ESP32: RECEIVE Data FROM Multiple Boards (one-from-many) WITH WEBSERVER DISPLAY AJAX DATA FROM MULTIPLE BOARDS!!!
    P L E A S E !!!!!

    Reply
  2. Hi Sara,
    Thank you so much for the wonderful tutorial. Just what I needed!
    I have two questions though:
    1. If I want to perform a function in the sender sketch, say blink and LED (initially defined), when the data has been delivered successfully to the receiver, can I write:
    If (status== ESP_NOW_SEND_SUCCESS)
    {
    blinkLED() ;
    }
    If yes, how do I write it for a particular receiver?
    2. Can ESP-01 be programmed as one of the receivers?
    Thank you in anticipation.

    Reply
    • Hi David, I have done that using ESP-Now between two devices. The issue is, apparently, the ESP device cannot do ESP-Now and be linked to the WiFi simultaneously. The code I am running which I did not write switches between the two communication methods. I am testing by reading a BME280 sensor every 15 minutes. the Wemos D1 mini (battery powered) wakes up takes a reading sends it to a second Wemos D1 using ESP-Now, the second Wemos( uses a permanent power supply) receives the ESP_Now communication then switches to Wifi and sends the output via MQTT to NR. I think Ruis latest offering could be adapted to do a similar thing. However, what we need is a single receiver to collect data from multiple sensors and send it via MQTT. As far as I can see there is no reason why a many to one set up with the ‘one’ sending MQTT data would not work for sensors where the data is not required too frequently. This set up is ideal were battery powered sensors only need to supply data, say, evey 15 minutes.

      Reply
      • That is what I need. I want to monitor temperature, humidity and soil moisture for my irrigation system. I have a separate WiFi just for that system.

        Reply
        • Hi David, thats pretty much what I am doing. PC crashed at the moment, so I’m on my tablet. I can provide more info when I get the PC sorted. I would be interested how you implemented a separate wifi.
          Bob

          Reply
      • Probably your statement is wrong. AP (Software AP) and the hardware WiFi have differnet MAC addresses, so it is possible to do ESP-NOW and run WEB server on AP simoultaneously.

        Reply
  3. Hi Rui and Sara

    What is the maximum distance between sender and receiver with obstacles like wall etc and without obstacles like open room or hall.

    Thanks

    Reply
    • Hi Rajiv,

      with obstacles it highly depends on the nature of the obstacles.
      through an Iron-armed cement wall you might not get even through the wall even if you put the two device directly on the surface of the wall on both sides.

      If it is in open space with free sight it still depends somehow on how much 2,4 Ghz-Noise is in the air. I estimate 100 to 200 feet

      If you need a maximized range you could use LoRa (Long-range) modules

      Reply
  4. Hi Sara,

    if something follows a fixed pattern it is pre-determined to be done by a computer.
    MAC-Adresses are such a thing. So I wrote a “Print-MAC-Adresse-code that allows to just copy & paste a line of the serial monitor into your code replacing the ‘#’ symbol with a number and coding the line

    uint8_t broadcastAddress1[] = { 0x2C, 0x3A, 0xE8, 0x22, 0x5F, 0x70 };

    is done

    her it is

    #ifdef ESP32
    #include <WiFi.h>
    #else
    #include <ESP8266WiFi.h>
    #endif

    String MacAdrStr;
    char HexByteDigits[2];

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

    MacAdrStr = WiFi.macAddress();
    Serial.print(“ESP Board MAC Address: “);
    Serial.println(MacAdrStr);

    Serial.println();
    Serial.println(“copy the line below and replace the ‘#’ with a number”);
    Serial.println();
    Serial.print(“uint8_t broadcastAddress#[] = { “);
    for (uint8_t i = 0; i < 16; i = i + 3)
    { // 12:34
    HexByteDigits[0] = MacAdrStr[i];
    HexByteDigits[1] = MacAdrStr[i+1];
    Serial.print(“0x”);
    Serial.print(HexByteDigits);
    if (i < 14) Serial.print(“, “);
    }
    Serial.println(” };”);
    Serial.println();
    }

    void loop(){

    }

    best regards

    Stefan

    Reply
  5. Hi Sara,
    I want to add something. I tried to transmit strings in a code that is using ESP-NOW but it did not work. On my research to solve this problem I came across this websitehttps://hackingmajenkoblog.wordpress.com/2016/02/04/the-evils-of-arduino-strings/

    Which explains why you should AVOID Strings. They eat up all RAM over time causing the code to fail in very strange and hard to find ways. Code using Strings that does something repeatedly will lead straight into such problems. So I switched over to arrays of chars. It is a little more typing coding with arrays of chars but it is SECURE.
    In the ongoing research I discovered the library “PString” which offers (most) of the comfort Strings do but in a 100% SECURE way.

    Especcially with transmitting data over ESP-NW with structures I recommend using arrays of chars instead of Strings.

    best regards

    Stefan

    Reply
    • Hi Nahla,

      the ESP-NOW-libraries of ESP32 and ESP8266 are different in many points.
      It would be a lot of work to adapt ESP32-ESP-NOW-code to make it work on ESP8266
      registering a peer is completely different on ESP8266.

      So it is much better to start with a code that is written for ESP8266.

      as a short description: each send-command includes the MAC_ADRESS of the RECEIVING-board. It is very clear: If you send something you must define WHO is meant as receiver?

      therefore for each receiver you have to execute a function call

      esp_now_send (ESP_NOW_MAC_adrOfRecv, (uint8_t *) &myESP_NOW_Data, sizeof(myESP_NOW_Data));

      and setup the ESP_NOW_MAC_adrOfRecv to the right MAC-ADRESS.

      If you plan to do BI-directional communication via ESP-NOW AVOID the command delay() as the plaque.

      delay makes the CPU count up as fast as the CPU can which means there is no calculation-power left to manage receiving ESP-NOW-messages. If your receiving unit is executing a delay-command and a ESP-NOW-message arrives the transmission will fail.

      So you have to use NON-blocking timer-techniques that make use of the command millis().

      Next thing is AVOID String-variables ! The variable-type with the CAPITAL “S”
      Strings eat up all memory over time and start overwrite your code which means your code MUST crash. It may take days or weeks but your code WILL crash.

      use array of char or “PStrings” instead.

      best regards

      Stefan

      Reply
    • Hi Nahla,

      so I have written a send / receive-demo-code which explains in the comments not all but a lot of details.

      Additionaly this demo-code includes code for non-blocking timers and some useful “tools”

      printout filename of the sketch including the path and timestamp date and time of compiling right before upload
      Print WiFi-Macadress in a line of code which is ready to mark, copy & paste into your code
      demonstrating the use of PString which is a highly-secure alternative for Strings
      democode that shows how to add integers and floats to the ESP-NOW-Data that shall be sended

      variabletype String is unsecure and often will make your code crash

      So this democode should answer most (surely not all) questions how to use ESP-NOW to send and receive bi-directional between two or up to 20 ESP8266-boards

      I have tested this code with a WeMos D1 mini and a GeekCreit nodeMCU
      The only things that have to be adapted is the MAC-adresses and the datastructure you want to send over ESP-NOW to match your needs

      So I would like to read from you what questions you still have regarrding my democode or if you successfully adapted it to your projects

      As this Comment-function of RNT does not allow inserting formatted code I uploaded the democode as an attachment to this post on
      Arduino.cc

      …. hm trying to reach the Arduino-Forum I get an error Error 504 Ray ID: 594fdaa05b4d9736 • 2020-05-17 19:49:06 UTC
      Gateway time-out

      So I posted my code here

      https://www.arduinoforum.de/arduino-Thread-ESP8266-Democode-bi-directional-send-receive-using-ESP-NOW

      best regards

      Stefan

      Reply
    • Hi.
      I haven’t tried it, but the documentation mentions:
      – Limited encrypted peers. 10 encrypted peers at the most are supported in Station mode; 6 at the most in SoftAP or SoftAP + Station mode;
      – Multiple unencrypted peers are supported, however, their total number should be less than 20, including encrypted peers;
      Regards,
      Sara

      Reply
    • Hi.
      This is what is mentioned in the documentation:
      Limited encrypted peers. 10 encrypted peers at the most are supported in Station mode; 6 at the most in SoftAP or SoftAP + Station mode;
      Multiple unencrypted peers are supported, however, their total number should be less than 20, including encrypted peers;
      Regards,
      Sara

      Reply

Leave a Comment

Download our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.