Your code is mimimum missing the add_peer-function call
try this demo-code.
It does a lot of serial output that helps tracking down bug/misadjustments
#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)
// don't try to adapt this code to ESP32-modules
// ESP32-modules need another library with SUBSTANTIAL differencies
// go search for a demo-code that is written for ESP32
// if you want to receive ESP-NOW-messages reliably you have to strictly avoid
// the command delay(). Delay blocks the whole CPU and if a ESP-NOW-Message
// is send while the receivers CPU is blocked the receiving will fail
// so use a timing-function based on millis() like the one below instead.
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 SendDataTimer;
//#############################################################################################
// list of MAC-Adresses of all receivers:
// important note: ESP-NOW sending is based on the RECEIVERS Mac-adress.
// this means for every ESP8266-modul that shall receive a ESP-NOW-Messages
// you have to execute register a peer in the Setup-function
// esp_now_add_peer(ESP_NOW_MAC_adr Of Recv, ESP_NOW_ROLE_COMBO, 1, NULL, 0);
// and you have to execute the command
// esp_now_send(ESP_NOW_MAC_adr Of Recv, (uint8_t *) &myESP_NOW_Data, sizeof(myESP_NOW_Data));
uint8_t ESP_NOW_MAC_adrOfRecv[] = { 0xF4, 0xCF, 0xA2, 0xD1, 0x3D, 0x28 }; // Board 0x06 sendet an Board 0x28
char MAC_adrOfRecv_as_AoC[18];
//##############################################################################################
// Structure example to send data. Must match the receiver structure
typedef struct MyESP_NOW_Data_type {
char MyESP_NOW_MsgStr[128];
int MyESP_NOW_Int;
} MyESP_NOW_Data_type;
// Create a "variable" called myESP_NOW_Data of variable-type MyESP_NOW_Data_type
MyESP_NOW_Data_type my_received_ESP_NOW_Data; //neu 2020.07.14-15:00
MyESP_NOW_Data_type my_READYtoSEND_ESP_NOW_Data; //neu 2020.07.14-15:00
void ESP_NOW_SendData()
{
// Set values to send
strcpy(my_READYtoSEND_ESP_NOW_Data.MyESP_NOW_MsgStr, "HI I'M SENDING EVERY TWO SECONDS COUNTiNG UP + 6");
my_READYtoSEND_ESP_NOW_Data.MyESP_NOW_Int = my_READYtoSEND_ESP_NOW_Data.MyESP_NOW_Int + 6;
// Send message via ESP-NOW
esp_now_send(ESP_NOW_MAC_adrOfRecv, (uint8_t *) &my_READYtoSEND_ESP_NOW_Data, sizeof(my_READYtoSEND_ESP_NOW_Data));
Serial.println("esp_now_send(ESP_NOW_MAC_adrOfRecv, (uint8_t *) &my_READYtoSEND_ESP_NOW_Data, sizeof(my_READYtoSEND_ESP_NOW_Data)); done");
Serial.print("I am the board with the MAC-Adress ");
Serial.println(WiFi.macAddress());
Serial.print("and I try to send my ESP-NOW-Data to the board with MAC-Adress ");
Serial.println(MAC_adrOfRecv_as_AoC);
// 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");
for (int i=0;i<6;i++) {
strcat (MAC_adrOfRecv_as_AoC, hex02str(ESP_NOW_MAC_adrOfRecv[i]) );
if (i < 6) {
strcat (MAC_adrOfRecv_as_AoC, ":" );
}
}
MAC_adrOfRecv_as_AoC[17] = 0;
strupr(MAC_adrOfRecv_as_AoC); // make letters UPPERCASE
Serial.print("MAC-Adress of Receiver is ");
Serial.println(MAC_adrOfRecv_as_AoC);
}
void PrintFileNameDateTime()
{
Serial.print("Code running comes from file ");
Serial.println(__FILE__);
Serial.print("compiled ");
Serial.print(__DATE__);
Serial.println(__TIME__);
}
// print macadress of THIS device
// prepared for inserting code by copy & paste
// that will be flashed into receiving partner
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 codeline");
Serial.println("uint8_t ESP_NOW_MAC_adrOfRecv[] = { 0x60, 0x01, 0x94, 0x70, 0xAE, 0x7B };");
Serial.println("inside the code with the copied line from the serial monitor");
Serial.println();
Serial.print("uint8_t ESP_NOW_MAC_adrOfRecv[] = {");
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();
}
char* hex02str(uint8_t b) {
static char str[]="FF"; // RAM für einen 2-Zeichen string reservieren.
snprintf(str,sizeof(str),"%02x",b);
return str;
}
// 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 my_received_ESP_NOW_Data
memcpy(&my_received_ESP_NOW_Data, incomingData, sizeof(my_received_ESP_NOW_Data));
Serial.print("No of Bytes received: ");
Serial.println(NoOfBytesRcv);
//these lines must match the variables inside the ESP_NOW-data-structure
Serial.print("Array of Char: #");
Serial.print(my_received_ESP_NOW_Data.MyESP_NOW_MsgStr);
Serial.println("#");
Serial.print("Int: ");
Serial.println(my_received_ESP_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();
}
void loop()
{
// check if timer-intervall is over
if (TimePeriodIsOver(SendDataTimer,2000) ) //neu 2020.07.14-15:00
{
Serial.println("SendData");
ESP_NOW_SendData();
Serial.println("SendData done");
}
}
best regards Stefan