user51226: Originally I was creating a project consisting of the Atmega328P and NRF24L01 to create multiple arduinos that could talk to each other.  Basically I was creating something like this:

As the network grows I am starting to run out of memory when using the atmega328P. I have to be careful about allocating memory and more. So I will like to replace the NRF24L01 with the ESP32.  The price of the ESP-WROOM-32 by itself is also cheaper than buying an NRF24L01 + the atmega328.

Question
How can I send data from one ESP32 to another ESP32 without establishing a WiFi connection? Just like the NRF24L01 where you specify a writting pipe where you want to send the data and the other NRF24L01 is listening on that pipe.

I know I can do that with ESP-NOW. This video explains how. The problem with ESP NOW is that I have to know in advance the mac address of the ESP32 where I intend to send a message. I plan to be able to keep adding smart home devices without having to know the mac address.
My algorithm with NRF24 that I already created is as follows:

There is a master node listening on address 2
If I add a new  node A to the network it listens on address 1.
Master discovers all devices that have address 1 every few seconds.
So eventually node A receives a discovery packet message that comes from master. Node A listens now on a random address and replies to master with that random anddress.
So node A is now connected to the network.

The cool thing about this algorithm is that I can keep adding new nodes to the network without knowing in advance its address. I will like to do the same thing with the ESP32. How can I send a simple message betweenn 2 ESP32 modules?

user51226: Finally I found a way how to do it. If you use the mac address FF:FF:FF:FF:FF:FF that works as a broadcast address. This is the working code on how to send a message from the master to another esp32 without knowing its mac address. Later the slave can reply with its real mac address:
Code for master (tx)
#include <Arduino.h>
#include <esp_now.h>
#include "WiFi.h"

uint8_t broadcastAddress[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};

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

  Serial.println();
}       

void setup()
{

  Serial.begin(115200);

  Serial.println("Starting...\n");

  // Set device as a Wi-Fi Station
  WiFi.mode(WIFI_STA);

  Serial.println(WiFi.macAddress());

  // Init ESP-NOW
  if (esp_now_init() != ESP_OK) {
    Serial.println("Error initializing ESP-NOW");
    for(;;) {      delay(1);  } // do not initialize wait forever
  }

  Serial.println("initialized ESP-NOW");


  // Once ESPNow is successfully Init, we will register for Send CB to
  // get the status of Trasnmitted packet
  esp_now_register_send_cb(OnDataSent);

   // Register peer
  esp_now_peer_info_t peerInfo;
  memcpy(peerInfo.peer_addr, broadcastAddress, 6);
  peerInfo.channel = 0;  
  peerInfo.encrypt = false;

   // Add peer        
  if (esp_now_add_peer(&peerInfo) != ESP_OK){
    Serial.println("Failed to add peer");
    for(;;) {      delay(1);  } // do not initialize wait forever
  }            
}

int dataToSend = 1;
void loop()
{
    dataToSend++;

    // Send message via ESP-NOW (size of an int is 4 bytes on ESP32)
    esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &dataToSend, 4);
   
    if (result == ESP_OK) {
      Serial.println("Sent with success");
    }
    else {
      Serial.println("Error sending the data");
    }

    //Serial.println(WiFi.macAddress());    
    delay(1000);
}

Code for slave (Rx)
#include <Arduino.h>
#include <esp_now.h>
#include "WiFi.h"


// Callback when data is received
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) 
{
  int dataReceived;
  memcpy(&dataReceived, incomingData, sizeof(dataReceived));

  Serial.print("Data Received: ");
  Serial.println(dataReceived);

  Serial.println();    
}

void setup()
{

  Serial.begin(115200);
  delay(100);

  Serial.println("Starting...\n");

  // Set device as a Wi-Fi Station
  WiFi.mode(WIFI_STA);

  // print our mac address
  Serial.println(WiFi.macAddress());

  // Init ESP-NOW
  if (esp_now_init() != ESP_OK) {
    Serial.println("Error initializing ESP-NOW");
    for(;;) {      delay(1);  } // do not initialize wait forever
  }
  
  Serial.println("initialized ESP-NOW");



  // Register for a callback function that will be called when data is received
  esp_now_register_recv_cb(OnDataRecv);

}

void loop()
{
}

You can also reply back on the receiver code if you want. It will be the same code.

