ESP8266 (Web Client – Part1): TCP/IP communication (examples ESP8266WiFi and ESP866HTTPClient)

With the adaptation for the ESP8266 modules of the Arduino libraries WiFiClient (ESP8266WiFi), HTTPClient (ESP8266HTTPClient), it is very easy to exchange data with a home automation server or an online service over TCP/IP protocole, to drive the GPIO of the ESP8266. In the previous article, we saw how to set up a Web server stored in the ESP82266 SPIFFS file system. This tutorial is the first in a new series of articles devoted to communication with an ESP8266 client.

In this first tutorial, we will discover the ESP8266WiFi, ESP8266HTTPClient and ESP8266WiFiMulti libraries.

ESP8266WiFi / ESP8266wifi, the confusion!

If you’re new to programming ESP8266, you probably have met these two libraries that have a very similar name on the internet. The first is the “official” library (ESP8266WiFi) installed at the same time as the ESP8266 cards. It comes with many examples, especially those offered by Wemos to accompany these cards Wemos D1 mini and Wemos R2. The GitHub repository can be found here. Like many ESP8266 libraries adapted from Arduino code, there is no documentation, we discover the functions using the examples or go to the original libraries

Its namesake, ESP8266wifi is an equivalent library developed by Jonas Ekstrand. It is not available from the library manager, it will have to be installed manually by decompressing the repository in the libraries directory of the Arduino IDE, then restarting it. Jonas Ekstrand’s library is much more complete than the original library. Let’s say it combines several functions into a single library. It allows to manage the ESP8266 as an access point, to monitor the arrival of messages, to manage the automatic re-connection …

It’s up to you to choose the one that best suits your needs. In this tutorial, we will use the adaptation of the Arduino ESP8266WiFi library.

The ESP8266WiFi library

The ESP8266WiFi library is installed at the same time as the ESP8266 support. If not, you can install it directly from the library manager.

Example: a watchdog

To discover how the bookstore works, it is best to go through a small example. We will connect to the test server (Node.js or SocketTest) and send it the elapsed time since the start of the ESP8266 (obtained with the millis () function) as well as the IP address of the module. We’ll stay tuned until we get a response from the server (You are alive!).

A watchdog can be useful to monitor that the module is still running and send an alert. For this example, I have adapted the code of the example WiFiClient installed at the same time as the board Wemos D1 R2 & mini.

One must therefore first establish a connection with the WiFi network. This is done by the WiFi.begin class.

The program is continued only when a connection has been established. Be careful, this code is perfectly unsuitable for battery operation. In this case, it is better to put a counter and sleep the ESP to start again later after several unsuccessful attempts.

Now that we have a WiFi connection, we will send a message to the server at regular intervals. To do this, you must create a client object that will connect to the server.

A string is then constructed in which the data sent to the server is encoded. Here the time since the start of the ESP8266 as well as the IP address of the latter.

All you have to do is send the message to the server

If like me, you find that writing this command is difficult, we will see how to improve it right after.

Here is the complete sketch code.

HTTPS secure connection: WiFiClientSecure

Securing the HTTP connection is not supported natively by the ESP8266WiFi library. To do this, you must integrate the WiFiClientSecure.h library. If you need it, here is an example available on GitHub as well as the discussion that deals with the subject.

The ESP8266WiFiMulti library

Before introducing the ESP8266HTTPClent library, let’s talk about the ESP8266WiFiMulti library. The goal of this library is to let the ESP8266 choose the WiFi network to which to connect based on its availability and signal strength. This library is included with the ESP8266WiFi library.

It is very simple to use. One like to create a wifiMulti object.

The different access points are added in a list using the class wifiMulti.addAP()

We open a connection with the run() class.

Several keys are available to test the correct operation of the connection:

  • WL_CONNECTED: Connection is established
  • WL_NO_SSID_AVAIL: no access point specified in the list is available
  • WL_CONNECT_FAILED: Connection failed. Probably the password is bad or a filtering by MAC address has been set up on the router.

