Master ESP-NOW on the Arduino Nano ESP32: Fast, Router-Free Wireless Communication

Master ESP-NOW on the Arduino Nano ESP32: Fast, Router-Free Wireless Communication
  1. Finding Your Nano ESP32's MAC Address
  2. Configuring the ESP-NOW Receiver
  3. Writing the Transmitter Code
  4. My Hands-On Experience with Nano ESP32 Wireless
  5. Troubleshooting and Range Optimization
  6. Frequently Asked Questions (FAQ)

Finding Your Nano ESP32's MAC Address

To get ESP-NOW up and running on your Arduino Nano ESP32, you have to start by finding the unique MAC address of your receiving board. Because ESP-NOW doesn't rely on a central Wi-Fi router, the sender needs to know exactly which device it is talking to. This is done by targeting the hardware-coded MAC address of the receiver. We can grab this address quickly by uploading a simple helper sketch to the board that will act as our receiver. Open your Arduino IDE, make sure you have selected the Arduino Nano ESP32 board under your board manager, and upload this short snippet of code:
#include "WiFi.h"

void setup() {
  Serial.begin(115200);
  delay(1000);
  WiFi.mode(WIFI_STA);
  Serial.print("Receiver MAC Address: ");
  Serial.println(WiFi.macAddress());
}

void loop() {
  // Nothing to do here
}
Once the code uploads, open your Serial Monitor set to 115200 baud, and hit the reset button on your board. You will see a hexadecimal string formatted like AA:BB:CC:DD:EE:FF. Copy this down immediately because you will need to paste it directly into the transmitter’s code later.
Serial monitor output displaying the successfully retrieved MAC address of the Arduino Nano ESP32 receiver board
Serial monitor output displaying the successfully retrieved MAC address of the Arduino Nano ESP32 receiver board

Configuring the ESP-NOW Receiver

Now that you have your receiver's MAC address, let's write the actual firmware that listens for incoming data. ESP-NOW works by sending structured packets, which means we can pack integers, floats, characters, or custom structs into a single message and send them over the air. For this setup, we'll define a simple data structure. Both the sender and the receiver must share this exact same structure for the communication to work. Here is the code for your receiver board:
#include 
#include 

// Define the structure of the incoming data
typedef struct struct_message {
  char text[32];
  int tempValue;
  float humidityValue;
} struct_message;

struct_message incomingData;

// Callback function executed when data is received
void OnDataRecv(const uint8_t  mac, const uint8_t incomingDataPtr, int len) {
  memcpy(&incomingData, incomingDataPtr, sizeof(incomingData));
  Serial.print("Bytes received: ");
  Serial.println(len);
  Serial.print("Text message: ");
  Serial.println(incomingData.text);
  Serial.print("Temperature: ");
  Serial.println(incomingData.tempValue);
  Serial.print("Humidity: ");
  Serial.println(incomingData.humidityValue);
  Serial.println("-------------------------");
}

void setup() {
  Serial.begin(115200);
  
  // Set device as a Wi-Fi Station
  WiFi.mode(WIFI_STA);

  // Initialize ESP-NOW
  if (esp_now_init() != ESP_OK) {
    Serial.println("Error initializing ESP-NOW");
    return;
  }
  
  // Register the receive callback function
  esp_now_register_recv_cb(esp_now_recv_cb_t(OnDataRecv));
}

void loop() {
  // The receiver stays idle and waits for the callback trigger
}
This sketch configures the board as a station, initializes the ESP-NOW protocol, and sets up a callback function. Whenever a packet arrives, the OnDataRecv function fires, unpacks the data back into our struct, and prints it cleanly to the Serial Monitor.
Code screenshot illustrating the custom data structure shared between the transmitter and receiver ESP-NOW sketch
Code screenshot illustrating the custom data structure shared between the transmitter and receiver ESP-NOW sketch

Writing the Transmitter Code

Next up is the transmitter. This board will continuously send data packets to our receiver using the MAC address we found earlier. You must replace the placeholder array below with your receiver's actual MAC address. We'll initialize the peer information, pair with the receiver, and send dummy sensor readings every two seconds. Let's look at the transmitter code:
#include 
#include 

// REPLACE WITH YOUR RECEIVER MAC ADDRESS
uint8_t receiverAddress[] = {0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF};

typedef struct struct_message {
  char text[32];
  int tempValue;
  float humidityValue;
} struct_message;

struct_message myData;
esp_now_peer_info_t peerInfo;

// Callback function executed 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() {
  Serial.begin(115200);
  WiFi.mode(WIFI_STA);

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

  // Register the send callback to monitor transmission success
  esp_now_register_send_cb(OnDataSent);
  
  // Register peer
  memcpy(peerInfo.peer_addr, receiverAddress, 6);
  peerInfo.channel = 0;  
  peerInfo.encrypt = false;
  
  // Add peer        
  if (esp_now_add_peer(&peerInfo) != ESP_OK){
    Serial.println("Failed to add peer");
    return;
  }
}

