Skip to content

Networking and Communication


In Week11 project I work on to demonstrates direct node-to-node wireless communication using two ESP32 microcontrollers. The system uses the ESP-NOW protocol, allowing communication without internet or external servers.

SSTM

SSTM

Assignments

Group Assignment

Send a message between two projects

In the group assignment, two independent projects will communicate by exchanging messages using a selected communication protocol (such as ESP-NOW, I2C, or UART). Each project will act as a node and demonstrate successful data transmission between systems. We are work on MQTTs things.

Individual Assignment

I am using ESP WROOM 32D to design, build, and connect wireless node(s) with network MAC addresses.

Two nodes were developed:

  • Node A (Sender) → Reads input from sensors
  • Node B (Receiver) → Controls output devices

System Architecture

System Architecture


Software

  • On Arduino IDE install the ESP library from the espressif (critical)
  • Arduino IDE will be used to generate the MAC address of the Node B and write in the code by save it somewhere for code t be write to Node A

ESP-NOW Project — Initial IDE Setup

1. Install Arduino IDE.

not installed befor/ if required

  • Download and install Arduino IDE
  • Open the software after installation

2. Install ESP32 Board Package

  1. Go to File → Preferences
  2. In “Additional Board Manager URLs”, paste:

[ESP-32 espressif download](https://dl.espressif.com/dl/package_esp32_index.json) 3. Go to Tools → Board → Boards Manager 4. Search for:

esp32 5. Install ESP32 by Espressif Systems


3. Select ESP32 Board

  • Go to Tools → Board → ESP32 Arduino → ESP32 Dev Module

4. Select Port

  • Go to Tools → Port
  • Select the port connected to your ESP32

5. Required Libraries

These are included automatically with ESP32:

  • WiFi.h
  • esp_now.h

No manual installation is required.

ESP espressif setup ESP espressif setup


6. Find MAC Address

Upload the following code to each ESP32:

#include <WiFi.h>

void setup() {
  Serial.begin(115200);
  WiFi.mode(WIFI_STA);

  Serial.print("MAC Address: ");
  Serial.println(WiFi.macAddress());
}

MAC ADDRESS

void loop() {}

Open Serial Monitor and note the MAC address:

Example: 7C:9E:BD:F6:5F:D4

Convert it to ESP-NOW format:

uint8_t address[] = {0x7C, 0x9E, 0xBD, 0xF6, 0x5F, 0xD4};

7. Define Node Roles

Node Role
Node A Sender
Node B Receiver

8. Set WiFi Mode (Both Nodes)

WiFi.mode(WIFI_STA);

9. Initialize ESP-NOW

esp_now_init();

10. Add Peer (Sender Only)

esp_now_peer_info_t peerInfo = {};
memcpy(peerInfo.peer_addr, receiverAddress, 6);
esp_now_add_peer(&peerInfo);

11. Register Receive Callback (Receiver Only)

esp_now_register_recv_cb(onReceive);

12. System Overview

  • Node A sends data using ESP-NOW
  • Node B receives data and controls output devices
  • Communication is direct (no internet or router required)

13. Common Errors and Fixes

Problem Cause Solution
WiFi.h not found Wrong board selected Select ESP32 board
No communication Incorrect MAC address Verify MAC
Send failed Peer not added Add peer correctly
No data received Callback missing Register receive callback

14. Ready for Implementation

After completing setup:

  • Upload sender code to Node A
  • Upload receiver code to Node B
  • Power both boards
  • Test communication using Serial Monitor

Succesfull ESP Communication

succesfull_esp_communication

This completes the initial setup for ESP-NOW communication. When ever I face Error … I copied and debug with AI… with prompt like ” could you correct the error in the following code”

error debug

Hardware Components

Node A (Sender)

  • MCU: ESP32 WROOM 32D
  • Input device: Flame Sensor KY-026
  • Input device: Rotary Encoder KY-040

Node B (Receiver)

  • MCU: ESP32 WROOM 32D
  • Out put device: RGB LED KY-016 (with resistors)

Connection Details

Node A (Sender)

Flame Sensor

Pin Connection
VCC 3.3V
GND GND
DO GPIO 34

Rotary Encoder

Pin Connection
CLK GPIO 25
DT GPIO 26
SW GPIO 27 (optional)
VCC 3.3V
GND GND

Setup and correction


Node B (Receiver)

RGB LED (Common Cathode)

Pin Connection
Red GPIO 14
Green GPIO 12
Blue GPIO 13
GND GND

Note: Each color pin uses a 220Ω resistor but my module have already installed resistor.


Network Addressing

Node MAC Address
Node B (Receiver) 7C:9E:BD:F6:5F:D4

Node A sends data directly to this address using ESP-NOW.


Communication Method

Feature Description
Protocol ESP-NOW
Type Wireless
Internet Required No
Communication Direct device-to-device

System Operation

  1. Flame sensor detects fire and sends a signal
  2. Rotary encoder changes value and sends control data
  3. Node A transmits data via ESP-NOW
  4. Node B receives data and reacts:

  5. Flame detected → Red LED ON

  6. Rotary changes → LED color changes

Flowchart

Node A (Sender)

ESP-NOW Flame Detection and Rotary encoder Workflow general description

ESP-NOW

This is described below as workflow illustrates the logical sequence for an ESP-NOW based flame detection system. The system continuously listens at Node A for data, evaluates flame detection, and responds with LED control at Node B.

Node A (Sender)

A. Workflow Steps for Flame sensor

1. Start

System initialization begins.

2. Initialize ESP-NOW

  • Configure ESP-NOW communication protocol
  • Set up MAC Address

3. Wait for Data

  • Device enters listening mode
  • Await incoming sensor data packets

4. Receive Data

  • Capture transmitted data from flame sensor
  • Parse and validate data integrity

5. Decision: Flame Detected?

  • YES → Turn RED LED ON
  • Immediate visual alert for flame presence

  • NO → Change RGB Color

  • Cycle or update RGB LED to indicate normal status

B. Workflow Steps for Rotary Encoder

ESP-NOW Rotary Encoder to RGB LED Workflow

Node A (Sender – Rotary Encoder)

1. Start

Begin system operation.

2. Initialize ESP32 + ESP-NOW

  • Configure ESP32 microcontroller
  • Set up ESP-NOW communication protocol

3. Read CLK and DT Pins

  • Monitor rotary encoder signals

4. Detect Rotation Direction

  • Determine clockwise or counterclockwise movement

5. Increase or Decrease Counter

  • Adjust counter value based on rotation

6. Limit Counter (0–255)

  • Ensure counter stays within valid range

7. Send Value via ESP-NOW

  • Transmit counter value to receiver node

8. Repeat Loop

  • Continue monitoring and sending values

Node B (Receiver – RGB LED)

1. Start

Begin system operation.

2. Initialize ESP32 + ESP-NOW

  • Configure ESP32 microcontroller
  • Set up ESP-NOW communication protocol

3. Wait for Data

  • Enter listening mode

4. Receive Rotary Value

  • Capture transmitted counter value

5. Calculate Mode = Value % 3

  • Compute mode based on modulo operation

6. Decision: LED Color

  • Mode == 0 → RED
  • Mode == 1 → GREEN
  • Mode == 2 → BLUE

7. Update LED Color

  • Change RGB LED to corresponding color

8. Wait for Next Data

  • Return to listening mode

rotary encoder

6. Repeat

  • Loop back to Wait for Data
  • Continuous monitoring cycle

Node B (Receiver)

Workflow Steps

1. Start

System initialization begins.

2. Initialize ESP-NOW

  • Configure ESP-NOW communication protocol
  • Set up peer-to-peer communication

3. Wait for Data

  • Device enters listening mode
  • Await incoming sensor data packets

4. Receive Data

  • Capture transmitted data from flame sensor
  • Parse and validate data integrity

5. Decision: Flame Detected?

  • YES → Turn RED LED ON
  • Immediate visual alert for flame presence

  • NO → Change RGB Color

  • Cycle or update RGB LED to indicate normal status

6. Repeat

  • Loop back to Wait for Data
  • Continuous monitoring cycle

Testing Results

  • Successful wireless communication between two ESP32 nodes
  • Real-time sensor data transmission
  • Immediate response on output device

Challenges and Solutions

Problem Solution
ESP-NOW callback error Updated to correct function format
No communication Fixed MAC address and power
Unstable input Used INPUT_PULLUP
Limited 3.3V pin Shared using breadboard

Breadboard used for 3V3 pin extension


Learning Outcome

This project successfully demonstrates a direct wireless communication system between two embedded nodes using ESP32 and ESP-NOW using MAC address. I lean how to

  • Communication protocols (ESP-NOW)
  • Implementation of node-to-node communication
  • Integration of sensors and to remote outputs

Final work ESP-NOW-Video


Sources Code

Chat GPT used for Library and code generation

RECEIVER CODE

#include <WiFi.h>              //v WiFi library
#include <esp_now.h>           //v ESP-NOW library

int redPin = 14;               //v RED LED pin
int greenPin = 12;             //v GREEN LED pin
int bluePin = 13;              //v BLUE LED pin

typedef struct {               //v structure for received data
  int flame;
  int rotary;
} Data;

Data incoming;                 //v variable to store received data

//v NEW CALLBACK FORMAT (REQUIRED for ESP32 v3.x)
void onReceive(const esp_now_recv_info *info, const uint8_t *incomingData, int len) {

  memcpy(&incoming, incomingData, sizeof(incoming)); //v copy received data

  if (incoming.flame == 1) {        //v flame detected
    digitalWrite(redPin, HIGH);
    digitalWrite(greenPin, LOW);
    digitalWrite(bluePin, LOW);
  } 
  else {                            //v no flame
    if (incoming.rotary % 3 == 0) {
      digitalWrite(redPin, HIGH);
      digitalWrite(greenPin, LOW);
      digitalWrite(bluePin, LOW);
    } 
    else if (incoming.rotary % 3 == 1) {
      digitalWrite(redPin, LOW);
      digitalWrite(greenPin, HIGH);
      digitalWrite(bluePin, LOW);
    } 
    else {
      digitalWrite(redPin, LOW);
      digitalWrite(greenPin, LOW);
      digitalWrite(bluePin, HIGH);
    }
  }
}

void setup() {
  Serial.begin(115200);         //v start serial

  pinMode(redPin, OUTPUT);      //v set RGB pins
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);

  WiFi.mode(WIFI_STA);          //v required mode

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

  esp_now_register_recv_cb(onReceive); //v register callback
}

