esp now esp32 arduino examples

Easy ESP32 & Arduino ESP-NOW Communication Examples

By Mark Lee
AD

ESP-NOW is a simple and fast protocol developed by Espressif for their ESP series of microcontrollers, including the popular ESP32. It's great for implementing wireless communication between ESP32 and other ESP modules without needing a network infrastructure like access points or routers. When it comes to Arduino, the ESP32 can be programmed using the Arduino IDE, making it a breeze to use this protocol. Let's dive into ESP-NOW with ESP32 and Arduino, with helpful examples.

ESP-NOW operates in peer-to-peer mode, perfect for creating simple, fast, and secure wireless networks. It uses a part of the 2.4 GHz ISM band, which is unlicensed and free to use. The ESP-NOW protocol allows a given ESP device to broadcast to multiple recipient devices simultaneously, supporting both unicast and broadcast transmissions.

Setting Up ESP-NOW with ESP32 and Arduino

Before we get into the examples, let's discuss how to set up ESP-NOW on your ESP32 using Arduino.

AD
10 ESP32 Skills Every Maker Should Learn
10 ESP32 Skills Every Maker Should Learn
ESP8266 0.96 inch OLED Display with Arduino IDE | Random Nerd Tutorials
ESP8266 0.96 inch OLED Display with Arduino IDE | Random Nerd Tutorials
How to Power the ESP32 Correctly ⚡
How to Power the ESP32 Correctly ⚡

First, make sure you have the ESP32 board added to your Arduino IDE. Go to File > Preferences and add http://arduino.esp8266.com/stable/package_esp8266com_index.json to Additional Boards Manager URLs. Then, head to Tools > Board > Boards Manager, search for ESP32, and install it.

Enabling ESP-NOW in Arduino

In your sketch, include the necessary headers:

DAY 5 — ESP32 Explained | Wi-Fi & Bluetooth Microcontroller Notes
DAY 5 — ESP32 Explained | Wi-Fi & Bluetooth Microcontroller Notes
the esp32 wi - fi is shown with fireworks in the background
the esp32 wi - fi is shown with fireworks in the background
🌐 Build an ESP32 Web Server to Control LEDs (Step-by-Step Guide!)
🌐 Build an ESP32 Web Server to Control LEDs (Step-by-Step Guide!)
```cpp #include #include ```

Then, initialize ESP-NOW and set the broadcast channel to 1 (which is the default channel for ESP-NOW):

```cpp void setup() { WiFi.mode(WIFI_STA); if (esp_now_init() != ESP_OK) { Serial.println("Error initializing ESP-NOW"); return; } esp_now_set_channel(1, NULL); } ```

Sending and Receiving Data with ESP-NOW

Let's explore sending and receiving data using ESP-NOW.

To send data, create a structure for your data, then pack and send it:

```cpp struct Dataset { int value; }; Dataset data; esp_now_send(broadcastAddress, (uint8_t *) &data, sizeof(data)); ```

To receive data, register a callback function and retrieve data in the callback:

```cpp void onDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) { Serial.print("\nStatus: "); Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail"); } void onDataRecv(const uint8_t *mac, const uint8_t *incomingData, int len) { memcpy(&data, incomingData, sizeof(data)); Serial.print("Got: "); Serial.println(data.value); } void setup() { // ... esp_now_register_send_cb(onDataSent); esp_now_register_recv_cb(onDataRecv); } ```

ESP-NOW Examples with ESP32 and Arduino

Now that you have the basics let's explore some examples.

Here's a simple example of two ESP32s sending data back and forth:

```cpp // Sender sketch void setup() { // ... data.value = 10; esp_now_send(broadcastAddress, (uint8_t *) &data, sizeof(data)); delay(1000); } // Receiver sketch void loop() { // ... } ```

In this example, the sender constantly sends data with an increasing value, while the receiver prints incoming data values.

ESP-NOW offers powerful, low-level communication capabilities for ESP32 and Arduino. By leveraging this protocol, you can create complex wireless systems with minimal network infrastructure. So go ahead, grab some ESP32 modules, and start experimenting with ESP-NOW today!

Complete Guide for Ultrasonic Sensor HC-SR04 with Arduino | Random Nerd Tutorials
Complete Guide for Ultrasonic Sensor HC-SR04 with Arduino | Random Nerd Tutorials
ESP32 Universal IR Remote 📺✨
ESP32 Universal IR Remote 📺✨
Custom Arduino Library for ESP32 📦⚙️
Custom Arduino Library for ESP32 📦⚙️
🔧 Install ESP32 in Arduino IDE – Quick & Easy Setup Guide!
🔧 Install ESP32 in Arduino IDE – Quick & Easy Setup Guide!
how to send sms from esp32 on the breadboard with an esp23
how to send sms from esp32 on the breadboard with an esp23
ESP32: 10 Easy Projects for Beginners | Random Nerd Tutorials
ESP32: 10 Easy Projects for Beginners | Random Nerd Tutorials
ESP32 Explained: The Powerful IoT Board
ESP32 Explained: The Powerful IoT Board
Better Debugging for Arduino IDE: SerialDebug Library | Random Nerd Tutorials
Better Debugging for Arduino IDE: SerialDebug Library | Random Nerd Tutorials
Getting Started with ESP-NOW (ESP32 with Arduino IDE) | Random Nerd Tutorials
Getting Started with ESP-NOW (ESP32 with Arduino IDE) | Random Nerd Tutorials
✨ ESP32 Capacitive Touch – Control with Just a Tap!
✨ ESP32 Capacitive Touch – Control with Just a Tap!
esp 32 is a machine that can make you an engineer
esp 32 is a machine that can make you an engineer
🌐 ESP32 Static IP Address – Assign a Fixed IP in Arduino IDE!
🌐 ESP32 Static IP Address – Assign a Fixed IP in Arduino IDE!
⏱️ ESP32 NTP Time Sync – How to Get Accurate Time via Wi-Fi!
⏱️ ESP32 NTP Time Sync – How to Get Accurate Time via Wi-Fi!
ESP32 Telegram Bot: Send Sensor Readings To Your Phone!
ESP32 Telegram Bot: Send Sensor Readings To Your Phone!
ESP32 Over-the-Air (OTA) Updates – Wireless Firmware Upload
ESP32 Over-the-Air (OTA) Updates – Wireless Firmware Upload
AD