Comments on: ESP-NOW Two-Way Communication Between ESP32 Boards https://randomnerdtutorials.com/esp-now-two-way-communication-esp32/ Learn ESP8266, ESP32, Arduino, and Raspberry Pi Fri, 13 Jun 2025 14:28:47 +0000 hourly 1 https://wordpress.org/?v=6.8.2 By: Mohan https://randomnerdtutorials.com/esp-now-two-way-communication-esp32/#comment-1057167 Fri, 13 Jun 2025 06:58:14 +0000 https://randomnerdtutorials.com/?p=93083#comment-1057167 i want to establish a connection between two esp32 devices, acting as transmitter and receiver with ESP now peer to peer, by pressing a press button at both the sides. the transmitter and receiver should exchange their mac addresses and pair each other. however, i am failing to connect both. do you have any suggestions?

]]>
By: Mohan https://randomnerdtutorials.com/esp-now-two-way-communication-esp32/#comment-1057142 Fri, 13 Jun 2025 04:32:09 +0000 https://randomnerdtutorials.com/?p=93083#comment-1057142 Paired with receiver!"); digitalWrite(STATUS_LED, HIGH); delay(300); digitalWrite(STATUS_LED, LOW); } } </code> } } void sendMACToBroadcast() { uint8_t broadcast[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; uint8_t myMAC[6]; WiFi.macAddress(myMAC); esp_now_send(broadcast, myMAC, 6); Serial.print("📤 Sent my MAC: "); printMAC(myMAC); } void setup() { Serial.begin(115200); pinMode(PAIR_BUTTON_PIN, INPUT_PULLUP); pinMode(STATUS_LED, OUTPUT); WiFi.mode(WIFI_STA); esp_wifi_set_promiscuous(true); esp_wifi_set_channel(1, WIFI_SECOND_CHAN_NONE); esp_wifi_set_promiscuous(false); if (esp_now_init() != ESP_OK) { Serial.println("❌ ESP-NOW init failed"); return; } Serial.println("✅ Transmitter Ready"); esp_now_register_recv_cb(onDataRecv); } void loop() { if (digitalRead(PAIR_BUTTON_PIN) == HIGH) { Serial.println("🔘 Button Pressed - Sending pairing request..."); sendMACToBroadcast(); delay(1000); // Debounce and prevent spamming } } This is my receiver code #include <esp_now.h> #include <WiFi.h> #include "esp_wifi.h" #define PAIR_BUTTON_PIN 4 #define STATUS_LED 2 uint8_t transmitterMAC[6]; void printMAC(const uint8_t *mac) { char macStr[18]; snprintf(macStr, sizeof(macStr), "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); Serial.print("Transmitter MAC: "); Serial.println(macStr); } void onDataRecv(const esp_now_recv_info_t *info, const uint8_t *data, int len) { if (len == 6 && digitalRead(PAIR_BUTTON_PIN) == HIGH) { memcpy(transmitterMAC, data, 6); printMAC(transmitterMAC); <code>esp_now_peer_info_t peerInfo = {}; memcpy(peerInfo.peer_addr, transmitterMAC, 6); peerInfo.channel = 1; peerInfo.encrypt = false; if (!esp_now_is_peer_exist(transmitterMAC)) { if (esp_now_add_peer(&peerInfo) == ESP_OK) { Serial.println("✅ Paired with transmitter!"); digitalWrite(STATUS_LED, HIGH); delay(300); digitalWrite(STATUS_LED, LOW); } } // Send our MAC back to transmitter uint8_t myMAC[6]; WiFi.macAddress(myMAC); esp_now_send(transmitterMAC, myMAC, 6); </code> } } void setup() { Serial.begin(115200); pinMode(PAIR_BUTTON_PIN, INPUT_PULLUP); pinMode(STATUS_LED, OUTPUT); WiFi.mode(WIFI_STA); esp_wifi_set_promiscuous(true); esp_wifi_set_channel(1, WIFI_SECOND_CHAN_NONE); esp_wifi_set_promiscuous(false); if (esp_now_init() != ESP_OK) { Serial.println("❌ ESP-NOW init failed"); return; } Serial.println("✅ Receiver Ready"); esp_now_register_recv_cb(onDataRecv); } void loop() { // Nothing here; wait for pairing request } please help me to connect 2 MCU's.]]> i want to establish a connection between two esp32 devices through ESP now peer to peer, acting as transmitter and receiver, by pressing a press button at both the sides. the transmitter and receiver should exchange their mac addresses. however, i am failing to connect both. do you have any suggestions?
This is my transmitter code
#include <esp_now.h>
#include <WiFi.h>
#include “esp_wifi.h”