51226: <p>Originally I was creating a project consisting of the Atmega328P and NRF24L01 to create multiple arduinos that could talk to each other.  Basically I was creating something like this:</p>
<p><a href="https://i.stack.imgur.com/0MBQ4.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/0MBQ4.png" alt="enter image description here" /></a></p>
<p>As the network grows I am starting to run out of memory when using the atmega328P. I have to be careful about allocating memory and more. So I will like to replace the NRF24L01 with the ESP32.  The price of the ESP-WROOM-32 by itself is also cheaper than buying an NRF24L01 + the atmega328.</p>
<hr />
<h2>Question</h2>
<p><strong>How can I send data from one ESP32 to another ESP32 without establishing a WiFi connection?</strong> Just like the NRF24L01 where you specify a writting pipe where you want to send the data and the other NRF24L01 is listening on that pipe.</p>
<hr />
<p>I know I can do that with ESP-NOW. <a href="https://www.youtube.com/watch?v=qxwXwNS3Avw" rel="nofollow noreferrer">This video explains how</a>. The problem with ESP NOW is that I have to know in advance the mac address of the ESP32 where I intend to send a message. I plan to be able to keep adding smart home devices without having to know the mac address.</p>
<p>My algorithm with NRF24 that I already created is as follows:</p>
<ol>
<li>There is a master node listening on address 2</li>
<li>If I add a new  node <code>A</code> to the network it listens on address 1.</li>
<li>Master discovers all devices that have address 1 every few seconds.</li>
<li>So eventually node <code>A</code> receives a discovery packet message that comes from master. Node <code>A</code> listens now on a random address and replies to master with that random anddress.</li>
<li>So node <code>A</code> is now connected to the network.</li>
</ol>
<p>The cool thing about this algorithm is that I can keep adding new nodes to the network without knowing in advance its address. I will like to do the same thing with the ESP32. <strong>How can I send a simple message betweenn 2 ESP32 modules?</strong></p>
 51226: <p>Finally I found a way how to do it. If you use the mac address FF:FF:FF:FF:FF:FF that works as a broadcast address. This is the working code on how to send a message from the master to another esp32 without knowing its mac address. Later the slave can reply with its real mac address:</p>
<p><strong>Code for master (tx)</strong></p>
<pre><code>#include &lt;Arduino.h&gt;
#include &lt;esp_now.h&gt;
#include &quot;WiFi.h&quot;

uint8_t broadcastAddress[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};

void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
  Serial.println(status == ESP_NOW_SEND_SUCCESS ? &quot;Delivery Success 1&quot; : &quot;Delivery Fail 1&quot;);
  if (status ==0){
    Serial.println(&quot;Delivery Success 2&quot;);
  }
  else{
    Serial.println(&quot;Delivery fail 2&quot;);
  }

  Serial.println();
}       

void setup()
{

  Serial.begin(115200);

  Serial.println(&quot;Starting...\n&quot;);

  // Set device as a Wi-Fi Station
  WiFi.mode(WIFI_STA);

  Serial.println(WiFi.macAddress());

  // Init ESP-NOW
  if (esp_now_init() != ESP_OK) {
    Serial.println(&quot;Error initializing ESP-NOW&quot;);
    for(;;) {      delay(1);  } // do not initialize wait forever
  }

  Serial.println(&quot;initialized ESP-NOW&quot;);


  // Once ESPNow is successfully Init, we will register for Send CB to
  // get the status of Trasnmitted packet
  esp_now_register_send_cb(OnDataSent);

   // Register peer
  esp_now_peer_info_t peerInfo;
  memcpy(peerInfo.peer_addr, broadcastAddress, 6);
  peerInfo.channel = 0;  
  peerInfo.encrypt = false;

   // Add peer        
  if (esp_now_add_peer(&amp;peerInfo) != ESP_OK){
    Serial.println(&quot;Failed to add peer&quot;);
    for(;;) {      delay(1);  } // do not initialize wait forever
  }            
}

int dataToSend = 1;
void loop()
{
    dataToSend++;

    // Send message via ESP-NOW (size of an int is 4 bytes on ESP32)
    esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &amp;dataToSend, 4);
   
    if (result == ESP_OK) {
      Serial.println(&quot;Sent with success&quot;);
    }
    else {
      Serial.println(&quot;Error sending the data&quot;);
    }

    //Serial.println(WiFi.macAddress());    
    delay(1000);
}
</code></pre>
<p><strong>Code for slave (Rx)</strong></p>
<pre><code>#include &lt;Arduino.h&gt;
#include &lt;esp_now.h&gt;
#include &quot;WiFi.h&quot;


// Callback when data is received
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) 
{
  int dataReceived;
  memcpy(&amp;dataReceived, incomingData, sizeof(dataReceived));

  Serial.print(&quot;Data Received: &quot;);
  Serial.println(dataReceived);

  Serial.println();    
}

void setup()
{

  Serial.begin(115200);
  delay(100);

  Serial.println(&quot;Starting...\n&quot;);

  // Set device as a Wi-Fi Station
  WiFi.mode(WIFI_STA);

  // print our mac address
  Serial.println(WiFi.macAddress());

  // Init ESP-NOW
  if (esp_now_init() != ESP_OK) {
    Serial.println(&quot;Error initializing ESP-NOW&quot;);
    for(;;) {      delay(1);  } // do not initialize wait forever
  }
  
  Serial.println(&quot;initialized ESP-NOW&quot;);



  // Register for a callback function that will be called when data is received
  esp_now_register_recv_cb(OnDataRecv);

}

void loop()
{
}
</code></pre>
<p>You can also reply back on the receiver code if you want. It will be the same code.</p>
