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


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()
  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");
  Serial.println("esp_now_init() was successful");

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

  Serial.println("esp_now_register_recv_cb(OnDataRecv); done");
  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.print("compiled ");

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("copy the line below and replace the '#' with variablename of your choice");
  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
    if (i < 14) Serial.print(", ");
  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"); }
    { 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.print("Array of Char: ");
  Serial.print("Int: ");


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

  // prepare non-blocking timer for use
  SendDataTimer = nbTimer.OccupyTimer();
  if (SendDataTimer > 0) // Check if assigning a Timer-IDNr was successful
    {Serial.println("registering Timer was NOT successful!");}
void loop() 
  // check if timer-intervall is over
  if (nbTimer.elapsedTime(SendDataTimer) > 1000)
     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

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