#define PAIR_BUTTON_PIN 4
#define STATUS_LED 2

uint8_t receiverMAC[6];

void printMAC(const uint8_t *mac) {
char macStr[18];
snprintf(macStr, sizeof(macStr), “%02X:%02X:%02X:%02X:%02X:%02X”,
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
Serial.print(“Receiver MAC: “);
Serial.println(macStr);
}

void onDataRecv(const esp_now_recv_info_t *info, const uint8_t *data, int len) {
if (len == 6 && digitalRead(PAIR_BUTTON_PIN) == HIGH) {
memcpy(receiverMAC, data, 6);
printMAC(receiverMAC);

esp_now_peer_info_t peerInfo = {};
memcpy(peerInfo.peer_addr, receiverMAC, 6);
peerInfo.channel = 1;
peerInfo.encrypt = false;

if (!esp_now_is_peer_exist(receiverMAC)) {
if (esp_now_add_peer(&peerInfo) == ESP_OK) {
Serial.println("✅ Paired with receiver!");
digitalWrite(STATUS_LED, HIGH);
delay(300);
digitalWrite(STATUS_LED, LOW);
}
}

}
}

void sendMACToBroadcast() {
uint8_t broadcast[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
uint8_t myMAC[6];
WiFi.macAddress(myMAC);
esp_now_send(broadcast, myMAC, 6);
Serial.print(“📤 Sent my MAC: “);
printMAC(myMAC);
}

void setup() {
Serial.begin(115200);
pinMode(PAIR_BUTTON_PIN, INPUT_PULLUP);
pinMode(STATUS_LED, OUTPUT);

WiFi.mode(WIFI_STA);
esp_wifi_set_promiscuous(true);
esp_wifi_set_channel(1, WIFI_SECOND_CHAN_NONE);
esp_wifi_set_promiscuous(false);

if (esp_now_init() != ESP_OK) {
Serial.println(“❌ ESP-NOW init failed”);
return;
}
Serial.println(“✅ Transmitter Ready”);
esp_now_register_recv_cb(onDataRecv);
}

void loop() {
if (digitalRead(PAIR_BUTTON_PIN) == HIGH) {
Serial.println(“🔘 Button Pressed – Sending pairing request…”);
sendMACToBroadcast();
delay(1000); // Debounce and prevent spamming
}
}
This is my receiver code
#include <esp_now.h>
#include <WiFi.h>
#include “esp_wifi.h”

#define PAIR_BUTTON_PIN 4
#define STATUS_LED 2

uint8_t transmitterMAC[6];

void printMAC(const uint8_t *mac) {
char macStr[18];
snprintf(macStr, sizeof(macStr), “%02X:%02X:%02X:%02X:%02X:%02X”,
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
Serial.print(“Transmitter MAC: “);
Serial.println(macStr);
}

void onDataRecv(const esp_now_recv_info_t *info, const uint8_t *data, int len) {
if (len == 6 && digitalRead(PAIR_BUTTON_PIN) == HIGH) {
memcpy(transmitterMAC, data, 6);
printMAC(transmitterMAC);

esp_now_peer_info_t peerInfo = {};
memcpy(peerInfo.peer_addr, transmitterMAC, 6);
peerInfo.channel = 1;
peerInfo.encrypt = false;

if (!esp_now_is_peer_exist(transmitterMAC)) {
if (esp_now_add_peer(&peerInfo) == ESP_OK) {
Serial.println("✅ Paired with transmitter!");
digitalWrite(STATUS_LED, HIGH);
delay(300);
digitalWrite(STATUS_LED, LOW);
}
}

// Send our MAC back to transmitter
uint8_t myMAC[6];
WiFi.macAddress(myMAC);
esp_now_send(transmitterMAC, myMAC, 6);

}
}

void setup() {
Serial.begin(115200);
pinMode(PAIR_BUTTON_PIN, INPUT_PULLUP);
pinMode(STATUS_LED, OUTPUT);

WiFi.mode(WIFI_STA);
esp_wifi_set_promiscuous(true);
esp_wifi_set_channel(1, WIFI_SECOND_CHAN_NONE);
esp_wifi_set_promiscuous(false);

if (esp_now_init() != ESP_OK) {
Serial.println(“❌ ESP-NOW init failed”);
return;
}
Serial.println(“✅ Receiver Ready”);
esp_now_register_recv_cb(onDataRecv);
}

void loop() {
// Nothing here; wait for pairing request
}
please help me to connect 2 MCU’s.

]]>
By: Mohan https://randomnerdtutorials.com/esp-now-two-way-communication-esp32/#comment-1057140 Fri, 13 Jun 2025 04:19:13 +0000 https://randomnerdtutorials.com/?p=93083#comment-1057140 Hi,
I want to establish a connection between two esp32 devices, acting as transmitter and receiver, by pressing a press button at both the sides. the transmitter and receiver should exchange their MAC addresses and they should pair each other. however, i am failing to connect both. do you have any suggestions?

]]>
By: Sara Santos https://randomnerdtutorials.com/esp-now-two-way-communication-esp32/#comment-1052306 Fri, 30 May 2025 09:01:18 +0000 https://randomnerdtutorials.com/?p=93083#comment-1052306 In reply to Abhi.

