Testcode for two ESP8266-boards transferring data with the ESP-NOW-protocol

damm

selected accidentically all and posted code without code brackets
the comments around the code are losr

the code below uses non-blocking timers
the code for this is posted here

#include <ESP8266WiFi.h> // for use with ESP32 use file "WiFi.h"
#include <espnow.h>      // for use with ESP32  use file "esp_now.h" (underline in the middle)

#include <nonBlocking_Timer_class.h>
const byte Dummy = 1;
nonBlocking_Timer nbTimer(Dummy); // create object of class

int SendDataTimer;

// Amica-nodeMCU
// { 0x5C, 0xCF, 0x7F, 0x00, 0xD2, 0x9C };

// Wemos-D1-Mini 
// {0x18, 0xFE, 0x34, 0xCE, 0xD2, 0xBB};

uint8_t ESP_NOW_MAC_adrOfRecv[] = { 0x5C, 0xCF, 0x7F, 0x00, 0xD2, 0x9C };

// Structure example to send data. Must match the receiver structure
typedef struct MyESP_NOW_Data_type {
  char MyESP_NOW_MsgStr[65];
  int  MyESP_NOW_Int;
} MyESP_Data_type;

// Create a "variable" called myESP_NOW_Data of variable-type MyESP_NOW_Data_type
MyESP_NOW_Data_type myESP_NOW_Data;

void ESP_NOW_SendData()
{
  // Set values to send
  strcpy(myESP_NOW_Data.MyESP_NOW_MsgStr, "My ESP-Now Message");
  myESP_NOW_Data.MyESP_NOW_Int = myESP_NOW_Data.MyESP_NOW_Int + 1; 

  // Send message via ESP-NOW  
  esp_now_send(ESP_NOW_MAC_adrOfRecv, (uint8_t *) &myESP_NOW_Data, sizeof(myESP_NOW_Data));
  Serial.println("esp_now_send(ESP_NOW_MAC_adrOfRecv, (uint8_t *) &myESP_NOW_Data, sizeof(myESP_NOW_Data)); done");
  // if sending has finished function OnDataSent is called

}


void ESP_Now_setup()
{
  WiFi.mode(WIFI_STA);
  Serial.println("WiFi.mode(WIFI_STA); done");
  WiFi.disconnect(); // for strange reasons WiFi.disconnect() makes ESP-NOW work
  Serial.println("WiFi.disconnect(); done");
  
  // Init ESP-NOW
  if (esp_now_init() != 0) 
  {
    Serial.println("Error initializing ESP-NOW");
    return;
  }
  Serial.println("esp_now_init() was successful");

  esp_now_set_self_role(ESP_NOW_ROLE_COMBO);
  Serial.println("esp_now_set_self_role(ESP_NOW_ROLE_COMBO) done");

  esp_now_register_recv_cb(OnDataRecv);
  Serial.println("esp_now_register_recv_cb(OnDataRecv); done");
  
  esp_now_register_send_cb(OnDataSent);
  Serial.println("esp_now_register_send_cb(OnDataSent); done");
  
  // each receiving unit must be registered. Register peer
  esp_now_add_peer(ESP_NOW_MAC_adrOfRecv, ESP_NOW_ROLE_COMBO, 1, NULL, 0);
  Serial.println("esp_now_add_peer(MAC_adressOfReceivingUnit, ESP_NOW_ROLE_COMBO, 1, NULL, 0) done");  
}

// print macadress of THIS device 
// prepared for inserting code by copy & paste
// that will be flashed into receiving partner 
void PrintFileNameDateTime()
{
  Serial.print("Code running comes from file ");
  Serial.println(__FILE__);
  Serial.print("compiled ");
  Serial.print(__DATE__);
  Serial.println(__TIME__);  
}

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();      
}

// Callback when data is sent
void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus) 
{
  Serial.print("Last Send Status: ");
  if (sendStatus == 0)
    { Serial.println("Delivery Success"); }
  else
    { Serial.println("Delivery failed"); }
}

// Callback function that will be executed when data is received
void OnDataRecv(uint8_t * mac, uint8_t *incomingData, uint8_t NoOfBytesRcv) 
{ 
  // copy data bytewise from variable incomingData to variable myData
  memcpy(&myESP_NOW_Data, incomingData, sizeof(myESP_NOW_Data));

  //these lines must match the variables inside the ESP_NOW-data-structure
  Serial.print("No of Bytes received: ");
  Serial.println(NoOfBytesRcv);
  Serial.print("Array of Char: ");
  Serial.println(myESP_NOW_Data.MyESP_NOW_MsgStr);
  Serial.print("Int: ");
  Serial.println(myESP_NOW_Data.MyESP_NOW_Int);

  Serial.println();
}

 
void setup() 
{
  // Init Serial Monitor
  Serial.begin(115200);
  Serial.println();  // a carriage return to make sure serial-output begins in colum 1 of a new line
  PrintFileNameDateTime();
  // Set device as a Wi-Fi Station
  ESP_Now_setup();

  PrintWiFiMacAdress();  
  
  // prepare non-blocking timer for use
  SendDataTimer = nbTimer.OccupyTimer();
  if (SendDataTimer > 0) // Check if assigning a Timer-IDNr was successful
    {nbTimer.StartTimer(SendDataTimer);}
  else   
    {Serial.println("registering Timer was NOT successful!");}
}
 
void loop() 
{ 
  // check if timer-intervall is over
  if (nbTimer.elapsedTime(SendDataTimer) > 1000)
    {
     nbTimer.UpDateTimer(SendDataTimer,1000);
     Serial.println("SendData");
     ESP_NOW_SendData();
     Serial.println("SendData done");
    }
}

don’t like this forum software. It’s like writing code. gives you a smack for every little missing tiny something

Hello Stefan,
I looked at your code and for me , the code in english or german is the same like i am looking to a cuneiform message written by a Sumerian geek.
I know how to program microprocessors, mostly PIC processors, as i did it all my life.
For me ESP, with all it's commands it is a virgin territory.
I never studied ESP yet.
When i need something, i google, or look in the examples books, found what i need and use it.
I will test your code, late tonight and see if i understood what you wrote.
Thank you
Ion

Hello Stefan,
I cannot download the library.
I get DNS error.
Can you email me at igeorge688@gmail.com , please.