Nowadays, wireless communication between devices is essential in the development of IoT (Internet of Things) systems. ESP32 microcontrollers enable the implementation of this type of communication efficiently through the ESP-NOW protocol, which allows data exchange without the need for a conventional WiFi network.
In this practice, a bidirectional communication system was developed between two ESP32 modules, where each device includes a switch (input) and an LED (output). The objective is that when the switch of one ESP32 is activated, the LED of the other device turns on, thereby demonstrating direct and real-time wireless communication.
Each ESP32 was configured as follows:
This same configuration is repeated on both devices, allowing each ESP32 to operate simultaneously as both transmitter and receiver.
Initially, the correct operation of the components was verified individually by testing the LED activation and the push button reading through the serial monitor.
Subsequently, the MAC addresses of each ESP32 were obtained, which are required to establish communication using the ESP-NOW protocol.
The communication was carried out bidirectionally, allowing both devices to interact with each other without the need for a router or internet connection.
Code ESP32 A
#include <WiFi.h>
#include <esp_now.h>
#define LED_PIN 2
#define BOTON_PIN 4
// MAC del ESP32 B
uint8_t peerAddress[] = {0x88,0x57,0x21,0xE0,0x45,0x5C};
bool estadoRemoto = 0;
void OnDataRecv(const esp_now_recv_info_t *info, const uint8_t *data, int len) {
if (len == 1) estadoRemoto = data[0];
}
void setup()
{
Serial.begin(115200);
pinMode(LED_PIN, OUTPUT);
pinMode(BOTON_PIN, INPUT_PULLDOWN);
WiFi.mode(WIFI_STA);
WiFi.disconnect();
esp_now_init();
esp_now_register_recv_cb(OnDataRecv);
esp_now_peer_info_t peerInfo = {};
memcpy(peerInfo.peer_addr, peerAddress, 6);
esp_now_add_peer(&peerInfo);
}
void loop()
{
bool estadoLocal = digitalRead(BOTON_PIN);
uint8_t data = estadoLocal;
esp_now_send(peerAddress, &data, 1);
digitalWrite(LED_PIN, estadoRemoto);
delay(50);
}
#include <WiFi.h>
#include <esp_now.h>
#define LED_PIN 2
#define BOTON_PIN 4
// MAC del ESP32 A
uint8_t peerAddress[] = {0x88,0x57,0x21,0xE0,0x31,0x08};
bool estadoRemoto = 0;
void OnDataRecv(const esp_now_recv_info_t *info, const uint8_t *data, int len) {
if (len == 1) estadoRemoto = data[0];
}
void setup()
{
Serial.begin(115200);
pinMode(LED_PIN, OUTPUT);
pinMode(BOTON_PIN, INPUT_PULLDOWN);
WiFi.mode(WIFI_STA);
WiFi.disconnect();
esp_now_init();
esp_now_register_recv_cb(OnDataRecv);
esp_now_peer_info_t peerInfo = {};
memcpy(peerInfo.peer_addr, peerAddress, 6);
esp_now_add_peer(&peerInfo);
}
void loop()
{
bool estadoLocal = digitalRead(BOTON_PIN);
uint8_t data = estadoLocal;
esp_now_send(peerAddress, &data, 1);
digitalWrite(LED_PIN, estadoRemoto);
delay(50);
}
A stable wireless communication was successfully established between the two ESP32 modules. The observed results were as follows:
It was verified that the ESP-NOW protocol enables efficient, fast, and reliable direct wireless communication between ESP32 devices without requiring network infrastructure, making it suitable for applications in automation, home automation, and industrial control systems.