Seamless Communication: Sending Messages Between ESP8266 Modules on a Shared Wi-Fi Network

Seamless Communication: Sending Messages Between ESP8266 Modules on a Shared Wi-Fi Network

Introduction

In the Internet of Things (IoT), seamless communication between devices is key to building smart systems. The ESP8266, a popular Wi-Fi module, allows easy wireless interaction between devices. In this project, we’ll demonstrate how to send and receive messages between two ESP8266 modules on the same Wi-Fi network by setting up one as a server and the other as a client. This simple setup forms the foundation for more complex IoT applications

Parts Required

  1. 2 x ESP8266 Modules (e.g., NodeMCU or ESP-01)
    • These will serve as the server and client for communication.
  2. USB to Micro-USB Cables
    • For powering and programming the ESP8266 modules.

Set Up One ESP8266 as a Server

The first ESP8266 will act as a server, listening for incoming connections from the client (the second ESP8266).

Server Code (ESP8266 A):

#include <ESP8266WiFi.h>

const char* ssid = “your_SSID”; // Replace with your Wi-Fi SSID
const char* password = “your_PASSWORD”; // Replace with your Wi-Fi password

WiFiServer server(80); // Server on port 80

void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println(“Connecting to WiFi…”);
}

Serial.println(“Connected to WiFi”);
Serial.print(“IP Address: “);
Serial.println(WiFi.localIP());

server.begin(); // Start the server
Serial.println(“Server started”);
}

void loop() {
WiFiClient client = server.available(); // Check for incoming clients

if (client) {
Serial.println(“Client connected”);
while (client.connected()) {
if (client.available()) {
String message = client.readStringUntil(‘\n’); // Read incoming message
Serial.print(“Message received: “);
Serial.println(message);

// Respond to the client
client.println(“Hello from Server”);
}
}
client.stop();
Serial.println(“Client disconnected”);
}
}

 

2. Set Up the Second ESP8266 as a Client

The second ESP8266 will act as a client, connecting to the server and sending a “hi” message.

Client Code (ESP8266 B):

#include <ESP8266WiFi.h>

const char* ssid = “your_SSID”; // Replace with your Wi-Fi SSID
const char* password = “your_PASSWORD”; // Replace with your Wi-Fi password

const char* serverIP = “192.168.1.100”; // Replace with the server ESP8266’s IP address
const uint16_t serverPort = 80;

void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println(“Connecting to WiFi…”);
}

Serial.println(“Connected to WiFi”);
}

void loop() {
WiFiClient client;

if (client.connect(serverIP, serverPort)) {
Serial.println(“Connected to server”);

// Send a “hi” message to the server
client.println(“hi”);
Serial.println(“Message sent: hi”);

while (client.connected()) {
if (client.available()) {
String response = client.readStringUntil(‘\n’); // Read server’s response
Serial.print(“Response received: “);
Serial.println(response);
}
}

client.stop();
Serial.println(“Disconnected from server”);
} else {
Serial.println(“Connection to server failed”);
}

delay(5000); // Wait 5 seconds before sending the next message
}

Github link :  https://github.com/makertribe/IoT-Codes/blob/86256d11a19cd864dcd9033f4bbbcf5ccf8dca91/Seamless%20Communication%3A%20Sending%20Messages%20Between%20ESP8266%20Modules%20on%20a%20Shared%20Wi-Fi%20Network