ESP-NOW Two-way communication problem

Hello everyone, I have a question about ESP-NOW.
I am currently building a two way communication between two ESP32 boards.

What the code should do:

The first ESP should create a random Number for the variable called "pod" then send it to the second ESP. When the second ESP recieves the Number he checks if the number he recieved is the same number as a number which is assigned to the second ESP called "pod_ID".
If thats the case he should wait for a touch sensor to detect a signal.
When a signal is detected he should send a message with a random number called "reactiontime" back to the first ESP.

My Problem:

The issue is that i dont want the ESP thats waiting for a message coming in from the other ESP, to just keep on running through the loop().
I tried to solve it with a while loop kinda like:

while(/* no data recieved*/ )
  {
    //do nothing
  }

But the issue with this is that I am not sure what or where I would declare a "recieve status" for a recieved data packet.

I hope the explaination of what my issue is, was understandable.
I would really appreciate any help.

The code for the first ESP is:

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

//define pod number
const int pod_ID = 1;

//Receiver 
//uint8_t broadcastAddress[] = {0x08, 0x3A, 0xF2, 0xAC, 0xE0, 0xFC};
uint8_t broadcastAddress[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};

// Define variables to be sent
int pod;

// Define variables to store incoming readings
float incoming_reactionTime;

// Variable to store if sending data was successful
String success;

//Structure example to send data
//Must match the receiver structure
typedef struct message_send 
{
  int rPod; 
} message_send;

typedef struct message_in 
{
  float rTime;
} message_in;

// Create a message to hold incoming readings
message_in incomingReadings;
// Create a struct_message the data to be sent
message_send dataToSend;

//troubleshooting / make peerinfo a global variabel
esp_now_peer_info_t peerInfo;

// Callback when data is 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");
  if (status ==0)
    {
      success = "Delivery Success";
    }
  else
    {
      success = "Delivery Fail";
    }
}

// Callback when data is received
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) 
{
  char macStr[18];
  Serial.print("Packet received from: ");
  snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x",
           mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  Serial.println(macStr);
  
  memcpy(&incomingReadings, incomingData, sizeof(incomingReadings));
  Serial.print("Bytes received: ");
  Serial.println(len);
  incoming_reactionTime = incomingReadings.rTime;
}

void showData()
{
  Serial.println("_______________________________");
  Serial.println("INCOMING READINGS:");
  Serial.print("Pod number: ");
  Serial.print(dataToSend.rPod);
  Serial.println();
  Serial.print("Time: ");
  Serial.print(incoming_reactionTime);
  Serial.println();
}

void setup() {
  // Init Serial Monitor
  Serial.begin(115200);
  
  //while the serial stream is not open, do nothing
  while(!Serial);
  
  //led pin
  pinMode(2, OUTPUT);

  // 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 Send CB to
  // get the status of Transmitted 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");
    return;
  }
  // Register for a callback function that will be called when data is received
  esp_now_register_recv_cb(OnDataRecv);
}

void loop() 
{
  while(/* no data recieved*/ )
  {
    //do nothing
  }

  // Recieve message via ESP-NOW  (not sure where this needs to be called)
  esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &incomingReadings, sizeof(incomingReadings));

  //define Variables to be send
  dataToSend.rPod = random(1,4);
  
  if(dataToSend.rPod == pod_ID) 
  { 
    do{
      //turn on LED
    }while(touchRead(4) >= 50);
  }
  
  //Send message via ESP-NOW
  esp_err_t sending = esp_now_send(broadcastAddress, (uint8_t *) &dataToSend, sizeof(dataToSend));

  showData();
}

Here is the code for the second ESP:

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

//define pod number
const int pod_ID = 2;

//Receiver 
//uint8_t broadcastAddress[] = {0x9C, 0x9C, 0x1F, 0xE9, 0xC5, 0xFC}; //{0x08, 0x3A, 0xF2, 0xAC, 0xE0, 0xFC}
uint8_t broadcastAddress[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};

// Define variables to be sent
float reactionTime;

// Define variables to store incoming readings
int incoming_pod;

// Variable to store if sending data was successful
String success;