Hi.
For that, you can use this tutorial as a starting point: https://randomnerdtutorials.com/esp32-esp-now-wi-fi-web-server/
regards,
Sara

]]>
By: Abhi https://randomnerdtutorials.com/esp-now-two-way-communication-esp32/#comment-1052144 Thu, 29 May 2025 13:43:22 +0000 https://randomnerdtutorials.com/?p=93083#comment-1052144 With an ESP32, am I able to broadcast a wifi network that has a basic web server displaying data, in conjunction with ESP now? I basically want to receive data to a “host” ESP32 via ESPnow and display it on a local web server. Thanks

]]>
By: Michael Morlan https://randomnerdtutorials.com/esp-now-two-way-communication-esp32/#comment-1044936 Fri, 16 May 2025 17:10:40 +0000 https://randomnerdtutorials.com/?p=93083#comment-1044936 Following to my posts, above: I’m going to try setting up multi-core tasks per your excellent post: https://randomnerdtutorials.com/esp32-dual-core-arduino-ide

As far as shared data and mutex, do I need to use mutex if only one process is writing and the other only reading the same structure?

]]>
By: Michael Morlan https://randomnerdtutorials.com/esp-now-two-way-communication-esp32/#comment-1044935 Fri, 16 May 2025 17:08:54 +0000 https://randomnerdtutorials.com/?p=93083#comment-1044935 In reply to Geoff.

Geoff, thanks for that note about ESPNOW running on Core0. Does that happen if I’m not setting up multi-core tasks per this article?

https://randomnerdtutorials.com/esp32-dual-core-arduino-ide

And, if I am setting up multi-core tasks, do I place a callback interrupt function inside the core0 task or leave it declared at the top level?

]]>
By: Geoff https://randomnerdtutorials.com/esp-now-two-way-communication-esp32/#comment-1044475 Thu, 15 May 2025 04:22:23 +0000 https://randomnerdtutorials.com/?p=93083#comment-1044475 In reply to Michael Morlan.

Its unlikely to be an ESPNOW issue. You can always ensure separation on receive by using the senders MAC addresses as an ID (see RNT tutorial) or you can add an ‘enum’ to id each transmission. ESPNOW should default to ESP32 Core0 so make sure all you other code is running concurrently on Core1 only.You may also need to use Mutexes etc… to manage data flow and avoid conflicts.
Are you sending structs?
Are your port expanders I2C? SPI versions are a lot faster.

]]>
By: Michael Morlan https://randomnerdtutorials.com/esp-now-two-way-communication-esp32/#comment-1044459 Thu, 15 May 2025 01:31:59 +0000 https://randomnerdtutorials.com/?p=93083#comment-1044459 Following up on my question, above. I added a .1 second delay to the main loop. I think that gives the I2C devices time to respond and everything time to run before incoming interrupts break things. It’s much more stable but I still get an occasional stutter every 90 seconds or so. So, all I did is slow the stuttering down as well.

I suspect this is caused by a collision when both devices send at the same time and the incoming interrupt stops the outgoing packets. I may try adding logic where each device waits for an incoming packet before sending one out, in essence getting them to exchange back and forth to avoid collisions. We’ll see how it goes.

]]>
By: Michael Morlan https://randomnerdtutorials.com/esp-now-two-way-communication-esp32/#comment-1043791 Tue, 13 May 2025 19:31:20 +0000 https://randomnerdtutorials.com/?p=93083#comment-1043791 Hi Rui & Sara,

Thanks for the excellent tutorial. I have a working two-way setup using your library and example. I’m using it for bidirectional signaling of 16 i/O channels (8 each direction) using a MCP23017 on each side. 8 inputs on one MCP23017 control 8 outputs on the opposite MCP23017.

I’m encountering an interesting issue: The inputs/outputs of the two MCP23017 chips are working fine. But, I’m experiencing an intermittent stuttering of the outputs. All of them will blink intermittently, on both sides. I wonder if this is related to the ESP-NOW stream reconnecting and throwing junk at the incoming structures.

Thoughts?

Again, thanks for helping me get this far.

]]>