• 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

ESP-Now Sending to 2 receivers

Q&A Forum › Category: ESP32 › ESP-Now Sending to 2 receivers
0 Vote Up Vote Down
Donald asked 4 years ago

Hello,
I’ve been interested in learning more about ESP-Now, since seeing it in the Learn ESP32 book.
I’m modifying the code to sending when a PIR is ‘tripped.’ All that’s getting sent is, essentially, a 0 or a 1, (int e) to indicate PIR detection or no detection. It could be done by boolean, but I’m working within the established sketches provided in the examples. I got it to work fabulously when there’s just one receiver. However, when I add a second peer/receiver, the second one shows a Delivery Failure.
Sender Code:

#include <Arduino.h>
// ESP NOW communication
// Sender sends a signal when PIR is activated
// When receiver receives, LED will light

#include <esp_now.h>
#include <WiFi.h>
int PIRpin = 5;
int pirValue;

// REPLACE WITH YOUR RECEIVER MAC Address
uint8_t broadcastAddress1[] = {0x84, 0xCC, 0xA8, 0x68, 0xEE, 0x74};
uint8_t broadcastAddress2[] = {0x24, 0x0A, 0xC4, 0xED, 0x46, 0xEC};

// Structure example to send data
// Must match the receiver structure
typedef struct struct_message {
int e;
} struct_message;

// Create a struct_message called myData
struct_message myData;

// callback when data is sent
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
Serial.print("\r\nLast Packet Send Status:\t");
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
}
 
void setup() {
// Init Serial Monitor
Serial.begin(115200);
pinMode(PIRpin, INPUT);
myData.e = 0;
 
// 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 Send CB to
// get the status of Trasnmitted packet
esp_now_register_send_cb(OnDataSent);
 
// Register peer
esp_now_peer_info_tpeerInfo;
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;
}
// Add peer **This is the extraneous part! When Deleted, it functions **
if (esp_now_add_peer(&peerInfo) != ESP_OK){
Serial.println("Failed to add peer");
return;
}
}
 
void loop() {
// Set values to send
pirValue = digitalRead(PIRpin);
 
if (pirValue == HIGH) {
myData.e = 1;
Serial.println("motion detected");
} else {
myData.e = 0;
Serial.println("no motion detected.");
}
 
 

// Send message via ESP-NOW
esp_err_tresult = esp_now_send(0, (uint8_t *) &myData, sizeof(myData));
 
if (result == ESP_OK) {
Serial.println("Sent with success");
}
else {
Serial.println("Error sending the data");
}
delay(1000);
}

The 2 receiver boards code is:

#include <Arduino.h>
// ESP NOW Communication - Receiver code
#include <esp_now.h>
#include <WiFi.h>
int LED = 5;
int LedState = LOW;
unsigned long previousMillis = 0;
unsigned long interval = 250;
// Structure example to receive data
// Must match the sender structure
typedef struct struct_message {
int e;
} struct_message;

// Create a struct_message called myData
struct_message 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("Int: ");
Serial.println(myData.e);
Serial.println();

}

void setup() { Serial.begin(115200); pinMode(LED, OUTPUT); digitalWrite(LED, LOW); // Set device as a WiFi station WiFi.mode(WIFI_STA); Serial.println(WiFi.macAddress());
// Initialize ESP-Now if (esp_now_init() != ESP_OK) { Serial.println(“Error initializing ESP-NOW”); return; }
// Once ESP-Now is successfully initialized, we will register to receive CB to get recv packer into esp_now_register_recv_cb(OnDataRecv); }
void alarmAnimation() { unsignedlongcurrentMillis = millis(); if (currentMillis – previousMillis > interval) { previousMillis = currentMillis; if (LedState == LOW) LedState = HIGH; else LedState = LOW; digitalWrite(LED, LedState); } }
void loop() { if (myData.e == 1){ alarmAnimation(); } else { digitalWrite(LED, LOW); } }

The communication happens perfectly as expected with Mac Address 1. It is a usual internal antenna DoIt ESP32 board. Mac Address 2 is a new board to me, it’s an external antenna ESP32 board, a 32U.

Question Tags: Esp-now
2 Answers
0 Vote Up Vote Down
Donald answered 4 years ago

I found my error. There’s an extraneous Add peer left in the code right after the Register SECOND peer. I guess I thought Register Peer was different from Add Peer.
 
Now, when my in-laws or cats try to come up the stairs unannounced, we’ll get a visual warning!

0 Vote Up Vote Down
Sara Santos Staff answered 4 years ago

Great!
I’m glad you found the error, and everything is working now.
Nice project you did with our tutorials. Congratulations! 🙂
Regards,
Sara

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

  • [eBook Updated] Learn Raspberry Pi Pico/Pico W with MicroPython eBook – Version 1.2 May 26, 2025
  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition 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.