ESP8266: Upload data to a server using WiFi




    • Connect an ESP8266 module to a WiFi network.
    • Connect a DHT11 sensor and read the temperature and humidity data.
    • Learn to upload the data to a server.

Bill of materials

NodeMCU Protoboard
Dupont wires DHT11 semsor

Previous considerations


Before starting this session we will review some things that we will need to have prepared, and others that we have to consider before starting to do it.

The first is quite obvious, if we want to upload values to a database that we have on a server, we need a server prepared for it. You can make your own, or use a page that allows it, such as https://thingspeak.com/.

We are going to take advantage of the fact that we assemble a server using a Raspberry and we will upload the values to the table that we create in a SQL database.

On the other hand, if you are familiar with esp8266, you will know that there are quite a few different models with that same chip. Here some time ago we decided on the NodeMCU (from model 1.0), which are in our opinion the simplest and most complete, so this tutorial we will do using one of them.

Node MCU board

Connect and program the DHT11 temperature and humidity sensor


The DHT11 is a sensor that allows us to read the temperature and relative humidity. It is not very precise, but it is very cheap and easy to use, and it will serve us well for what we want to do. These sensors are presented in many different ways, with 3 or 4 pins depending on whether they have pull-up resistance included or not, and the order of the pins may vary from one model to another, so it is important that you look at yours. The most common are these two:

distribución de pines

Once we have clear which is ours we just have to connect it. In this case ours has the built-in pull-up resistor and 3 pins, so the connection would be done in this way.

conexion dht11 esp8266 nodemcu

Now we can start with the program, but first we will have to download the pluggin for the esp8266 if you have never used it before. Here you have a session in which we indicate how to do it Install the ESP8266 pluggin in Arduino.

Once installed the pluggin the first thing you should do is download the library that we will use, DHT11, if you have never used it before. We start the program including the library and define an instance of the sensor where we declare the pin to which it is connected.

#include <DHT11.h>
DHT11 dht11(D4);

If you look, we have named the pin as D4, and not as 4 as we would with any normal Arduino. And is that the names of the pins do not correspond to the GPIOS in the NodeMCU. In fact, if you try to put 4, it will not work, since the pin that identifies 4 is the one labeled D2. In this image you can see it more clearly:


So to avoid confusion you can put the ‘D’ in front and he pluggin will be responsible for making the relationship.

Otherwise the program is simple. We will use the function dht11.read passing as parameters the variables where we want to save the values, checking that there is not an error. The program would look like this:

#include <DHT11.h>

int pin=2;
DHT11 dht11(pin);

void setup()

void loop()
       int err;
       float temp, hum;
       if((err = dht11.read(hum, temp)) == 0)
             Serial.print("Temperature: ");
             Serial.print(" Humidity: ");
             Serial.print("Error Num :");

Now try to upload the program, making sure that you have selected the NodeMCU as a board. Once loaded, if we open the serial port we will see the temperature and humidity values.

  • You can make both the temperature and the humidity rise by taking a little breath in the sensor, and you will see how the values change. 
Medidas de consola

Upload the values to a database on a server


We only have to upload the values to the server. Basically what we have done has been to adapt one of the programs that come with the example called “WiFiClient”. The first thing we do is include the library (you do not have to download it because it is installed with the pluggin that we have already downloaded), and we will also declare some variables that we will use to connect to the Wi-Fi network, name of the network and password, and the address of the server to which we will upload the data.

#include <DHT11.h>
#include <ESP8266WiFi.h>

DHT11 dht11(D4);

const char* ssid = "Euskaltel-cs7A";
const char* password = "xxxxxxxx";

const char* host = "prometecserver.ddns.net";

In the setup, we establish the connection with the WiFi network and show the IP address assigned to us by the serial port.

void setup()
 // We start by connecting to a WiFi network

 Serial.print("Connecting to ");

 WiFi.begin(ssid, password);

 while (WiFi.status() != WL_CONNECTED) {

 Serial.println("WiFi connected");
 Serial.println("IP address: ");

And in loop, the first thing we will do is obtain the temperature and humidity data in the same way as in the previous section. Once we have them, we will use the WiFIClient class to establish a TCP connection. For this we have to declare a series of Strings from which we will mount the complete URL to which we are going to connect.

  • The symbol “?” indicates that the sending of parameters to the server begins.
  • After the “?” we send the parameters identifying them with their name and setting their value separated by the “&” symbol.
  • In our case, the parameters are the password “pass” and the sensor data “Temperature” and “Humidity”.
  • There are two ways to send the data to the page, through GET or POST. Both send a request and receive an answer although in a slightly different way, and both would work in the program (you can easily find information on their differences).

Now we can send the request to the server:

 Serial.print("connecting to ");

 // Use WiFiClient class to create TCP connections
 WiFiClient client;
 const int httpPort = 80;
 if (!client.connect(host, httpPort)) {
 Serial.println("connection failed");

 // We now create a URL for the request
 String url = "/dht11.php";
 String key = "?pass=1234";
 String dato1 = "&Temperatura=";
 String dato2 = "&Humedad=";

 Serial.print("Requesting URL: ");

 // This will send the request to the server
 client.print(String("GET ") + url + key + dato1 + temp + dato2 + hum + " HTTP/1.1\r\n" +
 "Host: " + host + "\r\n" +
 "Connection: close\r\n\r\n");
 unsigned long timeout = millis();
 while (client.available() == 0) {
 if (millis() - timeout > 5000) {
 Serial.println(">>> Client Timeout !");

And finally we read the server’s response and closed the connection. We will also put a delay to upload the data every 60 seconds. You can adapt the time to the one that best suits you.

  // Read all the lines of the reply from server and print them to Serial
 while (client.available()) {
 String line = client.readStringUntil('\r');

 Serial.println("closing connection");


Now you just have to upload the program and if you open the serial port you will see how it establishes the connection to the WiFi network and sends a request every minute.

esp8266 cliente servidor

And if you now access the server to which you have uploaded and look in the database should be those same values in the table that you have uploaded, in our case the Raspberry that we manage with phpMyAdmin.

tabla base de datos temperatura humedad

With this we believe you will be in a position to adapt this program for your own assemblies and purposes. We will be happy that you send us your projects. We leave you here a video with the result:



    • We have used a DHT11 to read the humidity temperature data using a NodeMcu.
    • We have connected to a WiFi network.
    • To establish a TCP connection with a server.
    • We have uploaded the temperature and humidity data to a server.

No Comments

Give a Reply