Connect Two ESP32 Without Internet

eee
This content provide you Experience

Previously you read about LoRaWAN where we send data from node and read to master gateway and upload every data to the Firebase Database .

Here you learn how to connect multiple ESP32 without internet and without Bluetooth , Here you build a network of ESP32 where both sender and receiver communicate with each other in their own network.

Component Required

  1. Two ESP32 Module
  2. OLLED Display

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

Many people told me Lora module is bit costly and multiple LoRa module for College project is bit monetary burden
So, In This blog I am going to make similar kind of network of nodes and gateway without using of internet , Bluetooth for communication.
Yes You heard it correct lets get started

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

#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 .

Now setting up Sender Gateway

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

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

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

// REPLACE WITH YOUR RECEIVER MAC Address
uint8_t broadcastAddress[] = {0XEC,0X62,0X60,0X84,0X35,0X88};  // put your receiver mac address here <<<<<              

// Structure example to send data
// Must match the receiver structure
typedef struct struct_message {
  char a[32];
  int b;
  float c;
  bool d;
} struct_message;

// Create a struct_message called myData
struct_message myData;

esp_now_peer_info_t peerInfo;

// callback when data is sent
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
  Serial.print("\r\nLast Packet Send Status:\t");
  Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
}
 
void setup() {
  // Init 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 Send CB to
  // get the status of Trasnmitted packet
  esp_now_register_send_cb(OnDataSent);
  
  // Register peer
  memcpy(peerInfo.peer_addr, broadcastAddress, 6);
  peerInfo.channel = 0;  
  peerInfo.encrypt = false;
  
  // Add peer        
  if (esp_now_add_peer(&peerInfo) != ESP_OK){
    Serial.println("Failed to add peer");
    return;
  }
}
 
void loop() {
  // Set values to send
  strcpy(myData.a, "Welcome To Elconics Farm");
  myData.b = random(1,20);
  myData.c = 1.2+myData.b;
  myData.d = false;
  
  // Send message via ESP-NOW
  esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &myData, sizeof(myData));
   
  if (result == ESP_OK) {
    Serial.println("Sent with success");
  }
  else {
    Serial.println("Error sending the data");
  }
  delay(2000);
}

Setting Up Receiver Node

Connect OLED Display as This circuit diagram below.

Copy this code and upload to your receiver node

//*************************** Receiver ***************************
// 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 struct_message {
    char a[32];
    int b;
    float c;
    bool d;
} struct_message;

// Create a struct_message called myData
struct_message 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("Char: ");
  Serial.println(myData.a);
  Serial.print("Int: ");
  Serial.println(myData.b);
  Serial.print("Float: ");
  Serial.println(myData.c);
  Serial.print("Bool: ");
  Serial.println(myData.d);
  Serial.println();

    display.clearDisplay();

  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);
  // Display static text
  display.println(myData.a);
 display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(0, 20); 
  display.println(myData.b);
   display.setCursor(0, 40); 
  display.println(myData.c);
  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(;;);
  }
  delay(2000);
  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() {

}

Now you are done you are ready to receive data connect you receiver to your laptop and see updates in serial monitor.

1 thought on “Connect Two ESP32 Without Internet”

  1. Pingback: Connect One to many ESP32 Without Internet(One master many Slaves)

Leave a Comment

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

Related post