0% found this document useful (0 votes)
7 views

Lab Experiment Manual Introduction to ESP NOW Communication (Lab Session 6)

The document outlines a lab session on the ESP-NOW communication protocol, which allows direct wireless communication between ESP devices without a Wi-Fi network. It details the protocol's key features, working principles, setup procedures, and practical applications in smart home automation and wireless sensor networks. The lab includes code examples for implementing ESP-NOW communication between a sender and receiver using ESP32 or ESP8266 microcontrollers.

Uploaded by

raufafzal29
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Lab Experiment Manual Introduction to ESP NOW Communication (Lab Session 6)

The document outlines a lab session on the ESP-NOW communication protocol, which allows direct wireless communication between ESP devices without a Wi-Fi network. It details the protocol's key features, working principles, setup procedures, and practical applications in smart home automation and wireless sensor networks. The lab includes code examples for implementing ESP-NOW communication between a sender and receiver using ESP32 or ESP8266 microcontrollers.

Uploaded by

raufafzal29
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Analog Digital Communication

Lab Session: 6

University of Engineering and Technology

Department of Electrical Engineering

Analog Digital Communication


Lab Session: 6

Lab Experiment: Introduction to ESP-NOW


Communication
CLOs Applicable: CLO 1, CLO 2 and CLO 3

NAME: ______________________
ROLL NO.:__________________

Objective

To understand the ESP-NOW communication protocol and how it enables direct


wireless communication between ESP devices without requiring a Wi-Fi network.

Theory

1. What is ESP-NOW?

ESP-NOW is a low-power, low-latency wireless communication protocol developed by


Espressif Systems. It allows ESP32 and ESP8266 microcontrollers to communicate
directly without the need for an internet connection or Wi-Fi router.

2. Key Features of ESP-NOW

• No Wi-Fi Network Required: Devices communicate without connecting to a


router.
• Low Power Consumption: Suitable for battery-powered applications.
• Low Latency (~2ms): Faster than traditional Wi-Fi communication.
• Secure Communication: Supports AES encryption.
• One-to-Many Communication: One ESP can send data to multiple ESPs.

3. Working Principle

ESP-NOW works by using the MAC addresses of ESP devices for communication. Unlike
traditional networking, it does not use IP addresses or an internet connection.
Ⓒ UNIVERSITY of ENGINEERING & TECHNOLOGY
Dr. Faheem Gohar Awan
Mr. Muhammad Ali Musawir
Analog Digital Communication
Lab Session: 6

Basic Communication Flow

• ESP1 (Sender) reads the input signal (e.g., button press).


• ESP1 transmits data using ESP-NOW protocol.
• ESP2 (Receiver) receives the data and performs an action (e.g., turns LED
ON/OFF).

4. Why Use ESP-NOW Instead of Wi-Fi?

5. Applications of ESP-NOW

• Smart home automation (wireless switches, remote-controlled lights).


• Wireless sensor networks (weather stations, temperature monitoring).
• Instant remote control (button-based triggering).

ESP-NOW Communication Concept

1. How Does ESP-NOW Work?

ESP-NOW is based on peer-to-peer communication using MAC addresses instead of IP


addresses. The sender must know the MAC address of the receiver to establish
communication.

2. Basic Components of ESP-NOW Communication

• Sender ESP: The device that transmits data (e.g., a button press).
• Receiver ESP: The device that receives data and performs an action (e.g.,
turning on an LED).
• MAC Address: A unique identifier (e.g., 24:0A:C4:8B:12:34) assigned to each ESP
module for communication.
• ESP-NOW Protocol: Handles secure, low-latency transmission between devices.

3. Communication Flow

Ⓒ UNIVERSITY of ENGINEERING & TECHNOLOGY


Dr. Faheem Gohar Awan
Mr. Muhammad Ali Musawir
Analog Digital Communication
Lab Session: 6

The sender and receiver must be paired before exchanging data. The steps are:

Step 1: Finding the MAC Address of Each ESP

• Each ESP has a unique MAC address that can be found using a simple program.
• The sender needs to store the MAC address of the receiver.

Step 2: Initializing ESP-NOW on Both Devices

• Both ESPs must enable ESP-NOW mode and disable Wi-Fi networking.
• The sender registers the receiver’s MAC address to allow communication.

Step 3: Sending and Receiving Data

• The sender transmits a small data packet (e.g., 1 for ON, 0 for OFF).
• The receiver listens and processes the received data.

4. One-to-One vs. One-to-Many Communication

5. Data Format in ESP-NOW

ESP-NOW sends small packets (maximum 250 bytes) containing:

• MAC Address (Receiver)


• Payload Data (Button status, sensor values, etc.)
• Security (Optional)

6. Security in ESP-NOW

• Unencrypted Mode: Default mode, easier to use.


• Encrypted Mode: Uses AES encryption for secure communication (not
supported on ESP8266).

Setting Up ESP-NOW Communication

Equipment Required

• 2 × ESP32 or ESP8266 boards

Ⓒ UNIVERSITY of ENGINEERING & TECHNOLOGY


Dr. Faheem Gohar Awan
Mr. Muhammad Ali Musawir
Analog Digital Communication
Lab Session: 6

• 1 × LED with a resistor (for output on the receiver)


