Connect One to many ESP32 Without Internet(One master many Slaves)

Untitled-2
This content provide you Experience

Previously you read about How to connect two ESP32 module without internet .

Here you learn how to display data to many slaves (nodes ) by sending data through a single master gateway.
In this blog we are sending temperature and humidity data from one master to many slaves.

Component Required

  1. ESP32 Modules as many you want to connect.
  2. OLLED Display

Setting up ESP32 environment << click in this link to setup your ESP in Arduino IDE

It is better if you go through previous blog where you learn how to connect two ESP module without internet through this link —https://elconics.com/connect-multiple-esp32-without-internet/

Every WIFI device has its unique MAC address
By using that Mac address we are going to send data from one ESP32 to other

SO your first question will be how to get MAC address
Copy this code and past in Arduino IDE
This code will give you MAC Address of every node ESP32

#include "WiFi.h"
 
void setup(){
  Serial.begin(115200);
  WiFi.mode(WIFI_MODE_STA);
  Serial.println(WiFi.macAddress());
}
 
void loop(){

}

Upload this Code in Receiver ESP32 Board to get MAC address of that board

When you get MAC address copy and keep it .
Repeat this step for every node and keep their MAC address for further use.

Install library in Arduino IDE

Goto tools >> manage libraries >> install this two libraries given below >>

Adafruit GFX Library
Adafruit SSD1306

Now setting up Sender Gateway

Connect DHT22 according to this Circuit diagram

Copy this code below and paste in Arduino IDE >> change MAC address to your receiver’s mac address which is obtain previously .

// ******************Sender ******************
// www.elconics.com

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

// REPLACE WITH YOUR ESP RECEIVER'S MAC ADDRESS
uint8_t broadcastAddress1[] = {0x48,0x55,0x19,0xC8,0xD9,0xEE}; //1st receiver's MAC Address  
uint8_t broadcastAddress2[] = {0x24,0x62,0xAB,0xFE,0xE1,0x30 };//2nd receiver's MAC Address
//uint8_t broadcastAddress3[] = {0xFF, , , , , }; //3rd receiver's MAC Address and so on ..

#define sen 4

typedef struct test_struct {
  int x;
  int y;
} test_struct;

test_struct test;

esp_now_peer_info_t peerInfo;

// callback when data is sent
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
  char macStr[18];
  Serial.print("Packet to: ");
  // Copies the sender mac address to a string
  snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x",
           mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
  Serial.print(macStr);
  Serial.print(" send status:\t");
  Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
}
 
void setup() {
  Serial.begin(115200);
 pinMode(sen,INPUT);
  WiFi.mode(WIFI_STA);
 
  if (esp_now_init() != ESP_OK) {
    Serial.println("Error initializing ESP-NOW");
    return;
  }
  
  esp_now_register_send_cb(OnDataSent);
   
  // register peer
  peerInfo.channel = 0;  
  peerInfo.encrypt = false;
  // register first peer  
  memcpy(peerInfo.peer_addr, broadcastAddress1, 6);
  if (esp_now_add_peer(&peerInfo) != ESP_OK){
    Serial.println("Failed to add peer");
    return;
  }
  // register second peer  
  memcpy(peerInfo.peer_addr, broadcastAddress2, 6);
  if (esp_now_add_peer(&peerInfo) != ESP_OK){
    Serial.println("Failed to add peer");
    return;
  }

}
 
void loop() {
  test.x = analogRead(sen);
  test.y = map(test.x,1,100,0,1023);
 
  esp_err_t result = esp_now_send(0, (uint8_t *) &test, sizeof(test_struct));
   
  if (result == ESP_OK) {
    Serial.println("Sent with success");
  }
  else {
    Serial.println("Error sending the data");
  }
  delay(2000);
}

Setting Up 1st Receiver Node

Connect OLED Display as This circuit diagram below.

Copy this code and upload to your receiver node

//*************************** Receiver 1 ***************************
// www.elcoonics.com

 
#include <esp_now.h>
#include <WiFi.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
//Structure example to receive data
//Must match the sender structure
typedef struct test_struct {
  float x;
  float y;
} test_struct;

//Create a struct_message called myData
test_struct myData;

//callback function that will be executed when data is received
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
  memcpy(&myData, incomingData, sizeof(myData));
  Serial.print("Bytes received: ");
  Serial.println(len);
  Serial.print("x: ");
  Serial.println(myData.x);
  Serial.print("y: ");
  Serial.println(myData.y);
  Serial.println();

   display.clearDisplay();

  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 10);
  // Display static text
  display.println("Temp and Humi");
  display.setTextSize(2);
  display.setCursor(0, 20);
  display.println("Temp" + String(myData.x) );
    display.setTextSize(2);
  display.setCursor(0, 45);
  display.println("Temp" + String(myData.y) );
  
  display.display(); 
}
 
void setup() {
  //Initialize Serial Monitor
  Serial.begin(115200);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
    Serial.println(F("SSD1306 allocation failed"));
    for(;;);
  }

  display.clearDisplay();

  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 10);
  // Display static text
  display.println("Hello, world!");
  
  display.display(); 
  
  //Set device as a Wi-Fi Station
  WiFi.mode(WIFI_STA);

  //Init ESP-NOW
  if (esp_now_init() != ESP_OK) {
    Serial.println("Error initializing ESP-NOW");
    return;
  }
  
  // Once ESPNow is successfully Init, we will register for recv CB to
  // get recv packer info
  esp_now_register_recv_cb(OnDataRecv);
}
 
void loop() {

}

Setting up 2nd Receiver node

Copy and upload this code on second receiver and keep connected to your system to see results in serial monitor.

****************************Receiver 2 code***********

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

//Structure example to receive data
//Must match the sender structure
typedef struct test_struct {
  int x;
  int y;
} test_struct;

//Create a struct_message called myData
test_struct myData;

//callback function that will be executed when data is received
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
  memcpy(&myData, incomingData, sizeof(myData));
  Serial.print("Bytes received: ");
  Serial.println(len);
  Serial.print("x: ");
  Serial.println(myData.x);
  Serial.print("y: ");
  Serial.println(myData.y);
  Serial.println();
}
 
void setup() {
  //Initialize Serial Monitor
  Serial.begin(115200);
  
  //Set device as a Wi-Fi Station
  WiFi.mode(WIFI_STA);

  //Init ESP-NOW
  if (esp_now_init() != ESP_OK) {
    Serial.println("Error initializing ESP-NOW");
    return;
  }
  
  // Once ESPNow is successfully Init, we will register for recv CB to
  // get recv packer info
  esp_now_register_recv_cb(OnDataRecv);
}
 
void loop() {

}

Now you are done Give power supply to all Master and Slave node and see result.

2 thoughts on “Connect One to many ESP32 Without Internet(One master many Slaves)”

  1. Pingback: Connect Many Master to one Slave ESP32 Without

  2. Pingback: Connect Many Master to one Slave ESP32 Without internet

Leave a Comment

Your email address will not be published. Required fields are marked *

Related post