void loop() {
  //v nothing here
}

SENDER CODE

#include <WiFi.h>
#include <esp_now.h>

//v YOUR MAC ADDRESS USED CORRECTLY
uint8_t receiverAddress[] = {0x7C, 0x9E, 0xBD, 0xF6, 0x5F, 0xD4};

int flamePin = 34;
int clk = 25;
int dt = 26;

int lastStateCLK;
int counter = 0;

typedef struct {
  int flame;
  int rotary;
} Data;

Data data;

void setup() {
  Serial.begin(115200);

  pinMode(flamePin, INPUT);
  pinMode(clk, INPUT);
  pinMode(dt, INPUT);

  lastStateCLK = digitalRead(clk);

  WiFi.mode(WIFI_STA);

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

  esp_now_peer_info_t peerInfo = {}; //v IMPORTANT: initialize properly
  memcpy(peerInfo.peer_addr, receiverAddress, 6);
  peerInfo.channel = 0;
  peerInfo.encrypt = false;

  esp_now_add_peer(&peerInfo);
}

void loop() {
  int flameState = digitalRead(flamePin);

  int currentStateCLK = digitalRead(clk);

  if (currentStateCLK != lastStateCLK) {
    if (digitalRead(dt) != currentStateCLK) {
      counter++;
    } else {
      counter--;
    }
  }

  lastStateCLK = currentStateCLK;

  data.flame = flameState;
  data.rotary = counter;

  esp_now_send(receiverAddress, (uint8_t *) &data, sizeof(data));

  delay(200);
}