Hi;
I am trying to duplicate the simplest ESPNOW tutorial. two boards, one sender one receiver. I have cut and pasted the entire code from your tutorial. “Getting Started with ESP-NOW” and loaded the two sketches onto two D1 mini’s. The only change I have made is to insert the MAC address of the receiver into the sender sketch. Both sketches compile and upload OK. (platform IO). The only result is that the sender dutifully reports “Delivery Fail” every 2 seconds on the serial monitor. I have carefully reviewed everything. Especially the MAC address and can find no mistakes. I have also swapped the two boards, making the receiver the sender and get the same results.
My question is. Are there any error codes returned by the send process that I could send to the serial monitor to figure out what is happening. and how to do that. A simple success or fail notice doesn’t help much.
Rick
You need to check out the documentation to see the return types. Do you get the “Sent with Success” message?
Hi Steve,
No, I only get the “Delivery fail” message.
I have reviewed the ESP_now API docs and they show the esp_now_send() function as returning an int.and lists a series of error statements. that I assume could be accessed via the returned integer.
Then in the send callback function one of the parameters is the int sendStatus which is tested in the If statement to determine success or failure. It would seem that the sendStatus variable would contain the error code but I can’t seem to print it out. i.e. a Serial.print(sendstatus); inserted in the code just ahead of the fail message compiles OK but outputs nothing.
Not sure where to go next.
Thanks, Rick
I don’t see a sendStatus variable anywhere, just a status variable (Are we looking at the same tutorial). At the bottom of the documentation you will see the definition of status as a esp_now_sent_status_t which is an enumeration of two values (Which you can check for in code) of ESP_NOW_SEND_SUCCESS or ESP_NOW_SEND_FAIL. Seeing as there are only two types the else in code checks for ESP_NOW_SEND_FAIL which gives you your error message.
To see what’s going on you should try some other functions. esp_now_get_peer seems likely. You just need to supply it with the MAC address of the other board. If you get an error check for the returns (ESP_ERR_ESPNOW_NOT_INIT etc)
Hi Rick,
The tutorial code is buggy.
The peerInfo
variable is instantiated locally within the setup()
function. In other words, it is not supposed to exist once you exit that function.
void setup() { // ... esp_now_peer_info_t peerInfo; // ... if (esp_now_add_peer(&peerInfo) != ESP_OK){ Serial.println("Failed to add peer"); return; } }
However, its allocation address is passed to the esp_now_add_peer
function. This is a programming error. A locally allocated variable should not be used outside its scope. However, the instantiated object will be used in the loop()
function through the esp_now_send()
function.
The observed behavior is totally erratic at runtime.
The first thing to do is to declare this variable as a global variable (i.e. outside the setup()
function).
// ... esp_now_peer_info_t peerInfo; // ... void setup() { // ... if (esp_now_add_peer(&peerInfo) != ESP_OK){ Serial.println("Failed to add peer"); return; } }
It seemed to me that I had already made this remark to Sara and Rui…
And if that’s not enough, try replacing the receiving MAC address with a broadcast address in the sender’s source code to see if that’s where the problem is:
uint8_t broadcastAddress[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
Try this and tell us what you get.
Thanks guys for stepping in and looking at this. Lets see if I can simplify. first of all, sending to a broadcast address works fine. a specific address does not.
Steve, I think we are looking at different tutorials. I am working from the 8266 version of “Getting started with ESP-NOW” its code is slightly different from the ESP32 version. sendStatus is used in the send callback function and is of type uint8_t. I can test this for success or fail. Likewise, I can test the esp_now_is_peer_exist() function since it returns type boolean. But the esp_now_send() function returns type esp_err_t which I cant find the definition of. How do I test for the error returned.
Steph, I agree. using a local variable out of scope will cause problems. In this version of the tutorial, peerInfo is not used. the add_peer function is passed the parameters directly.
Thanks again
Rick
Hi Rick,
Ok, so I guess it’s the following tutorial:
Getting Started with ESP-NOW (ESP8266 NodeMCU with Arduino IDE)
Indeed, in the case of the ESP8266, the API is different and the problem I raised does not apply here. Although it remains relevant in the case of the ESP32!
I don’t have an ESP8266 board and have never developed anything on this platform… but while searching a bit, I found something that might help you to interpret the int
error codes that are returned by esp_now_send()
.
Take a look at it and tell us if it helps you more?