ESP8266 Projects: How to Send A Message From One ESP Module to Another

In this post, we will demonstrate how two ESP8266-12E modules can communicate with each other by sending a predefined message.

The scope in this demonstration does not include talking about how to set up your Arduino IDE to program your module. It is also assumed that you have already programmed an ESP8266-XX before. The objective in this demonstration is to give you a little understanding how to send data from one ESP8266 module to the other.

Step 1: Set Up Your Devices

In this demo project, we will use two ESP modules where one will be assigned as a client device and the other is the server. The client will be our sender of the message “THIS IS AMAZING” and when the server receives it, the message should be available in the serial monitor. An LED is also assigned in the server to indicate that a client has connected.

Step 2: Download the Library

ESP8266 Client-Server Communication

To make everything work, we will need the library containing the ESP8266WiFi.h file. You may download it here:

https://github.com/esp8266/Arduino/tree/master/libraries/ESP8266WiFi

Step 3: Start Coding

Client Code

First, we need to create a client instance that will connect to the server with the configurations initiated. Our server will be listening to port 80 with an IP address “192.168.9.1”. 

#include <ESP8266WiFi.h>

const char* ssid = "ESP8266-AP";
const char* pass = "1234567890";
byte server[] = {192, 168, 9, 1};
const int port = 80;

WiFiClient client;

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

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, pass);

  while(WiFi.status() != WL_CONNECTED) {
    Serial.println(".");
    delay(500);
  }
  Serial.println("Connected");
}

void loop() {
 if(!client.connect(server, port)){
  Serial.print(".");
  delay(500);
  return;
 }
 client.print("THIS IS AMAZING!");
 delay(1000); 
}

We set the device as a station by using WiFi_STA since this device will be connecting to the server as a wireless client. The device then scans through available connections and selects the one with specified host name and passphrase. Until the connection is established, a buffer constantly checks the connection status.

In the loop function, the client regularly connects to the server using ‘client.connect(server, port)’ which returns true/success if the connection is established. When it connects, it then send the message using ‘client.print()’.

Server Code

Next, we need to instantiate a server to listen to port 80. We should also instantiate a client object that we can use when data is available for reading.

#include <ESP8266WiFi.h>


const char* ssid = "ESP8266-AP";
const char* pass = "1234567890";
const int port = 80;

IPAddress ip (192, 168, 9, 1);
IPAddress gateway (192, 168, 0, 1);
IPAddress subnet (255, 255, 255, 0);

WiFiServer server(port);

WiFiClient client;

void setup() {
  Serial.begin(115200);    // Initialize serial communications

  pinMode(2, OUTPUT); 
 
  WiFi.mode(WIFI_AP);
  WiFi.softAP(ssid, pass);
  WiFi.softAPConfig(ip, gateway, subnet);
  
  server.begin();

  IPAddress myIP = WiFi.softAPIP();

  Serial.print("Connection created with ip address: ");
  Serial.println(myIP);
}

void loop() { 
  client = server.available();
  char message[20] = "";
  digitalWrite(2, HIGH);
  
  if(client) {
    digitalWrite(2, LOW);
    int c = 0;
    while(client.available()){
      char* m = message;
      *(m + c) = client.read();
      c++;
    }
  }
  Serial.println(message);
  delay(500);
}

To set the server as an access point is to set it as ‘WIFI_AP’ in the mode function. We then start the connection by calling the ‘softAP’ function with the specified host name and pass phrase.

At this stage, the IP address is not consistent with what we defined in the client. Using ‘softAPConfig’ after the access point is established will force the IP address to configure with the IP address (static) we defined above. Finally, the server starts to listen to the incoming connection using the ‘begin()’ function.

In the loop, we check for any incoming data from our client. After the ‘client.print(message)’ occurs (from the client), this function will return a client object (in the if-condition) that contains the message we can read.

We then check the size of the data in bytes using ‘available()’. We use this function inside our while-loop so that we can keep reading until no more data is available. For reading the message, we use the ‘read()’ function. This function keeps the next character after the last call of this function. Using this function together with ‘available’ inside a loop will enable us to read a string of characters.

Step 4: Upload the Codes

Upload the codes to the modules while keeping the server devices connected to the pc via serial. In the serial monitor, you should be able see an output similar to this:

Serial Monitor Display

And that’s it!

Have you tried communicating your ESP modules by sending messages? Let us know in the comment section below. If you liked this demo project, don’t forget to share it with your friends!

Leave a Reply

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