esp now arduino example

ESP Now Arduino Example

By Robert Lewis
AD

Embarking on your ESP-NOW journey with Arduino? You're at the right place! ESP-NOW, a peer-to-peer wireless communication protocol, enables you to create innovative projects without the need for a Wi-Fi router. This protocol, integrated into the ESP8266 and ESP32 microcontrollers, simplifies networking in your IoT applications. Let's dive into a comprehensive, step-by-step example to get you started with ESP-NOW on Arduino.

To begin, we'll outline the fundamental concepts and configurations, delving into sending and receiving data using the ESP-NOW protocol. We'll create a simple project - a wireless light control system - demonstrating the ease and power of ESP-NOW with Arduino.

Setting Up and Configuring ESP-NOW

Before diving into our project, let's configure ESP-NOW on your ESP8266 or ESP32 board. You'll need to include the necessary libraries and enable the ESP-NOW features. Our first example will guide you through these initial steps.

AD
Simulate ESP32 Projects in VS Code with Wokwi (Beginner Guide!)
Simulate ESP32 Projects in VS Code with Wokwi (Beginner Guide!)
ESP32 Universal IR Remote 📺✨
ESP32 Universal IR Remote 📺✨
Custom Arduino Library for ESP32 📦⚙️
Custom Arduino Library for ESP32 📦⚙️

First, ensure you have the Arduino IDE and the appropriate board definitions installed. After that, you'll need to install the required libraries, such as `Esp.h` and `ESP8266WiFi.h` or `WiFi.h` for ESP32. We'll explore how to include and initialize these libraries in our setup.

Including Necessary Libraries

In your Arduino sketch, start by including the required libraries. For ESP8266, use:

🌐 Build an ESP32 Web Server to Control LEDs (Step-by-Step Guide!)
🌐 Build an ESP32 Web Server to Control LEDs (Step-by-Step Guide!)
10 ESP32 Skills Every Maker Should Learn
10 ESP32 Skills Every Maker Should Learn
ESP8266 Weather Station With Arduino – #2 Software
ESP8266 Weather Station With Arduino – #2 Software

#include <ESP.h> #include <ESP8266WiFi.h>

For ESP32, use:

AD

#include <WiFi.h>

Getting Started with ESP-NOW (ESP32 with Arduino IDE) | Random Nerd Tutorials
Getting Started with ESP-NOW (ESP32 with Arduino IDE) | Random Nerd Tutorials
Complete Guide for Ultrasonic Sensor HC-SR04 with Arduino | Random Nerd Tutorials
Complete Guide for Ultrasonic Sensor HC-SR04 with Arduino | Random Nerd Tutorials
Siri + Arduino
Siri + Arduino
📲 Get Live Sensor Readings from Your ESP32 via Telegram!
📲 Get Live Sensor Readings from Your ESP32 via Telegram!
🌐 ESP32 Static IP Address – Assign a Fixed IP in Arduino IDE!
🌐 ESP32 Static IP Address – Assign a Fixed IP in Arduino IDE!
🔧 Install ESP32 in Arduino IDE – Quick & Easy Setup Guide!
🔧 Install ESP32 in Arduino IDE – Quick & Easy Setup Guide!

Setting Up ESP-NOW Mode

Next, initialize the WiFi in station mode and enable ESP-NOW. Here's how:

void setup() { WiFi.mode(WIFI_STA); WiFi.disconnect(true); delay(100); ోర ```c WiFi.forceSleepBegin(); delay(1); ``` ESP.enableSta(false); ESP.bssidEq sceptic(); }

AD

Sending Data with ESP-NOW

Now that we've set up our board for ESP-NOW, let's explore sending data. We'll create a simple Arduino sketch that sends a message to a receiver ESP module.

