• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

Error sending to second peer in ESP_NOW

Q&A Forum › Category: ESP32 › Error sending to second peer in ESP_NOW
0 Vote Up Vote Down
John Lamport asked 9 months ago

Hello
I have found your tutorials very helpful and well written but I’m having a problem with sending data to 2 ESP32’s using ESP_NOW. I downloaded the examples to send the same data to 2 ESP32’s. I have confirmed the MAC Address.
What i have found is it doesn’t initiate the second peer. If I use the serial monitor with the first one I get the random numbers. I then swap the MAC Addresses in the initiator sketch and now I get see the numbers in that one but not the other. Whichever ESP32 I have as the second peer I get the “error sending data” message.
I am sending the same data to both so the broadcast mode maybe a better way but I would like to get this to work as it may be helpful for a future project. Any thoughts or suggestions? Do you have a tutorial on the broadcast mode?
Thanks
John

7 Answers
0 Vote Up Vote Down
Sara Santos Staff answered 8 months ago

Hi.
We recently tested the ESP-NOW projects and everything was working as expected.
Which tutorial are you following and code that you’re running?
Regards
Sara

0 Vote Up Vote Down
John Lamport answered 8 months ago

Sara
Thanks so much for getting back to me, I’m sure you and Rui are very busy with your other projects, It’s much appreciated.I got these examples from your course “Learn_ESP32_with_Arduino_IDE_V2_2”. I have 3 ESP32s, I downloaded the send code to 1 ESP and the receive code to the other 2. As far as I can remember the only changes I made were to the send code, I commented out the references to the third ESP as I only have two.
When I open the serial monitor on the send unit I get that the first Broadcast address was successful and an error sending the data to the second. I open the serial monitor on both of the receive units and the one that was successful shows me the random number, the other one is blank. If I swap the broadcastaddresses I get the same results other than the other ESP was now successful and error sending data to the second.
I’ve attached both of my copies of the sketches and a screenshot of the error message as well.
Thanks again for taking the time to look into this for me.
John
Send sketch
/*******Error Messgae**
Rui Santos
Complete project details at https://RandomNerdTutorials.com
*********/
#include <esp_now.h>
#include <WiFi.h>
// REPLACE WITH YOUR ESP RECEIVER’S MAC ADDRESS
uint8_t broadcastAddress1[] = {0xFC, 0xE8, 0xC0, 0x7C, 0xA2, 0x40};
uint8_t broadcastAddress2[] = {0x60, 0x55, 0xF9, 0xEB, 0xE6, 0x80};
//uint8_t broadcastAddress3[] = {0xFF, , , , , };
// Variable to add info about peer
esp_now_peer_info_t peerInfo;
typedef struct test_struct {
int x;
int y;
} test_struct;
test_struct test;
// callback when data is sent
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
char macStr[18];
Serial.print(“Packet to: “);
// Copies the sender mac address to a string
snprintf(macStr, sizeof(macStr), “%02x:%02x:%02x:%02x:%02x:%02x”,
mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
Serial.print(macStr);
Serial.print(” send status:\t”);
Serial.println(status == ESP_NOW_SEND_SUCCESS ? “Delivery Success” : “Delivery Fail”);
}

void setup() {
Serial.begin(115200);

WiFi.mode(WIFI_STA);

if (esp_now_init() != ESP_OK) {
Serial.println(“Error initializing ESP-NOW”);
return;
}

esp_now_register_send_cb(OnDataSent);

// register peer
peerInfo.channel = 0;
peerInfo.encrypt = false;
// register first peer
memcpy(peerInfo.peer_addr, broadcastAddress1, 6);
if (esp_now_add_peer(&peerInfo) != ESP_OK){
Serial.println(“Failed to add peer”);
return;
}
// register second peer
memcpy(peerInfo.peer_addr, broadcastAddress2, 6);
if (esp_now_add_peer(&peerInfo) != ESP_OK){
Serial.println(“Failed to add peer”);
return;
}
/// register third peer
// memcpy(peerInfo.peer_addr, broadcastAddress3, 6);
// if (esp_now_add_peer(&peerInfo) != ESP_OK){
// Serial.println(“Failed to add peer”);
// return;
// }
}

