Notifications
Clear all

Understanding the ESP-NOW code

4 Posts
3 Users
2 Likes
1,317 Views
(@awpologies)
Member
Joined: 2 years ago
Posts: 3
Topic starter  

Hi there!

I recently bought some ESP32 devkits and I am having a blast with them, but as I have very little experience with programming, I'm still trying to wrap my head around what the example code I'm using actually does. I was following the ESP-NOW tutorial on the DroneBot Youtube channel and came across this if-then statement:

  if (esp_now_add_peer (&peerInfo) != ESP_OK) {
    Serial.println("Failed to add peer");
    return;
  }  

 

Now I'm having some questions about what this piece of code actually does:

  1. In the first line of code, are we killing two birds with one stone here? Are we calling a function named esp_now_add_peer with an earlier declared variable named peerInfo, so it will add a peer, and then immediately check if it does not return with ESP_OK? If not what does it do?
  2. Where does the function esp_now_add_peer come from? Is it there because I included a header a header file called esp_now.h? If so, is there a way to look inside esp_now.h to see what other functions are available?
  3. Why is there an "&" in front of peerInfo? I understand it has to do something with the location of peerInfo in memory. Why do we need it here?
  4. Why do we type return; at the end of the if-then statement? What is this piece of code actually returning? Or are we just telling the compiler to return to the rest of the code?

 

Thanks a lot in advance for answering my questions! Any help would be greatly appreciated!

 

-AWPologies

 

 


   
Quote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2508
 
Posted by: @awpologies

 

  if (esp_now_add_peer (&peerInfo) != ESP_OK) {
    Serial.println("Failed to add peer");
    return;
  }  

 

Now I'm having some questions about what this piece of code actually does:

  1. In the first line of code, are we killing two birds with one stone here? Are we calling a function named esp_now_add_peer with an earlier declared variable named peerInfo, so it will add a peer, and then immediately check if it does not return with ESP_OK?

Yes

  1. Where does the function esp_now_add_peer come from? Is it there because I included a header a header file called esp_now.h? If so, is there a way to look inside esp_now.h to see what other functions are available?

Yes, look for the documentation or, if desperate, look at the .h file itself. It will be in the general directory chain of

your home > Documents > Arduino >libraries > folder with library name > library name.h

DO  NOT   MODIFY  ANYTHING  IN THERE !!!!

  1. Why is there an "&" in front of peerInfo? I understand it has to do something with the location of peerInfo in memory. Why do we need it here?

Because you're passing the address in to the function. Depending on the context which you haven't shown, this is likely so that the call can change the value at that address and thus return the peer info to you.

  1. Why do we type return; at the end of the if-then statement? What is this piece of code actually returning? Or are we just telling the compiler to return to the rest of the code?

Because if the creation failed, then this subroutine or function cannot proceed any further so it returns control back to whatever called it.

Anything seems possible when you don't know what you're talking about.


   
Ron reacted
ReplyQuote
(@awpologies)
Member
Joined: 2 years ago
Posts: 3
Topic starter  

@will 

That helps a lot, thanks for answering! 

Now I can go break my head over an other piece of code 😉

Cheers!


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6907
 

@awpologies Actually you did pretty good on your own, but master @will did his usual expert polishing. You have NOOOOO idea how refreshing and rewarding it is to see your question and response to the help provided. Keep it up.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
AWPologies reacted
ReplyQuote