• 1 × Push button with a pull-down resistor (for input on the sender)
• Jumper wires
• USB cables
• Arduino IDE

Theory

To set up ESP-NOW communication, we need to:

1. Find the MAC address of each ESP.


2. Initialize ESP-NOW on both sender and receiver.
3. Register the receiver’s MAC address on the sender.
4. Send and receive data to control an LED based on button presses.

1. Finding the MAC Address

Each ESP module has a unique MAC address that is needed for ESP-NOW
communication.

Steps to find the MAC address:

1. Upload the following code to an ESP module.


2. Open the Serial Monitor to see the MAC address.

#include <WiFi.h>

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

void loop() {}

3. Note down the MAC address of each ESP.


4. Use the receiver's MAC address in the sender’s program.

2. Initializing ESP-NOW

ESP-NOW requires both devices to be in Wi-Fi station mode but without connecting
to a network.

Steps for Initialization

• Call WiFi.mode(WIFI_STA);

Ⓒ UNIVERSITY of ENGINEERING & TECHNOLOGY


Dr. Faheem Gohar Awan
Mr. Muhammad Ali Musawir
Analog Digital Communication
Lab Session: 6

• Call esp_now_init(); to start ESP-NOW.

3. Registering the Receiver

The sender must add the receiver’s MAC address to send data.

• Use esp_now_peer_info_t structure to add the receiver.


• Call esp_now_add_peer() function.

4. Sending and Receiving Data

ESP-NOW transfers data in small packets (e.g., 1 for ON, 0 for OFF).

• The sender sends button press data (0 or 1).


• The receiver listens for the data and controls an LED accordingly.

Implementing ESP-NOW Communication

Circuit Diagram

Sender (ESP 1 - Button Input)

• Connect one terminal of the button to GPIO 4.


• Connect the other terminal to GND.
• Use a 10kΩ pull-down resistor between GPIO 4 and GND.

Receiver (ESP 2 - LED Output)

• Connect the LED’s positive leg (anode) to GPIO 2.


• Connect the negative leg (cathode) to GND through a 220Ω resistor.

ESP-NOW Code Implementation

Sender Code (ESP 1 - Button Input)

#include <WiFi.h>

#include <esp_now.h>

uint8_t receiverMAC[] = {0x24, 0x6F, 0x28, 0x12, 0x34, 0x56}; // Replace


with Receiver MAC Address

const int buttonPin = 4;

int buttonState = 0;
Ⓒ UNIVERSITY of ENGINEERING & TECHNOLOGY
Dr. Faheem Gohar Awan
Mr. Muhammad Ali Musawir
Analog Digital Communication
Lab Session: 6

typedef struct struct_message {

int buttonStatus;

} struct_message;

struct_message buttonData;

void setup() {

Serial.begin(115200);

WiFi.mode(WIFI_STA);

if (esp_now_init() != ESP_OK) {

Serial.println("ESP-NOW Init Failed");

return;

esp_now_peer_info_t peerInfo;

memcpy(peerInfo.peer_addr, receiverMAC, 6);

peerInfo.channel = 0;

peerInfo.encrypt = false;

if (esp_now_add_peer(&peerInfo) != ESP_OK) {

Serial.println("Failed to add peer");

return;

pinMode(buttonPin, INPUT);

void loop() {

buttonState = digitalRead(buttonPin);

buttonData.buttonStatus = buttonState;

Ⓒ UNIVERSITY of ENGINEERING & TECHNOLOGY


Dr. Faheem Gohar Awan
Mr. Muhammad Ali Musawir
Analog Digital Communication
Lab Session: 6

esp_now_send(receiverMAC, (uint8_t *)&buttonData, sizeof(buttonData));

delay(100);

Receiver Code (ESP 2 - LED Output)

#include <WiFi.h>

#include <esp_now.h>

const int ledPin = 2;

typedef struct struct_message {

int buttonStatus;

} struct_message;

struct_message receivedData;

void onDataReceive(const uint8_t * mac, const uint8_t *incomingData, int


len) {

memcpy(&receivedData, incomingData, sizeof(receivedData));

digitalWrite(ledPin, receivedData.buttonStatus);

void setup() {

Serial.begin(115200);

WiFi.mode(WIFI_STA);

if (esp_now_init() != ESP_OK) {

Serial.println("ESP-NOW Init Failed");

return;

esp_now_register_recv_cb(onDataReceive);

Ⓒ UNIVERSITY of ENGINEERING & TECHNOLOGY


Dr. Faheem Gohar Awan
Mr. Muhammad Ali Musawir
Analog Digital Communication
Lab Session: 6

pinMode(ledPin, OUTPUT);

void loop() {

Procedure

1. Upload the sender code to the first ESP module.


2. Upload the receiver code to the second ESP module.
3. Press the button on the sender.
4. Observe the LED on the receiver turning ON/OFF based on the button state.
5. Verify the data transmission using Serial Monitor.

Conclusion

• Summary of what was learned in this lab.


• Why ESP-NOW is useful (low power, no WiFi needed, fast).

Ⓒ UNIVERSITY of ENGINEERING & TECHNOLOGY


Dr. Faheem Gohar Awan
Mr. Muhammad Ali Musawir

You might also like