//Structure to send data
//Must match the receiver structure
typedef struct message_send 
{
  float rTime; 
} message_send;

typedef struct message_in 
{
  int rPod;
} message_in;

// Create a struct_message to hold incoming readings
message_in incomingReadings;

// Create a struct_message the data to be sent
message_send dataToSend;

//troubleshooting / make peerinfo a global variabel
esp_now_peer_info_t peerInfo;

// Callback when data is 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");
  if (status ==0)
    {
      success = "Delivery Success";
    }
  else
    {
      success = "Delivery Fail";
    }
}

// Callback when data is received
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) 
{
  char macStr[18];
  Serial.print("Packet received from: ");
  snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x",
           mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  Serial.println(macStr);
  
  memcpy(&incomingReadings, incomingData, sizeof(incomingReadings));
  Serial.print("Bytes received: ");
  Serial.println(len);
  incoming_pod = incomingReadings.rPod;
  }

void showData()
{
  Serial.println("_______________________________");
  Serial.println("INCOMING READINGS:");
  Serial.print("Pod number: ");
  Serial.print(incoming_pod);
  Serial.println();
  Serial.print("Time: ");
  Serial.print(dataToSend.rTime);
  Serial.println();
}
  
void setup() {
  // Init Serial Monitor
  Serial.begin(115200);
  
  //while the serial stream is not open, do nothing
  while(!Serial);

  //led pin
  pinMode(2, OUTPUT);

  // 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 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");
    return;
  }
  // Register for a callback function that will be called when data is received
  esp_now_register_recv_cb(OnDataRecv);
}

void loop() 
{
  while(/* no data recieved*/ )
  {
    //do nothing
  }
    
  // Recieve message via ESP-NOW  (not sure where this needs to be called)
  esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &incomingReadings, sizeof(incomingReadings));

  //define Variables to be send
  dataToSend.rTime = random(1,4001);

  if(incoming_pod == pod_ID) 
  { 
    do{
      //turn on LED
    }while(touchRead(4) >= 50);
  }

  // Send message via ESP-NOW
  esp_err_t sending = esp_now_send(broadcastAddress, (uint8_t *) &dataToSend, sizeof(dataToSend));
 
  showData();
}

If I count right im summary these are three microcontrollers

  1. The first ESP should create a random Number for the variable called "pod"
  2. send it to the second ESP.
  3. When the second ESP recieves the Number he checks if the number he recieved is the same number as a number which is assigned to the second ESP called "pod_ID".

If not describe everything new with more clarity

Same thing here

You haven't yet understood that the function

OnDataRecv

is called as a callback-function when data is received
Callback means this function is called just by the receiving without any function-call that is written by your code.
From the perspective of the code you write "it just happends" similar to an interrupt

All in all you should describe the pure functionality by avoiding all programming-terms and describing it in normal words.

Describe it as you would describe it to your grandma. Zero programming-words just everyday language-words. You have some misconceptions about programming and to make sure to describe the real wanted functionality
describe the functionality with everyday normal words.

In Addition describe the context why you need this functionality by giving an overview about your project.

best regards Stefan

Thank you for the quick response Stefan.

1. Quick overview of my project:

I have several "Pods" (each pod is an ESP). There is one master Pod and and the goal is to be have several slave Pods (I am not sure about the amount yet but at right now I am trying to build the code for 1 Master and one Slave).
I will refer to the master Pod as Pod_1 and to the slave Pod as Pod_2

When the program is uploaded, a randomly selected Pod (in this case either Pod_1 or Pod_2) should turn on their LED (but not both at the same time).
Lets say in this case Pod_2 turns the LED on. The LED on Pod_2 should stay on until the touch-senoser from Pod_2 detects a signal. While Pod_2 is doing all that Pod_1 should just wait and not do anything (my callback function is probably why this part does not work).
After the signal is detectet a new randomly selected Pod should turn on its LED and the whole process repeats.

2. I have question about the callback function.

The callback function gets called everytime a message is recieved?
So the loop() is doing whatever the code tells it to do and while thats happening the callback function can be called anytime?
If I understood that correct, then thats not what I need for my code. Do you know any solution for sending and recieving data without a callback function?

