ACROBOTIC Industries ACROBOTIC Industries

ESP8266: Visualizing Temp/Humidity Data


Time to complete: 10–15min; Level of difficulty: Beginner

This guide will show you how to quickly set up an ESP8266 Serial to Wi-Fi module to read temperature and humidity data from a DHT-22 sensor and publish it to the web. We’ll set up a widget to easily visualize and monitor the measured data!

List of Materials

Obtaining the Code

For this activity, we'll be loading a program to our ESP8266 Development Board, it's available in our Github repository:

We can open a new window in the Arduino environment and copy-paste the code.  We'll of course need to setup the Arduino IDE to program the ESP8266.  In addition, we'll need to install a library that will allow us to communicate with the DHT-22 sensor.

Step-by-step Video

We've made a video following the process described in this tutorial, if that's your preferred media for tutorials go right ahead:

Web Frontend for Visualizing ESP8266 (ESP-12E) Data

Installing Additional Libraries

In order to use the code we've written for our data visualization application, we'll need to install a libary.  Fortunately, the latest version of the Arduino software (IDE) includes a library manger.  Simply select the menu option Sketch → Include Library → Library Manager... and search for "DHT".  After selecting the latest version from the drop-down menu, click install and you're set!

Arduino IDE Library Manager

Temperature and Humidity Sensor Operation

The DHT family of sensors provide an effective and inexpensive way to collect temperature and humidity data using any microcontroller development board or single-board computer.  The sensors are limited to collect data at around 1Hz, but typically temperature and humidity do not fluctuate on a fast timesacale.

DHT-22/AM2302 Sensor

The sensors are comprised by a capacitive humidity sensor and a temperature-sensitive resistor (thermistor).  Different than simple analog sensors, which change resistance when as they detect a specific parameter, the DHTs have a chip inside that converts the analog sensor measurements to digital values.  The digital signal uses a private communication protocol that shares some similarities with the 1-Wire protocol (a major difference is that the DHTs do not posses an address).

The protocol has, of course, been reversed engineered and ported over to libraries available for most microcontroller development boards and single-board computers.  There are two models in the DHT family, which share a common pinout, shape, and communication protocol:

DHT-11 DHT-22
Ultra low cost Low cost
3~5V power and I/O; 2.5mA max current (requesting data) 3~5V power and I/O; 2.5mA max current (requesting data)
Hum.: 20~80% ± 5%
Temp.: 0~50°C ± 2°C 
Hum.: 0~100% ± 5%
Temp.: –40~125°C ± 0.5°C 
0.5 Hz max sampling rate 1 Hz max sampling rate
Size: 15.5mm x 12mm x 5.5mm Size: 15.1mm x 25mm x 7.7mm
4 pins with 0.1in spacing (VCC, DATA, NC, GND) 4 pins with 0.1in spacing (VCC, DATA, NC, GND)

As you can see, the DHT-22 is a little more accurate and good over a slightly larger range. Both use a single digital pin and are 'sluggish' in that you can't query them more than once every second or two.

Wiring: DHT Dataviz Ubidots

The wiring of the DHT sensor is straight forward following the diagram below.  Ensuring that the board isn't powered, we can place it on a solderless breadboard with the USB micro-B connector facing outwards.  Then, using a jumper wire, connect one of the GND pins on the ESP8266 Development Board to the rightmost pin on the DHT-22 sensor. Also connect the leftmost pin of the DHT-22 sensor (supply voltage) to 3.3VDC.  You can use the vertical rails on the breadboad as we've done, which makes the wiring more manegeable.

ESP8266 Weather Station Wiring Diagram

Proceed to connect the sensor's 2nd pin (from the left) to pin D2 of the board. Finally, connect the 1KOhm resistor between the 3.3V supply voltage and the data pin, which will act as a pull-up on the data signal.

Code Walkthrough: DHT Tester