To see what happens, you can add this option in the setup()

The ESP8266HTTPClient library

As you saw in the previous example, you send a message to the server using the function client.print () where you pass a string containing the HTTP command. It is effective but not necessarily super friendly to use especially if one is not a pro of the Web. To do something “cleaner” and more advanced, there is the ESP8266HTTPClient library. It is also an adaptation of an Arduino library (ArduinoHTTPClient) for the ESP8266. It is available from the Arduino IDE library manager.

We will therefore create an http object at the beginning of the sketch

The url is constructed in exactly the same way as before.

Before sending the query we pass the parameters to the http object like this

We now have three distinct classes that correspond to the most traditional HTTP requests: GET (data request), POST (data sending) and PUT (file sending). Here we will make a GET request to simulate the reception of a response from the server

Each class returns a code to find out if the query was successful. The code complies with RFC7231 (source).

It is therefore very easy to know if the message has been received by the server. The content of the message (payload) is retrieved using the http.getString() class.

Here is the modified code based. The ESP8266 will look for the best WiFi network to connect to. During the idle phase, WiFi is switched off using the WiFi.disconnect() class.

You can wait for a response from the server. If you are transmitting to a home automation server, there is a big change that it returns you a status: OK, minimum, you should receive a code reply 200 meaning that the message was correctly received by the server. You can imagine other treatments according to the answer: to put in a stack and to start again later, to delete the measure or to store it in a log, to add in a log the event and the error message (to facilitate the Focus), control the GPIO (flash a LED) …

Node.js test server to communicate with ESP8266

To test the different libraries, I propose you to quickly develop a small server Node.js. In the next tutorials, we will go further by setting up a complete communication with some domotic servers (Jeedom, Domoticz, Home Assistant). If you’re new to Node.js, follow this tutorial to install and discover Node.js

For this server, you will need to install the moment, express packages.

Open a text editor and create a new file called server.js by pasting this code

Open the Terminal (or PowerShell on Windows). Move to the directory where you saved the server.js file and start the server with node server.js  or nodemon server.js  (nodemon restarts the server every time the files are changed).

For each message received, the server retrieves the data passed in parameters and constructs an information message which is then sent to the terminal.

serveur test communication tcp ip http esp8266wifi

SocketTest: a test server for

If you do not have the courage to get started with Node.js, you can opt for SocketTest which is a small program that starts a test server on your machine. You can retrieve it from Sourceforge here. SocketTest runs on macOS (start SocketTest.jar), Windows (SocketTest.exe), and Linux (

 sockettest sourceforge test esp8266wifi

Go to the Server tab and enter the ip address of your machine. It is absolutely necessary to indicate the ip address of the machine on which SocketTest works so that it works.

Then start the server by clicking Start Listening. You will receive your first messages (depending on the send frequency set by the watchdog variable).

sockettest watchdog esp8266wifi

You can send a response to the ESP8266 by entering it in the message field and then clicking Send. For example here, I answered OK which appears in the console under S: OK. If you have opened the serial monitor on the Arduino IDE, you will see the received message. If you do not respond within the time specified in the program (here 5 seconds), you will get an error message (>>> Client Timeout!) Before the client disconnects.

Note. Replies sent by SocketTest are not received with the ESP8266HTTPClient library. To dig…

sockettst watchdog send response esp8266wifi client

You now know the 3 main libraries allowing to communicate an Arduino / ESP8266 program in TCP / IP using HTTP requests. We now have to put all this into practice on concrete cases. We will see in the next tutorials how to simply send temperature measurements (or anything else for that matter) to Jeedom, Domoticz and Home Assistant.

Subscribe to the weekly newsletter

No spam and no other use will be made of your email. You can unsubscribe anytime.

  • mattbelle

    Hi there, I’m new to the field, can you tell me where to start as I didn’t understand most of what you wrote 😅

DIY Projects