void loop() {
test.x = random(0,20);
test.y = random(0,20);

esp_err_t result = esp_now_send(NULL, (uint8_t *) &test, sizeof(test_struct));

if (result == ESP_OK) {
Serial.println(“Sent with success”);
}
else {
Serial.println(“Error sending the data”);
}
delay(2000);
}
Receive Sketch
/*********
Rui Santos
Complete project details at https://RandomNerdTutorials.com
*********/
#include <esp_now.h>
#include <WiFi.h>
//Structure example to receive data
//Must match the sender structure
typedef struct test_struct {
int x;
int y;
} test_struct;
//Create a struct_message called myData
test_struct myData;
//callback function that will be executed when data is received
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
memcpy(&myData, incomingData, sizeof(myData));
Serial.print(“Bytes received: “);
Serial.println(len);
Serial.print(“x: “);
Serial.println(myData.x);
Serial.print(“y: “);
Serial.println(myData.y);
Serial.println();
}

void setup() {
//Initialize Serial Monitor
Serial.begin(115200);

//Set device as a Wi-Fi Station
WiFi.mode(WIFI_STA);
//Init ESP-NOW
if (esp_now_init() != ESP_OK) {
Serial.println(“Error initializing ESP-NOW”);
return;
}

// Once ESPNow is successfully Init, we will register for recv CB to
// get recv packer info
esp_now_register_recv_cb(esp_now_recv_cb_t(OnDataRecv));
}

void loop() {
}
I’m not sure if I inserted the code right and I don’t think the image was attached

0 Vote Up Vote Down
Sara Santos Staff answered 8 months ago

Hi.
I think you’re using an older version of the sender code.
Make sure you’re using this one: https://github.com/RuiSantosdotme/ESP32-Course/blob/master/code/ESP_NOW/Unit_3/ESP_NOW_Sender_Multiple_Boards_Same_Message/ESP_NOW_Sender_Multiple_Boards_Same_Message.ino
 
Basically, change this line:

esp_err_t result = esp_now_send(NULL, (uint8_t *) &test, sizeof(test_struct));

to
 

 esp_err_t result = esp_now_send(0, (uint8_t *) &test, sizeof(test_struct));

 
Let me know if this fixes the issue.
 
Regards,
Sara

0 Vote Up Vote Down
John Lamport answered 8 months ago

Hi Sara
No I still have the same problem, It successfully sends data to the first broadcast address but an error sending data to the second. I have swapped which is the first and same result. 
The original sketch I started with from the tutorial had the “0” not the “NULL” in the esp_now_send statement. I had gone on the Arduino forum and asked about this problem and got a suggestion to try NULL and nullptr, same result with both. 
Just to make sure there weren’t other changes I did download the sketch you sent me the github link to, also the same.
Not that I thought it would make a difference, I swapped a send and receive ESP32, it didn’t.
Would it have anything to do with that your original sketch had 3 broadcastaddresses, I only have 2 so I commented out the third address and the third register third peer statement.
 
Thanks for your help
John

0 Vote Up Vote Down
John Lamport answered 8 months ago

Sara
Good news, I have it working but I don’t understand why, perhaps you will.
I started out with 3 ESP32 Dev Modules and had the error I have described. As I need a smaller form factor for one of the receiving boards I switched that to a Adafruit Feather S2. I still had the error.
This morning, just as a test, I switched the feather to be the sending board and it works. I can now send the data to 2 ESP32 Dev Modules.
What is the difference between a ESP32 Dev Module and the Feather?
Thanks
John

0 Vote Up Vote Down
Sara Santos Staff answered 8 months ago

Hi.
I’m not sure.
Maybe it was some issue with the MAC addresses?? It is difficult to tell why this weird issue happened.
 
I’m sorry I can’t help much, but I’m glad you found a workaround.
 
Regards,
Sara

0 Vote Up Vote Down
John Lamport answered 8 months ago

Hi Sara
 
It is a weird problem but I’ll just use what I have that works. Thanks for looking into the problem.
John

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025
  • [eBook Updated] Learn ESP32 with Arduino IDE eBook – Version 3.2 April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.