void loop() {
  // Populate the data packet with values
  strcpy(myData.text, "Hello from Nano ESP32!");
  myData.tempValue = random(20, 35);
  myData.humidityValue = random(40, 80) / 1.1;

  // Send message via ESP-NOW
  esp_err_t result = esp_now_send(receiverAddress, (uint8_t *) &myData, sizeof(myData));
   
  if (result == ESP_OK) {
    Serial.println("Sent with success");
  }
  else {
    Serial.println("Error sending the data");
  }
  
  delay(2000);
}
Once you upload this sketch, make sure both boards are powered up. Open the Serial Monitor for the transmitter, and you should see "Sent with success" followed by "Delivery Success" as soon as the receiver acknowledges the message.

My Hands-On Experience with Nano ESP32 Wireless

Honestly, I've tried this myself on several custom home-automation setups, and using the Nano ESP32 for direct peer-to-peer messaging is a game changer compared to classic Wi-Fi. I used to rely on local MQTT brokers running on a Raspberry Pi to pass simple temperature data between microcontrollers, which felt bloated and introduced unnecessary lag.
When I swapped over to ESP-NOW, the latency dropped to practically nothing, and my sensor batteries lasted significantly longer since the boards didn't have to go through the lengthy process of handshake negotiation with a local router. The Nano form factor makes it incredibly easy to breadboard, though you do have to be mindful that the pin numbering can sometimes trip you up in the IDE if you aren't using the direct GPIO references.
A schematic diagram showing two Arduino Nano ESP32 boards communicating wirelessly via ESP-NOW, highlighting the sender and receiver setup
A schematic diagram showing two Arduino Nano ESP32 boards communicating wirelessly via ESP-NOW, highlighting the sender and receiver setup

Troubleshooting and Range Optimization

If your packets aren't arriving, the first thing to check is the Wi-Fi channel. ESP-NOW requires both devices to be on the same radio channel to communicate properly. By default, setting peerInfo.channel = 0; allows the boards to find each other, but if you have a noisy local Wi-Fi environment, you might need to force both boards onto a specific channel, like channel 1 or 6. Another common pitfall is power delivery. The ESP32-S3 chip on the Arduino Nano can pull brief, intense current spikes when the internal radio transmitter fires. If you are powering your board from a weak USB port or thin jumper wires, the board might brown out or drop connection. Adding a 10uF or 100uF capacitor across the 3.3V and GND pins close to the board can smooth out these power ripples and keep your connection steady. Finally, keep an eye on your antenna. The Nano ESP32 features a highly integrated, tiny antenna. While it is surprisingly capable, don't expect it to punch through multiple concrete walls without some packet loss. For maximum outdoor range, keep a clear line of sight, and try orienting the boards so their antennas face each other directly.

Frequently Asked Questions (FAQ)

Can I send data to multiple Nano ESP32 boards at the same time?

Yes. ESP-NOW supports both unicast (sending to a specific MAC address) and broadcast. By registering multiple peers on your transmitter, you can send targeted data packets to up to 20 different receiver boards simultaneously, or use a broadcast address (FF:FF:FF:FF:FF:FF) to send to everyone listening on the same channel.

Does using ESP-NOW disconnect my board from local Wi-Fi?

Not necessarily. The ESP32 is capable of running both ESP-NOW and a standard Wi-Fi station connection at the same time. However, because they share the same physical radio, they must operate on the same Wi-Fi channel. If your router changes channels, your ESP-NOW connection will drop unless your code dynamically adjusts to match it.

What is the maximum payload size I can send with ESP-NOW?

The maximum packet payload size for an ESP-NOW message is 250 bytes. This is more than enough for reading sensor data, sending button presses, or passing simple system states. If you need to send larger data packages like images or audio, you will need to split them into smaller chunks and reconstruct them on the receiving end.

Do I need an active internet connection or router for ESP-NOW?

No, you do not need an internet connection or any networking hardware. The boards talk directly to each other using their built-in 2.4GHz Wi-Fi radios without requiring any external router or access point. This makes it perfect for remote outdoor setups.

Need Digital Solutions?

Looking for business automation, a stunning website, or a mobile app? Let's have a chat with our team. We're ready to bring your ideas to life:

  • Bots & IoT (Automated systems to streamline your workflow)
  • Web Development (Landing pages, Company Profiles, or E-commerce)
  • Mobile Apps (User-friendly Android & iOS applications)

Free consultation via WhatsApp: 082272073765

Posting Komentar untuk "Master ESP-NOW on the Arduino Nano ESP32: Fast, Router-Free Wireless Communication"