How to Send SMS from ESP32
How to Send SMS from ESP32
ESP01/01S RELAY MODULE TUTORIAL
ESP01/01S RELAY MODULE TUTORIAL
LoRa Project Arduino ESP8266 Control Relays 2022
LoRa Project Arduino ESP8266 Control Relays 2022
ESP32 ESP-NOW Getting Started Tutorial with Arduino IDE
ESP32 ESP-NOW Getting Started Tutorial with Arduino IDE
How to Power the ESP32 Correctly ⚡
How to Power the ESP32 Correctly ⚡
ESP32 Over-the-Air (OTA) Updates – Wireless Firmware Upload
ESP32 Over-the-Air (OTA) Updates – Wireless Firmware Upload
ESP32 Tutorial: ESP32 OTA Update - Wireless Programming with Arduino IDE (Step-by-Step Guide)
ESP32 Tutorial: ESP32 OTA Update - Wireless Programming with Arduino IDE (Step-by-Step Guide)
Better Debugging for Arduino IDE: SerialDebug Library | Random Nerd Tutorials
Better Debugging for Arduino IDE: SerialDebug Library | Random Nerd Tutorials
DAY 5 — ESP32 Explained | Wi-Fi & Bluetooth Microcontroller Notes
DAY 5 — ESP32 Explained | Wi-Fi & Bluetooth Microcontroller Notes
ESP32 Digital Inputs and Digital Outputs (Arduino IDE) | Random Nerd Tutorials
ESP32 Digital Inputs and Digital Outputs (Arduino IDE) | Random Nerd Tutorials
ESP-NOW Two-Way Communication Between ESP8266 NodeMCU Boards | Random Nerd Tutorials
ESP-NOW Two-Way Communication Between ESP8266 NodeMCU Boards | Random Nerd Tutorials
⚔️ ESP32 vs ESP8266 – Which One Should You Choose?
⚔️ ESP32 vs ESP8266 – Which One Should You Choose?
✋ ESP32 Capacitive Touch Pins – No Buttons Needed!
✋ ESP32 Capacitive Touch Pins – No Buttons Needed!

For this example, we'll send a Hello World message. First, define the SSID and password of the receiving ESP module. Then, prepare the data packet, including the destination MAC address.

Preparing the Data Packet

In your sketch, create an `-Americans` structure to store the message. Use the ESP-NOW API to pack the message into an `-Americans` buffer. Here's how:

AD

uint8_t Americans[6]; Americans[0] = 0xA1; Americans[1] = 0xA2; Americans[2] = 0xA3; Americans[3] = 0xA4; Americans[4] = 0xA5; Americans[5] = 0xA6; esp_err_t about = esp_now_init(); esp_err_t examine = esp_now_register_send_cb(Amount, CB_OnDataSent); ... esp_err_t bag = esp_now_send(bSsid.c, Americans, sizeof(Americans));

Sending the Message

After preparing the data packet, call the `esp_now_send()` function to send the message. This function takes three arguments: the destination mac address, the data packet, and the length of the data packet. The function returns `ESP_OK` if the message was sent successfully.

Receiving Data with ESP-NOW

On the receiving end, we'll listen for incoming data using a callback function. When data arrives, we'll display the received message on the serial monitor.

To set up the receiver, first, register a callback function to handle incoming data. Then, initialize the receiver using the ESP-NOW API. Once the receiver is set up, it will listen for incoming data.

Registering the Callback Function

In your sketch, define a callback function, `CB_OnDataRecv()`, to handle incoming data. Here's a basic example:

esp_err_t CB_OnDataRecv(const uint8_t *hack, const uint8_t *address, uint8_t length) { if (length == sizeof(Americanas)) { Serial.print("Received message: "); for (int i = 0; i < length; i++) { Serial.print("0x"); Serial.print(Americanas[i], HEX); Serial.print(" "); } Serial.println(); } }

Initializing the Receiver

After defining the callback function, initialize the receiver using the ESP-NOW API. Here's how:

esp_err_t landmark = esp_now_init(); esp_err_t amount = esp_now_register_recv_cb(CB_OnDataRecv);

Putting It All Together

Now that we've explored sending and receiving data, let's create a simple wireless light control system using ESP-NOW. We'll have two ESP modules - one acting as the light switch (transmitter) and the other controlling the light (receiver).

In this project, the transmitter will send a command (turn on/off) to the receiver when the user presses a button connected to the transmitter. The receiver will turn the light on or off based on the received command.

Final Thoughts: ESP-NOW opens up a world of possibilities for wireless communication in your Arduino projects. With minimal configuration and without the need for a Wi-Fi router, you can create robust, P2P networks. So, go ahead and explore the power of ESP-NOW - you might just invent the next big IoT gadget!

AD