I'm trying to get ESP-NOW working on some D1 mini pro's (ESP8266) but the watchdog keeps rebooting the devices. I'm using this library: GitHub - yoursunny/WifiEspNow: ESP-NOW Arduino library for ESP8266 and ESP32 along with the example code from that called EspNowUnicast . Here is the error i keep getting:
Here is a demo-code for send-receive ESP-NOW which explains some basics and which does print quite a lot of infos if something goes wrong
#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[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
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;
MyESP_NOW_Data_type my_READYtoSEND_ESP_NOW_Data;
void ESP_NOW_SendData()
{
// Set values to send
strcpy(my_READYtoSEND_ESP_NOW_Data.MyESP_NOW_MsgStr, "just ESP-NOW 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( F("esp_now_send(ESP_NOW_MAC_adrOfRecv, (uint8_t *) &my_READYtoSEND_ESP_NOW_Data, sizeof(my_READYtoSEND_ESP_NOW_Data)); done") );
Serial.print( F("I am the board with the MAC-Adress ") );
Serial.println(WiFi.macAddress());
Serial.print( F("and I try to send my ESP-NOW-Data to the board with MAC-Adress ") );
Serial.println(MAC_adrOfRecv_as_AoC);
Serial.print( F("sended ====>Msg #") );
Serial.print( my_READYtoSEND_ESP_NOW_Data.MyESP_NOW_MsgStr);
Serial.print( F("#") );
// if sending has finished function OnDataSent is called
}
void ESP_Now_setup()
{
WiFi.mode(WIFI_STA);
Serial.println( F("WiFi.mode(WIFI_STA); done") );
WiFi.disconnect(); // for strange reasons WiFi.disconnect() makes ESP-NOW work
Serial.println( F("WiFi.disconnect(); done") );
// Init ESP-NOW
if (esp_now_init() != 0)
{
Serial.println( F("Error initializing ESP-NOW") );
return;
}
Serial.println( F("esp_now_init() was successful") );
esp_now_set_self_role(ESP_NOW_ROLE_COMBO);
Serial.println( F("esp_now_set_self_role(ESP_NOW_ROLE_COMBO) done") );
esp_now_register_recv_cb(OnDataRecv);
Serial.println( F("esp_now_register_recv_cb(OnDataRecv); done") );
esp_now_register_send_cb(OnDataSent);
Serial.println( F("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( F("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( F("MAC-Adress of Receiver is ") );
Serial.println(MAC_adrOfRecv_as_AoC);
}
void PrintFileNameDateTime()
{
Serial.print( F("Code running comes from file ") );
Serial.println(__FILE__);
Serial.print( F("compiled ") );
Serial.print(__DATE__);
Serial.print( F(" ") );
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( F("ESP Board Wifi.macAddress: ") );
Serial.println(MacAdr_AoC);
Serial.println();
Serial.println( F("copy the line below and replace the codeline") );
Serial.println( F("uint8_t ESP_NOW_MAC_adrOfRecv[] = { 0x...};") );
Serial.println( F("inside the code with the copied line from the serial monitor") );
Serial.println();
Serial.print( F("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( F(" };") );
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( F("Last Send Status: ") );
if (sendStatus == 0)
{ Serial.println( F("Delivery Success") ); }
else
{ Serial.println( F("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( F("No of Bytes received: ") );
Serial.println(NoOfBytesRcv);
//these lines must match the variables inside the ESP_NOW-data-structure
Serial.print( F("RECEIVED Message in Array of Char: #") );
Serial.print(my_received_ESP_NOW_Data.MyESP_NOW_MsgStr);
Serial.println( F("#") );
Serial.print( F("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
wifi_set_channel(13);
ESP_Now_setup();
PrintWiFiMacAdress();
}
void loop() {
// check if timer-intervall is over
if (TimePeriodIsOver(SendDataTimer,2000) )
{
Serial.println( F("SendData") );
ESP_NOW_SendData();
Serial.println( F("SendData done") );
}
}
Tried it, got the same behaviour. I suspected it might have something to do with the ESP's itself since it is using a different usb-to-uart, the cp2104. Maybe the drivers for it aren't properly installed. I just tried it on some genuine D1 mini's and the code runs fine.