Make Your Own ESP8266 DHT22 Temperature​ and Humidty Sensor

DZone 's Guide to

Make Your Own ESP8266 DHT22 Temperature​ and Humidty Sensor

Learn how to create a handy Wi-Fi humidity and temperature sensor.

· IoT Zone ·
Free Resource

In this tutorial, we will show how to build a plug-and-play Wi-Fi humidity and temperature sensor connected via EasyIoT Cloud. It shows how to send two parameters with one request with REST API V1. We will use ESP8266, DHT22, and Arduino IDE. 


Historical temperature and humidity chart. Data can be exported to CSV.


  • ESP8266 WiFi module.
ESP8266 ESP-01 Serial WIFI Wireless Transceiver Module ESP8266 ESP-01 Serial WIFI Wireless Transceiver Module


ESP8266 ESP-03 Serial WIFI Wireless Transceiver Module ESP8266 ESP-03 Serial WIFI Wireless Transceiver Module

ESP8266 ESP-12 Serial WIFI Wireless Transceiver Module ESP8266 ESP-12 Serial WIFI Wireless Transceiver Module


ESP8266 ESP-05 Serial WIFI Wireless Transceiver Module ESP8266 ESP-05 Serial WIFI Wireless Transceiver Module


ESP8266 ESP-07 Serial WIFI Wireless Transceiver Module ESP8266 ESP-07 Serial WIFI Wireless Transceiver Module


ESP8266 Lua Nodemcu WIFI Network Development Board ESP8266 Lua Nodemcu WIFI Network Development Board



  • DHT22 humidity sensor.
DHT22 Digital Temperature And Humidity Sensor DHT22 Digital Temperature And Humidity Sensor


  • 3.3V power supply (battery or regulator).
5pcs 3.3V regulator module 800mA 5pcs 3.3V regulator module 800mA


2xAA 3V Battery Holder Box Case Wire 2xAA 3V Battery Holder Box Case Wire




First, we need to register the sensor with EasyIoT Cloud. After registration, we do not need any other configuration because sensor is "Plug and Play." We just need to remember the Instance Id, which is visible under Config->User Info.

Our program is written in Arduino ESP8266 IDE. See Arduino ESP8266 IDE tutorial to learn how to connect our ESP8266 module to a computer to upload the program. The program is available at our GitHub. You will also need the DHT22 library and the REST API V1 library. Be sure to use the library from our GitHub — it's ported to Arduino ESP8266 IDE.

In the program, change the Access Point username, password, and Instance Id. Instance Id is under Config->User Info.

Instance ID:

Instance Id

Change those lines:
#define AP_USERNAME "xxx"
#define AP_PASSWORD "xxx"
#define INSTANCE_ID "xxx"

If you are using Fahrenheit instead of Celsius, use the dht.toFarenheit(temp) function to report temperature in Fahrenheit.

The first time you connect the sensor to the power supply, it will connect to the access point and check the module ID. If the module ID is 0, then  the program will call REST API functions to get a new token, add a new module, and configure its parameters. The module ID and token are then stored in EEPROM. If the module is already added to the cloud (when the module ID is not 0), then the program will skip this procedure and just read the module parameter's ID. Then, it reads the temperature and humidity and sends it to the cloud in one POST. The API function to post more parameter values is SetParameterValues.

#include <ESP8266WiFi.h>
#include "EIoTCloudRestApiV1.0.h"
#include <EEPROM.h>
#include "DHT.h"

//#define DEBUG_PROG 

    #define DEBUG_PRINTLN(x)  Serial.println(x)
    #define DEBUG_PRINT(x)    Serial.print(x)
    #define DEBUG_PRINTLN(x) 
    #define DEBUG_PRINT(x)

EIoTCloudRestApi eiotcloud;

// change those lines
#define AP_USERNAME "xxx"
#define AP_PASSWORD "xxx"
#define INSTANCE_ID "xxx"

#define CONFIG_START 0
#define CONFIG_VERSION "v01"

#define REPORT_INTERVAL 60 // in sec

