Mastering ESP-NOW: Two-Way Communication Between ESP32 Boards

In the world of IoT (Internet of Things), efficient communication between devices is crucial. ESP-NOW, a protocol developed by Espressif, enables quick and low-power wireless communication between ESP32 boards. This article will guide you through the process of setting up ESP-NOW for two-way communication between two ESP32 boards, allowing them to send and receive data seamlessly.

What is ESP-NOW?                                                                                                                                                                               

ESP-NOW is a connectionless communication protocol that uses peer-to-peer networking to allow multiple ESP32 devices to exchange data without the need for Wi-Fi. It is particularly useful in scenarios where low power consumption and fast data transfer are essential. Unlike traditional Wi-Fi communication, ESP-NOW does not require the devices to be connected to a network, making it ideal for remote sensors, home automation, and other IoT applications.

Setting Up ESP-NOW Two-Way Communication:

To set up two-way communication between two ESP32 boards using ESP-NOW, follow these steps:

  1. Hardware Requirements:

    • Two ESP32 boards
    • USB cables for programming
    • Arduino IDE installed on your computer
  2. Installing the Necessary Libraries:

    Ensure that you have the ESP32 board package installed in your Arduino IDE. You can install it by navigating to File > Preferences and adding the following URL in the “Additional Board Manager URLs”:
     
    https://dl.espressif.com/dl/package_esp32_index.json

    Next, go to Tools > Board > Board Manager and install the ESP32 library.

  3. Coding for Two-Way Communication:

    The basic structure of your code will involve setting up each ESP32 board as both a sender and a receiver. Below is a simple example code:
     
    #include <esp_now.h> #include <WiFi.h> uint8_t peerAddress[] = {0x24, 0x6F, 0x28, 0xA1, 0x5E, 0x7C}; typedef struct struct_message { char a[32]; int b; float c; bool d; } struct_message; struct_message myData; void setup() { Serial.begin(115200); WiFi.mode(WIFI_STA); esp_now_init(); esp_now_register_send_cb(OnDataSent); esp_now_register_recv_cb(OnDataRecv); esp_now_peer_info_t peerInfo; memcpy(peerInfo.peer_addr, peerAddress, 6); peerInfo.channel = 0; peerInfo.encrypt = false; if (esp_now_add_peer(&peerInfo) != ESP_OK) { Serial.println("Failed to add peer"); return; } } void loop() { strcpy(myData.a, "Hello ESP-NOW"); myData.b = random(1, 20); myData.c = 1.2; myData.d = false; esp_err_t result = esp_now_send(peerAddress, (uint8_t *) &myData, sizeof(myData)); delay(2000); } 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 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("Char: "); Serial.println(myData.a); Serial.print("Int: "); Serial.println(myData.b); Serial.print("Float: "); Serial.println(myData.c); Serial.print("Bool: "); Serial.println(myData.d); }
  4. Uploading the Code:

    • Connect your ESP32 boards to your computer using the USB cables.
    • Select the correct COM port from the Tools menu in Arduino IDE.
    • Upload the code to both ESP32 boards.
  5. Testing the Communication:

    Once the code is uploaded, open the Serial Monitor for each board. You should see the data being sent and received between the two boards. This setup demonstrates the basic two-way communication using ESP-NOW.

Practical Applications:

ESP-NOW’s two-way communication capability can be employed in various IoT applications, such as:

  • Remote sensors communicating data back and forth
  • Home automation systems with feedback loops
  • Industrial monitoring systems where quick data exchange is essential

 ESP-NOW is a powerful tool for enabling low-power, fast communication between ESP32 devices. By following this guide, you can set up two-way communication between two ESP32 boards, making your IoT projects more efficient and responsive. Whether you’re building a home automation system or a remote sensor network, ESP-NOW offers a reliable solution for your wireless communication needs.

Skip to content