$USD
  • EUR€
  • £GBP
  • $USD
TUTORIALS ESP8266Arduino

ESP8266 Arduino Tutorial: Socket Server

DFRobot Jul 27 2018 813

Introduction

In this tutorial, we will check how to set a socket server on the ESP8266 running the Arduino core. As a socket client for testing, we will use Putty. You can download it for free.

If you are looking on how to set a socket server on a ESP32, then please check this tutorial.

The tests of this tutorial were performed using a DFRobot’s ESP8266 FireBeetle board.

 

The code

To get started, we need to include the ESP8266WiFi.h library, so we can connect the ESP8266 to a WiFi network.

Naturally, we will also need to know the credentials of the network, more precisely, the name (Service Set Identifier or, in short, SSID), and password. We will store them in two global variables, so they are at the top of our code and they are easy to change.

Additionally, we will need an object of class WiFiServer, which we will also declare as global. We will use this object to configure the socket server and to listen to connections from socket clients.

The constructor for the WiFiServer class receives as argument the port where the socket server will be listening to incoming connections. The port is specified as an integer.

Note that the port is one of the parameters that the socket client will need to know in order to be able to reach the socket server hosted on the ESP8266.

#include "ESP8266WiFi.h"

const char* ssid = "yourNetworkName";
const char* password = "yourNetworkPass";

WiFiServer wifiServer(80);

Now we can move on to the Arduino setup function, where we will start by opening a serial connection, to output the results of our program. We will also use the setup function to connect the ESP8266 to the WiFi network, so it can be reached by clients connected to that same network.

At the end of the setup function, we will call the begin method on the WiFiServer object. This method call is used to initialize the socket server. You can check the full setup function below.

void setup() {

  Serial.begin(115200);

  delay(1000);

  WiFi.begin(ssid, password);

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

  Serial.print("Connected to WiFi. IP:");
  Serial.println(WiFi.localIP());

  wifiServer.begin();
}

We will handle the clients on the Arduino main loop. Since this implementation of the socket server is not asynchronous, we will need to poll the WiFiServer object periodically to check if there are available socket clients. For an asynchronous approach, please check this library.

So, to check if there is a client available, we need to call the available method on the WiFiServer object.

This method, which takes no arguments, will return an object of class WiFiClient. Nonetheless, this function is non-blocking, which means it will return a value even if no client is connected.

We can check if a client is indeed connected by using the connected method of the WiFiClient object. One particular thing about the WiFiClient class is that it overrides the C++ bool operator to return the value returned by the connected method, which means we can simply enclose the object on a IF condition to check if the client is connected.

WiFiClient client = wifiServer.available();

if (client) {
  // Client handling code
}

If the client is indeed connected, then we start reading for incoming data while the client is still connected. So we poll the WiFiClient object for data inside a while loop which will break as soon as the client disconnects.

if (client) {

    while (client.connected()) {
      // Poll for data
    }
}

Inside that loop, we check if there is data available to read by calling the available method on the WiFiClient object, which will return the number of bytes available to read, if there are any. If there aren’t any bytes to read, then the function will return zero.

When there are bytes to read, then we simply need to call the read method on the WiFiClient object. Upon reading a byte, we will print it to the serial console.

Note that we should do a small delay between each polling attempt for incoming bytes.

if (client) {

    while (client.connected()) {

      while (client.available()>0) {
        char c = client.read();
        Serial.write(c);
      }

      delay(10);
    }
}

As mentioned, we keep the loop until the client disconnects. When it does, we call the stop method on the WiFiClient object to free the resources and then we end the iteration of the Arduino loop function, so the next iteration starts again at the beginning, checking for new incoming clients.

You can check the full source code below.

#include "ESP8266WiFi.h"

const char* ssid = "yourNetworkName";
const char* password =  "yourNetworkPass";

WiFiServer wifiServer(80);

void setup() {

  Serial.begin(115200);

  delay(1000);

  WiFi.begin(ssid, password);

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

  Serial.print("Connected to WiFi. IP:");
  Serial.println(WiFi.localIP());

  wifiServer.begin();
}

void loop() {

  WiFiClient client = wifiServer.available();

  if (client) {

    while (client.connected()) {

      while (client.available()>0) {
        char c = client.read();
        Serial.write(c);
      }

      delay(10);
    }

    client.stop();
    Serial.println("Client disconnected");

  }
}

 

Testing the code

To test the whole system, first start by compiling and uploading the code to the ESP8266 using the Arduino IDE. As soon as the procedure finishes, open the Arduino IDE serial monitor.

After the device connects to the WiFi network, it should print the local IP obtained. Copy that IP, since we will need to use it on Putty, to connect to the socket server.

Then, after downloading Putty, open it. On the menu, select “Raw” as connection type. On the “Host Name (or IP address)” text box put the IP you have just copied and on the “Port” text box put 80 (remember that this was the value we specified in the Arduino code for our socket server). You can check the mentioned configuration at figure 1.

Putty Socket Client to ESP32 Socket server

Figure 1 – Configuring Putty for a raw socket connection.

After clicking the “Open” button, a command line window should appear. There you can type and send data to the ESP8266.

If you go back to the serial monitor, you should see the content you have sent getting printed, as indicated in figure 2.

ESP8266 Arduino Core Socket Server TCP.png

Figure 2 – Reaching the ESP8266 socket server from Putty.

REVIEW