After double-checking the wiring, we can go ahead and connect the USB micro-B cable to the ESP8266 board and our computer (remember to install the VCP drivers if you haven't done so already).  We can then upload the dht_sensor_test.ino file to ensure the proper operation of the DHT-22 sensor.

In the the first portion of the code  we define the pin that will be used for communicating with the sensor, and the type of sensor we'll be using (the library supports a few models that share the same functionality).  As usual, we also initialize the Serial object to communicate via USB with the computer.

#include "DHT.h"

#define DHTPIN D2     // what pin we're connected to
#define DHTTYPE DHT22   // DHT 22  (AM2302)

DHT dht(DHTPIN, DHTTYPE); // instantiate a DHT object

void setup() {
  Serial.println("DHTxx test!");
  dht.begin(); // call the DHT 'start' method for collecting data

In the second portion of the code, we call the methods that handle the communication with the sensor.  These high-level functions return the data already interpreted.  For example calling the readTemperature() method with no arguments returns the temperature data in Celsius (passing a value of true makes the method return Fahrenheit).

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);

  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println("Failed to read from DHT sensor!");

  // Compute heat index in Fahrenheit (the default)
  float hif = dht.computeHeatIndex(f, h);
  // Compute heat index in Celsius (isFahreheit = false)
  float hic = dht.computeHeatIndex(t, h, false);

  Serial.print("Humidity: ");
  Serial.print(" %\t");

The computeHeatIndex() method returns the Heat Index, which is a function of both humidity and temperature.  As the sensors are a bit sluggish, we set a 2-second delay at the beginning of each measurement iteration to ensure that it's ready for the next cycle.  The loop() function is mostly for sending the measured values to the Serial object, so that they can be visualized on the Serial Monitor window:

Serial Monitor Output of the DHT Sensor Test

A functioning sensor will result in temperature and humidity reading printing to the serial monitor.

Configuring the Ubidots Backend API

In order to publish our Temperature and Humidity data to the web we need two things in place: a place that we can access and store the data, and a way to communicate with it.  The way this is typically done is through a Web Application (place) and an API (way of communicating).

A Web Application is similar to other applications (programs) that run locally on our computers with the distinction that they reside on the web (e.g., Yahoo Mail and Google Maps). The API, in this context, specifies the way we exchange data with the Web Application. 

In this tutorial, we will be using Ubidots as our choice for Web Application, and we will show how to use the specifications of their API in order to store our Temperature and Humidity measurements.  The very first step is to open up a browser and navigate to and setup an account.

Ubidots Account Signup Page

After creating the account we're ready to setup the data source that will provide the API to publish our data.  To do so, we can click on "Sources" from the Top Navigation Menu, and on the new page select "Add Data Source".

Adding a Data Source on Ubidots

Feel free to name the Data Source anything you'd like, in our case we named it "ESP8266 IoT WS".  Hit enter after naming it, and click on the name to go onto the next screen.  Next, we can add a variable to the Data Source that will contain the measurements collected and published by our ESP8266.  In this specific case, we'll want to add two variables of type "Default": one named "Temperature"  and named "Humidity".  

Adding a Variable to a Data Source in Ubidots

After both variables are created, we are going to need their "Variable ID" value so that we can substitute them in our DHT Dataviz Ubidots program.  To do so, from the "Data Source" view we can click on either variables' information icon on the top right-hand corner of the widget.

Getting the Variables' ID Value on Ubidots

We'll be using both "Variable ID" values on the next step.  The last piece we'll need is the security token that prevents other users from posting data to our variables.  In order to get this token, we can use the Top Navigation Menu to select our profile page, which is linked from the drop-down menu under our username.

We can then use the Left Navigation Menu to select "API Keys" and click on the "Create Token" button.  The generated alphanumeric string is the Authentications Token we'll need for the next step!  Unlike we've done below, be sure to keep your token safe (i.e., hidden) smile

Getting the Access Token on Ubidots

Code Walkthrough: DHT Dataviz Ubidots

We're now ready to run our second program DHT Dataviz Ubidots.  As we did in the previous program (DHT Test) we begin by defining a few constants and variables that will be used throughout the code.  And, we also create an instance of the DHT Class.  We'll be using the built-in LED on our ESP8266 Development Board, which is connected to pin 16, for debugging purposes.  In this section of the code is where we modify values that depend on our particular setup:

  • idvariable1: the "Variable ID" alphanumeric string from Ubidots corresponding to "Temperature"
  • idvariable2: the "Variable ID" alphanumeric string from Ubidots corresponding to "Humidity"
  • token: the "Access Token" alphanumeric string from our Ubidots account API settings
  • ssid: the name of our local Wi-Fi network
  • password: the password of our local Wi-Fi network

A step-by-step guide is included on the previous step for how to find these values.  After you've substituted them in the code, you can go ahead and connect the ESP8266 Development Board to the USB port of the computer and upload the code.

#define errorPin 16
#define DHTPIN D2
#define DHTTYPE DHT22

// Instantiates and initializes the dht object

// Define and initialize constants and variables that we'll use later in the code
const int sleepTimeS = 20;  // Time to sleep (in seconds) between posts to Ubidots
long lastReadingTime = 0;
WiFiClient client;
char results[4];

// After creating an account on Ubidots, you'll be able to setup variables where you 
// will store the data. In order to post the measurements to the Ubidots variables,
// we need their "IDs", which are given on the website
String idvariable1 = "------your_temperature_variableID--------";
String idvariable2 = "------your_humidity_variableID----------";

// In addition, we'll need the API token, which is what prevents other users
// Ubidots to publish their data to one of your variables
String token = "---------your_access_token------------";

// We'll also initialize the values for our Wi-Fi network
const char* ssid = "your_WiFi_SSID";
const char* password = "your_WiFi_password";

After initializing the variables we define our setup() function.  Therein we initialize Serial Communication, DHT Sensor, and the Wi-Fi connection between the ESP8266 and your local Wi-Fi network.

  // Initialize Serial (USB) communication, which will be used for sending debugging messages
  // to the computer
  // Start the communication with the DHT sensor by callibg the begin method of the dht object:W
  // Manual delay while the communication with the sensor starts

  // If networks are found, attempt to connect to our Wi-Fi network
  WiFi.begin(ssid, password);

  // While the connection is not established, IDLE inside this while loop
  while (WiFi.status() != WL_CONNECTED) {

The loop() function is very compact, and it only includes calls to other functions that do the hard work.  We use the methods of the DHT Class to get the temperature and humidity measurements from the sensor, and we pass those values to our user-defined function ubiSave_value().  We also print a few messages with the Serial object so that we can debug any problems using the Serial Monitor when the code is running!

  // Read the current temperature and humidity measured by the sensor
  float temp = dht.readTemperature(true);
  float hum = dht.readHumidity();

  // Call our user-defined function ubiSave_value (defined below), and pass it both the 
  // measurements as well as the corresponding Ubidots variable IDs
  ubiSave_value(String(idvariable1), String(temp));
  ubiSave_value(String(idvariable2), String(hum));


As mentioned, the main portion of this program is handled by functions defined outside of loop().  The main one is defined in our code and has the name ubiSave_value().  This function takes two parameters, a Ubidots "Variable ID" string and a value.  The two parameters that are passed are used to form the URL specified by the Ubidots API, where we need to post the data that we want to store in the corresponding variable.

The Ubidots API  tells us that in order to add a value to a variable on one of the Data Sources in our account, we need to send an HTTP request with the following specifications:

  • Method: POST
  • Request-URI:
  • HTTP-Version: HTTP/1.1
  • Host:
  • Content-Type: application/json
  • Content-Length: length of the value that's to be stored
  • X-Auth-Token: our Ubidots account's alphanumeric token
Message Body
  • The data in JSON format (e.g., {"value" : "NNN"} where NNN is a number)

To construct the request within our program, we use the WiFiClient object quite similarly than we would the Serial object.  That is using the WiFiClient's println() method.  This, of course, after connecting to the server (port 80) using the connect() method:

  // Prepare the value that we're to send to Ubidots and get the length of the entire string
  // that's being sent
  int num=0;
  String var = "{\"value\": " + String(value)+"}"; // We'll pass the data in JSON format
  num = var.length();

  // If we get a proper connection to the Ubidots API
  if (client.connect("", 80)) {
    Serial.println("connected ubidots");

    // Construct the POST request that we'd like to issue
    client.println("POST /api/v1.6/variables/"+idvariable+"/values HTTP/1.1");
    // We also use the Serial terminal to show how the POST request looks like
    Serial.println("POST /api/v1.6/variables/"+idvariable+"/values HTTP/1.1");
    // Specify the contect type so it matches the format of the data (JSON)
    client.println("Content-Type: application/json");
    Serial.println("Content-Type: application/json");
    // Specify the content length
    client.println("Content-Length: "+String(num));
    Serial.println("Content-Length: "+String(num));
    // Use our own API token so that we can actually publish the data
    client.println("X-Auth-Token: "+token);
    Serial.println("X-Auth-Token: "+token);
    // Specify the host
    // Send the actual data

The rest of the code in this user-defined function deals with the the case where we can't connect to the server, and also prints the response we get from it if the connection is succesful.

Using the Ubidots Frontend Widgets

Once the code is uploaded to the ESP8266 Development Board the measured temperature and humidity values start being posted to the Ubidots API.  If everything is configured properly, we can then use "Widgets" to build our Frontend and visualize the stored data.  To do this on Ubidots, we simply use the Top Navigation Menu to access the "Dashboard".

For new accounts, there are quite a few example Widgets already in the "Dashboard".  We can remove them (optional) by clicking on the trash can icon on the top right corner of the Widget.   To add our own Widget we click on the icon on the top right corner of the page, and select Chart → Line chart from the options.

Data Visualization Options on Ubidots

After selecting the type of visualization, we can then choose the Data Source and Variable that we want to visualize.  In our case, we find the Temperature and Humidity variables under the My ESP8266 source.

Choosing a Data Source and Variable for Visualization on Ubidots

After repeating the process for both the avaiable variables (Temperature and Humidity) we can start visualizing our data.  The information can be accessed from wherever there's an Internet connection available even if the ESP8266 Development Board isn't physically present nearby!

Visualizing Data on Ubidots

Comments, questions, or concerns? Drop us a line!.