You are here
Home > IoT Experiments >

Experiment-9: DHT11 Humidity Temperature Monitor on ThingSpeak with NodeMCU

DHT11 Humidity Temperature Monitor on ThingSpeak with NodeMCU

Objective:

It explains how to log Humidity & Temperature data on the cloud. We can use Thingspeak as a cloud service provider and DHT11 to measure temperature and humidity.

Requirement:

NodeMCU ESP8266 Board, DHT11 humidity and temperature sensor, Connecting Wires, Breadboard

Theory

DHT11 Humidity & Temperature Sensor:

The DHT11 is a basic, ultra low-cost digital temperature and humidity sensor. It uses a capacitive humidity sensor and a thermistor to measure the surrounding air and spits out a digital signal on the data pin (no analog input pins needed).

It’s fairly simple to use but requires careful timing to grab data. The only real downside of this sensor is you can only get new data from it once every 2 seconds, so when using the library, sensor readings can be up to 2 seconds old.

Schematic:

Interfacing DHT11 on ESP

Configuring Thingspeak server

Getting Thingspeak & Getting API Key:

  1. Go to https://thingspeak.com/and create an account if you do not have one. Login to your account.
    2. Create a new channel by clicking on the button. Enter the basic details of the channel. Then Scroll down and save the channel. You can follow the video guide below.
    3. Then go to API keys copy and paste this key to a separate notepad file. You will need it later while programming.

Creating Sketch on Arduino IDE:

To create the sketch for Humidity & Temperature Monitoring using DHT11 & NodeMCU on ThingSpeak we need to do:

  • Write the sketch on Arduino IDE.
  • Download the DHT11/DHT22 library from GitHub and add it to your library manager.
  • Select the NodeMCU ESP-12E board from the board manager.
  • Paste your API Key from thingspeak which you created earlier on a programming section line.
  • Edit the program to change the wifi SSID and password with your own.
  • Compile the code and Upload it to NodeMCU board

Source Code/Program

#include <DHT.h>  // Including library for dht

#include <ESP8266WiFi.h>

String apiKey = “H38TEGNC0XKW43BB”;     //  Enter your Write API key from ThingSpeak

const char *ssid =  “myMOTO”;     // replace with your wifi ssid and wpa2 key

const char *pass =  “helloMOTO”;

const char* server = “api.thingspeak.com”;

#define DHTPIN 0          //pin where the dht11 is connected

DHT dht(DHTPIN, DHT11);

WiFiClient client;

void setup()

{

       Serial.begin(115200);

       delay(10);

       dht.begin();

       Serial.println(“Connecting to “);

       Serial.println(ssid);

       WiFi.begin(ssid, pass);

      while (WiFi.status() != WL_CONNECTED)

     {

            delay(500);

            Serial.print(“.”);

     }

      Serial.println(“”);

      Serial.println(“WiFi connected”);

}

void loop()

{

        float h = dht.readHumidity();

      float t = dht.readTemperature();

                    if (isnan(h) || isnan(t))

                 {

                     Serial.println(“Failed to read from DHT sensor!”);

                      return;

                 }

                         if (client.connect(server,80))   //   “184.106.153.149” or api.thingspeak.com

                      {

                             String postStr = apiKey;

                             postStr +=”&field1=”;

                             postStr += String(t);

                             postStr +=”&field2=”;

                             postStr += String(h);

                             postStr += “\r\n\r\n”;

                             client.print(“POST /update HTTP/1.1\n”);

                             client.print(“Host: api.thingspeak.com\n”);

                             client.print(“Connection: close\n”);

                             client.print(“X-THINGSPEAKAPIKEY: “+apiKey+”\n”);

                             client.print(“Content-Type: application/x-www-form-urlencoded\n”);

                             client.print(“Content-Length: “);

                             client.print(postStr.length());

                             client.print(“\n\n”);

                             client.print(postStr);

                             Serial.print(“Temperature: “);

                             Serial.print(t);

                             Serial.print(” degrees Celcius, Humidity: “);

                             Serial.print(h);

                             Serial.println(“%. Send to Thingspeak.”);

                        }

          client.stop();

          Serial.println(“Waiting…”);

  // thingspeak needs minimum 15 sec delay between updates

  delay(1000);

}

Output:

Summary:

In this experiment we have learnt how to configure thingspeak cloud server for a channel, create WiFi as a station. We also learnt how to interface the DHT11 and read the temperature and humidity in two variables and push the data on thingspeak cloud server. Finally we display the value as text and as a chart.

Viva Questions:

  1. What is the use of thingspeak.com cloud server, How do you configure your channel.
  2. What is the role of APIKEY in the communication between station and the cloud server (thingspeak.com)
  3. How do you connect ESP8266 to WiFi AP in the station (client) mode
  4. Explain the following line of code

WiFi.begin(ssid, pass);

      while (WiFi.status() != WL_CONNECTED)

     {

            delay(500);

            Serial.print(“.”);

     }

      Serial.println(“”);

      Serial.println(“WiFi connected”);

 

Leave a Reply

Top
error: Content is protected !!