struct StoreStruct {
    // This is for mere detection if they are your settings
    char version[4];
    // The variables of your settings
    char token[41];
    uint moduleId;
    //bool tokenOk; // valid token  
} storage = {
    // token
    // The default module 0 - invalid module
    //0 // not valid

float oldTemp;
float oldHum;

DHT dht;

String moduleId = "";
String parameterId1 = "";
String parameterId2 = "";

void setup() {


    eiotcloud.begin(AP_USERNAME, AP_PASSWORD);

    // if first time get new token and register new module
    // here hapend Plug and play logic to add module to Cloud
    if (storage.moduleId == 0)
        // get new token - alternarive is to manually create token and store it in EEPROM
        String token = eiotcloud.TokenNew(INSTANCE_ID);
        DEBUG_PRINT("Token: ");

        // remember token
        token.toCharArray(storage.token, 41);

        // add new module and configure it
        moduleId = eiotcloud.ModuleNew();
        DEBUG_PRINT("ModuleId: ");
        storage.moduleId = moduleId.toInt();

        // set module type
        bool modtyperet = eiotcloud.SetModulType(moduleId, "MT_GENERIC");
        DEBUG_PRINT("SetModulType: ");
        // set module name
        bool modname = eiotcloud.SetModulName(moduleId, "Humidity sensor");
        DEBUG_PRINT("SetModulName: ");

        // add image settings parameter
        String parameterImgId = eiotcloud.NewModuleParameter(moduleId, "Settings.Icon1");
        DEBUG_PRINT("parameterImgId: ");

        // set module image
        bool valueRet1 = eiotcloud.SetParameterValue(parameterImgId, "humidity.png");
        DEBUG_PRINT("SetParameterValue: ");
        // now add parameter to display temperature
        parameterId1 = eiotcloud.NewModuleParameter(moduleId, "Sensor.Parameter1");
        DEBUG_PRINT("parameterId1: ");

        //set parameter description
        bool valueRet2 = eiotcloud.SetParameterDescription(parameterId1, "Temperature");
        DEBUG_PRINT("SetParameterDescription: ");
        //set unit
        // see http://meyerweb.com/eric/tools/dencoder/ how to encode °C 
        bool valueRet3 = eiotcloud.SetParameterUnit(parameterId1, "%C2%B0C");
        DEBUG_PRINT("SetParameterUnit: ");

        //Set parameter LogToDatabase
        bool valueRet4 = eiotcloud.SetParameterLogToDatabase(parameterId1, true);
        DEBUG_PRINT("SetLogToDatabase: ");

        bool valueRet5 = eiotcloud.SetParameterAverageInterval(parameterId1, "10");
        DEBUG_PRINT("SetAvreageInterval: ");

        // now add parameter to display humidity
        parameterId2 = eiotcloud.NewModuleParameter(moduleId, "Sensor.Parameter2");
        DEBUG_PRINT("parameterId2: ");

        //set parameter description
        bool valueRet6 = eiotcloud.SetParameterDescription(parameterId2, "Humidity");
        DEBUG_PRINT("SetParameterDescription: ");
        //set unit
        bool valueRet7 = eiotcloud.SetParameterUnit(parameterId2, "%");
        DEBUG_PRINT("SetParameterUnit: ");

        //Set parameter LogToDatabase
        bool valueRet8 = eiotcloud.SetParameterLogToDatabase(parameterId2, true);
        DEBUG_PRINT("SetLogToDatabase: ");

        bool valueRet9 = eiotcloud.SetParameterAverageInterval(parameterId2, "10");
        DEBUG_PRINT("SetAvreageInterval: ");

        // save configuration

    // if something went wrong, wiat here
    if (storage.moduleId == 0)

    // read module ID from storage
    moduleId = String(storage.moduleId);
    // read token ID from storage
    // read Sensor.Parameter1 ID from cloud
    parameterId1 = eiotcloud.GetModuleParameterByName(moduleId, "Sensor.Parameter1");
    DEBUG_PRINT("parameterId1: ");

    parameterId2 = eiotcloud.GetModuleParameterByName(moduleId, "Sensor.Parameter2");
    DEBUG_PRINT("parameterId2: ");

    Serial.println("Status\tHumidity (%)\tTemperature (C)\t(F)");

    dht.setup(2); // data pin 2
    oldTemp = -1;
    oldHum = -1;

void loop() {
    float hum = dht.getHumidity();
    float temp = dht.getTemperature();

    Serial.print(hum, 1);
    Serial.print(temp, 1);
    Serial.println(dht.toFahrenheit(temp), 1);

    if (temp != oldTemp || hum != oldHum)
        eiotcloud.SetParameterValues("[{\"Id\": \""+parameterId1+"\", \"Value\": \""+String(temp)+"\" },{\"Id\": \""+parameterId2+"\", \"Value\": \""+String(hum)+"\" }]");
        oldTemp = temp;
        oldHum = hum;

    int cnt = REPORT_INTERVAL;

void loadConfig() {
    // To make sure there are settings, and they are YOURS!
    // If nothing is found it will use the default settings.
    if (EEPROM.read(CONFIG_START + 0) == CONFIG_VERSION[0] &&
        EEPROM.read(CONFIG_START + 1) == CONFIG_VERSION[1] &&
    for (unsigned int t=0; t<sizeof(storage); t++)
        *((char*)&storage + t) = EEPROM.read(CONFIG_START + t);

void saveConfig() {
    for (unsigned int t=0; t<sizeof(storage); t++)
        EEPROM.write(CONFIG_START + t, *((char*)&storage + t));



Theonnection diagram is shown below. Instead of ESP8266-01, you can use a different type of ESP8266. For the power supply, you can use 2 AA batteries or a 3.3V regulator.

ESP8266 DHT22 connection

ESP8266 DHT22 humidity sensor

ESP8266 DHT22 sensor

And there you have it! Your own temperature and humidity function.

module ,ide ,rest ,cloud ,sensor ,api ,arduino

Published at DZone with permission of Igor Jarc , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}