3. What I am also not exactly sure about is what this part of the code does?

  // Send message via ESP-NOW
  esp_err_t sending = esp_now_send(broadcastAddress, (uint8_t *) &dataToSend, sizeof(dataToSend));

  // Recieve message via ESP-NOW
  esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &incomingReadings, sizeof(incomingReadings));

From my understanding the esp_now_send() function returns a variable from the type esp_err_t.
Is esp_err_t the datatype for the message thats beeing sent? Like this one:

typedef struct message_send 
{
  float rTime; 
} message_send;

I am still pretty new to the ESP-NOW protocol, so I have a lot of question, but I can´t find any good ressources on ESP-NOW. Do you have any idea, on where I could find something?

Best regards Markus

1 Like

Hi Markus,

the name that is written as the parameter inside the
function
esp_now_register_recv_cb()

is that part of the code that will be executed when a ESP-NOW-message is received.

in C++-programming any part of the code that starts like

void myFirstFunc(.....

void mySecondBlaBlbla(.....

int myThirdHubbaBubba(....

is a function.

That is the reason why the next explanation uses the word "function" so often

the name that is written as the parameter inside the
function esp_now_register_recv_cb() becomes the callback-function

In your code the function

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

becomes the callback-function

If doing a lot of things inside the callback-function does not fit your needs the programming works this way:

inside the callback-function you just store the received-message and set a flag-variable that data has been received. And anything else is done outside the callback-function.

But there must be a callback-function because you can not sync your receiver-code to be ready to receive the data directly if a message rushes in.

You can imagine this as a the man working at the hotel-reception

If a phone-call comes in for a guest and the guest is not ready to take over the phone himself the reception-man will write down a note on a sheet of paper and laying the paper on his desk.
Some time later if the guest walks to the reception the reception-man will say "Mr. Miller" there is a message for you and wil hand over the message.

The callback-function is equal to writing the message on the sheet of paper

saying "there is a message for you" is equal to set a flag-variable to value "message received"

The return-value stored into variable sending is just a number it is NOT the message itself.
The message is stored into variable incomingReadings

 // Send message via ESP-NOW
  esp_err_t sending = esp_now_send(broadcastAddress, (uint8_t *) &dataToSend, sizeof(dataToSend));

The lines below are nonsense

// Recieve message via ESP-NOW
  esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &incomingReadings, sizeof(incomingReadings));

The function esp_now_send always sends data as the functions name says it esp_now_send

The receiving is always done inside the receiver-callback-function.
Pay attention there is one call-backfunction for "sending data has finished"
and a second callback-function for receiving data

You can give this tutorial a try if you understand it

This tutorial is pretty detailed.

Here is a demo-code that needs the library PString to be installed.

// WARNING !! Trying to adapt this ESP32-code using ESP-NOW to a ESP8266 is a big hassle 
// as the ESP8266-ESP-NOW-library works very differently using other functions etc.
// for coding with an ESP8266 start with a code that is written for ESP8266 DON'T try to adapt an ESP32-code


//WiFiLib and Esp-NowLib espnowlib 
#include <WiFi.h>    // ESP32 needs "WiFi.h"     ESP8266 needs "ESP8266WiFi.h"
#include <esp_now.h> // ESP32 needs "esp_now.h"  ESP8266 needs "espnow.h" do you recognize the difference?

#define CHANNEL 0

boolean gb_SerialOutput_enabled = true;

using MacAdr = uint8_t[6];

// the code must know all receivers MAC-adresses that THIS boards wants to send data to
MacAdr ESP_NOW_MAC_adrOfRecv1  { 0x24, 0x0A, 0xC4, 0x06, 0xC3, 0x64 };
MacAdr ESP_NOW_MAC_adrOfRecv2  { 0x24, 0x0A, 0xC4, 0x02, 0xBC, 0x20 };
MacAdr ESP_NOW_MAC_adrOfRecv3  { 0x24, 0x6F, 0x28, 0x22, 0x62, 0xFC };

MacAdr SendersOwnMacAdress     { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
void SetupOwnMacAdress(){
  memcpy (SendersOwnMacAdress, ESP_NOW_MAC_adrOfRecv3, sizeof(SendersOwnMacAdress) );
}

esp_now_peer_info_t peerInfo;

void RegisterPeer(const MacAdr &MyMac_Adress_array) {
  memcpy(peerInfo.peer_addr, MyMac_Adress_array, sizeof(peerInfo.peer_addr) );
  peerInfo.channel = 0;  
  peerInfo.encrypt = false;

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

//nbt nonblockingtimer 
boolean TimePeriodIsOver (unsigned long &expireTime, unsigned long TimePeriod) {
  unsigned long currentMillis  = millis();
  if ( currentMillis - expireTime >= TimePeriod )
  {
    expireTime = currentMillis; // set new expireTime
    return true;                // more time than TimePeriod) has elapsed since last time if-condition was true
  } 
  else return false;            // not expired
}

unsigned long BlinkTimer = 0;
const int     OnBoardLED = 2;

unsigned long SendDataTimer = 0;

// pslib pstringlib
#include <PString.h> // the use of PString avoids code-malfunction like Strings can do
char    TestESP_CmdStr_AoC[64];
PString TestESP_CmdStr_PS(TestESP_CmdStr_AoC, sizeof(TestESP_CmdStr_AoC) );

// pfndt
void PrintFileNameDateTime() {
  Serial.print("Code running comes from file ");  Serial.println(__FILE__);
  Serial.print("compiled ");  Serial.print(__DATE__);  Serial.print(" ");  Serial.println(__TIME__);
}

// pma pwma
void PrintWiFiMacAdress() {
  char MacAdr_AoC[18];  //suffix _AoC for easier remembering variable-type is ArrayOfChar
  char HexByteDigits[3];

  for (uint8_t i = 0; i < 18; i = i + 1){
    MacAdr_AoC[i] = WiFi.macAddress()[i];
  }
  MacAdr_AoC[17] = 0; // zero to terminate the string
  Serial.print("ESP Board Wifi.macAddress:  ");
  Serial.println(MacAdr_AoC);

  Serial.println();
  Serial.println("copy the line below and replace the '#' with variablename of your choice");
  Serial.println();
  Serial.print("uint8_t #[] = { ");
  for (uint8_t i = 0; i < 16; i = i + 3)
  {
    HexByteDigits[0] = MacAdr_AoC[i];
    HexByteDigits[1] = MacAdr_AoC[i + 1];
    HexByteDigits[2] = 0; // zero for terminating the string
    Serial.print("0x");
    Serial.print(HexByteDigits);
    if (i < 14) Serial.print(", ");
  }
  Serial.println(" };");
  Serial.println();
}

// espnowmsg espnowbytes enbytes espstruc
// Structure example to send data. Must match the receiver structure
typedef struct MyESP_NOW_Data_type {
  char  MyESP_NOW_SenderID_AoC[16];
  char  MyESP_NOW_StatusMsg_AoC[64];
  int   MyESP_NOW_Int;
  float MyESP_NOW_Float;
} MyESP_Data_type;

MyESP_NOW_Data_type ESP_Data_Received;
MyESP_NOW_Data_type ESP_Data_ToSend;


// Init ESP Now with restart in case something went wrong
void InitESPNow() {
  if (gb_SerialOutput_enabled)
  { if (esp_now_init() == ESP_OK) 
    { Serial.println("ESPNow Init Success"); }
    else 
    { Serial.println("ESPNow Init Failed");
      ESP.restart(); //there was an error try again through restarting the board
    }
  }  
}


void ESP_NOW_Setup() {
  WiFi.mode(WIFI_STA); //Set device in STA mode to begin with
  WiFi.disconnect();   // for strange reasons WiFi.disconnect(); is nescessary to make ESP-NOW work
  if (gb_SerialOutput_enabled)
    { Serial.println("WiFi.mode(WIFI_STA WiFi.disconnect() done");}

  InitESPNow();   // Init ESPNow with a fallback logic
  esp_now_register_send_cb(OnDataSent);
  
  esp_now_register_recv_cb(OnDataRecv);

  RegisterPeer(ESP_NOW_MAC_adrOfRecv1);
  RegisterPeer(ESP_NOW_MAC_adrOfRecv2);
  RegisterPeer(ESP_NOW_MAC_adrOfRecv3);
}


void PrintESP_Status(esp_err_t result) {
  Serial.print("Send Status: ");

  if (result == ESP_OK)
    { Serial.println("sended"); }
  else if (result == ESP_ERR_ESPNOW_NOT_INIT)
    { Serial.println("Master ESPNOW not Init."); }
  else if (result == ESP_ERR_ESPNOW_ARG)
    { Serial.println("Master Invalid Argument"); }
  else if (result == ESP_ERR_ESPNOW_INTERNAL)
    { Serial.println("Master Internal Error");}
  else if (result == ESP_ERR_ESPNOW_NO_MEM)
    { Serial.println("Master ESP_ERR_ESPNOW_NO_MEM"); }
  else if (result == ESP_ERR_ESPNOW_NOT_FOUND)
    { Serial.println("Master Peer not found."); }
  else 
    {Serial.println("Master Not sure what happened"); }
}

void SendData() {
  if (gb_SerialOutput_enabled)
    { Serial.println("sendData() Start");}
  esp_err_t result;  

  if(memcmp(ESP_NOW_MAC_adrOfRecv1, SendersOwnMacAdress, sizeof(SendersOwnMacAdress) ) != 0) { // if MAC-Adess is not the own MAC-Adress
    result = esp_now_send(ESP_NOW_MAC_adrOfRecv1, (uint8_t *) &ESP_Data_ToSend, sizeof(ESP_Data_ToSend));
    // if sending has finished function OnDataSent is called  
  }
  
  if(memcmp(ESP_NOW_MAC_adrOfRecv2, SendersOwnMacAdress, sizeof(SendersOwnMacAdress) ) != 0) {
    result = esp_now_send(ESP_NOW_MAC_adrOfRecv2, (uint8_t *) &ESP_Data_ToSend, sizeof(ESP_Data_ToSend));
    // if sending has finished function OnDataSent is called
  }

  if(memcmp(ESP_NOW_MAC_adrOfRecv3, SendersOwnMacAdress, sizeof(SendersOwnMacAdress) ) != 0) {
    result = esp_now_send(ESP_NOW_MAC_adrOfRecv3, (uint8_t *) &ESP_Data_ToSend, sizeof(ESP_Data_ToSend));
    // if sending has finished function OnDataSent is called  
  }
}


// callback-function that is executed when data was send to another ESP32-board
// parameter status contains info about the success or fail of the transmission
void OnDataSent(const uint8_t *Receivers_mac_addr, esp_now_send_status_t status) {
  char Receivers_macStr[18];
  snprintf(Receivers_macStr, sizeof(Receivers_macStr), "%02x:%02x:%02x:%02x:%02x:%02x",Receivers_mac_addr[0], Receivers_mac_addr[1], Receivers_mac_addr[2], Receivers_mac_addr[3], Receivers_mac_addr[4], Receivers_mac_addr[5]);

  if (gb_SerialOutput_enabled)
  {  
    Serial.print("callback-OnDataSent ");
    Serial.print("ESP32-Board Last Packet Sent to: ");
    Serial.println(Receivers_macStr);
    Serial.print("ESP32-Board1 Last Packet Send Status: ");
    Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Failed");
    Serial.println();
    Serial.println();
  }  
}

// callback-function that is executed when a ESP-NOW-Message is received
void OnDataRecv(const uint8_t *Senders_mac_addr, const uint8_t *receivedBytes, int NoOfreceivedBytes) {
  memcpy(&ESP_Data_Received, receivedBytes, sizeof(ESP_Data_Received));

  Serial.println("callback-OnDataRecv ");
  char Senders_macStr[18]; //MAC-Adress of sender
  snprintf(Senders_macStr, sizeof(Senders_macStr), "%02x:%02x:%02x:%02x:%02x:%02x", Senders_mac_addr[0], Senders_mac_addr[1], Senders_mac_addr[2], Senders_mac_addr[3], Senders_mac_addr[4], Senders_mac_addr[5]);
  Serial.print("received from MAC-Adress");
  Serial.println(Senders_macStr);

  Serial.println("receievd data:"); 
  Serial.print("SenderID #");
  Serial.print(ESP_Data_Received.MyESP_NOW_SenderID_AoC);
  Serial.println("#");

  Serial.print("StatusMsg #");
  Serial.print(ESP_Data_Received.MyESP_NOW_StatusMsg_AoC);
  Serial.println("#");
  
  Serial.print("Integer:");
  Serial.print(ESP_Data_Received.MyESP_NOW_Int);
  Serial.println();
  
  Serial.print("Float:");
  Serial.print(ESP_Data_Received.MyESP_NOW_Float);
  Serial.println();

  Serial.println();
  Serial.println();
}


void SetupMessageToSend() {
                                                                                                            //1
  if(memcmp(ESP_NOW_MAC_adrOfRecv1, SendersOwnMacAdress, sizeof(SendersOwnMacAdress) ) == 0) { // if MAC-Adess1 IS the own MAC-Adress
    memcpy(ESP_Data_ToSend.MyESP_NOW_SenderID_AoC,  "ESP32-Board1111",  sizeof(ESP_Data_ToSend.MyESP_NOW_SenderID_AoC) );
    memcpy(ESP_Data_ToSend.MyESP_NOW_StatusMsg_AoC, " I am Board No 1", sizeof(ESP_Data_ToSend.MyESP_NOW_StatusMsg_AoC) );
    ESP_Data_ToSend.MyESP_NOW_Int = ESP_Data_ToSend.MyESP_NOW_Int + 1;
    ESP_Data_ToSend.MyESP_NOW_Float = 111.111; 
  }
                                                                                                            //2 
  if(memcmp(ESP_NOW_MAC_adrOfRecv2, SendersOwnMacAdress, sizeof(SendersOwnMacAdress) ) == 0) { // if MAC-Adess2 IS the own MAC-Adress
    memcpy(ESP_Data_ToSend.MyESP_NOW_SenderID_AoC,  "ESP32-Board2222",  sizeof(ESP_Data_ToSend.MyESP_NOW_SenderID_AoC) );
    memcpy(ESP_Data_ToSend.MyESP_NOW_StatusMsg_AoC, " I am Board No 2", sizeof(ESP_Data_ToSend.MyESP_NOW_StatusMsg_AoC) );
    ESP_Data_ToSend.MyESP_NOW_Int = ESP_Data_ToSend.MyESP_NOW_Int + 2;
    ESP_Data_ToSend.MyESP_NOW_Float = 222.222; 
  }
                                                                                                            //3
  if(memcmp(ESP_NOW_MAC_adrOfRecv3, SendersOwnMacAdress, sizeof(SendersOwnMacAdress) ) == 0) { // if MAC-Adess3 IS the own MAC-Adress
    memcpy(ESP_Data_ToSend.MyESP_NOW_SenderID_AoC,  "ESP32-Board3333",  sizeof(ESP_Data_ToSend.MyESP_NOW_SenderID_AoC) );
    memcpy(ESP_Data_ToSend.MyESP_NOW_StatusMsg_AoC, " I am Board No 3", sizeof(ESP_Data_ToSend.MyESP_NOW_StatusMsg_AoC) );
    ESP_Data_ToSend.MyESP_NOW_Int = ESP_Data_ToSend.MyESP_NOW_Int + 3;
    ESP_Data_ToSend.MyESP_NOW_Float = 333.333; 
  }  
}


void setup() {
  Serial.begin(115200);
  if (gb_SerialOutput_enabled)
    { PrintFileNameDateTime(); }
    
  ESP_NOW_Setup();
  if (gb_SerialOutput_enabled)
    { PrintWiFiMacAdress(); }

  pinMode(OnBoardLED,OUTPUT);
}

void loop()
{
  if ( TimePeriodIsOver(BlinkTimer,500) ) {
    digitalWrite (OnBoardLED, !digitalRead(OnBoardLED) );
  }

  if ( TimePeriodIsOver(SendDataTimer,2000) ) {
    SetupMessageToSend();
    SendData();
  }  
}

best regards Stefan

2 Likes

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.