IoT Temperature and Humidity Project- DHT11 and NodeMCU

IoT Temperature and Humidity Project- DHT11 and NodeMCU

Let’s see how IoT can handle a large problem of temperature and humidity monitoring in the environment, warehouses, control sheds, and greenhouses.

Are you ready? Let’s start.

Get the Tools ready:

In order to start this project, we need the following tools:
1. NOdeMCU with micro USB cable
2. DHT11 Temperature and Humidity sensor
3. BreadBoard
4. Jumper Wires
3. A Thingspeak.com account

iot dht11 buy nodemcu in pakistan
Image Credits: Instructables

Normally, there are two different types of DHT sensors available out there, one mounted on a PCB having 3 pins – also known as DHT11 Breakout and the other without a PCB, having 4 Pins. Although each of them works pretty well and didn’t cause you any problems, but PCB mounted sensor is better because it includes a 10K Ohm pull-up resistor.
Pinouts for both types are shown in the figure below.

dht11 sensor in pakistan temperature sensor pakistan
Image Credits: Instructables

You can also get a complete pack of essential IoT components by grabbing our IoT starter kit, which includes 6 essential sensors including DHT11 breakout sensor thus making it an optimized and value enriched product for IoT enthusiasts and makers in Pakistan.

Step 1: Setting Up ThingSpeak account to get Temperature and Humidity data

ThingSpeak is an online platform that allows us to gather data from IoT devices and analyze it using MATLAB on the go. ThingSpeak’s Api keys are also compatible with other major IoT development boards like Arduino, Raspberry Pi, and Particle Photon.

In order to create an account, navigate to http://www.thingspeak.com. If you already have a MATHWORKS account, you can use the same credentials to log in.

thingspeak iot
dht11 sensor with thingspeak and arduino
ThingSpeak Sign Up

Use your own Email, Location, First and Last name to create a ThingSpeak account.

After sign up, log into thingspeak and click on the ‘New Channel ‘ button. A Channel is something where we can collect data from a single IoT device. if we need to collect data from more than one IoT device, we need to create as many channel as our devices.

thingspeak iot in pakistan buy arduino in pakistan
ThingSpeak New Channel Creation

Every thingSpeak channel includes eight Fields that can be used to collect upto eight different types of data from a single IoT device. Now, as we are using DHT11 sensor to collect two types of data – Humidity, and Temperature so we will enable two fields and name those fields.

thingspeak account iot sensor data arduino nodemcu
Fields in ThingSpeak gif Image

Leave all other fields as is, and click on the ‘Save Channel‘ button at the bottom. After saving, you will see your created channel.

Once you are on the screen shown below click on the edit icon in Field Charts to name each chart separately. Charts are used to show sensor data in a visual form, which we will see when we use our nodemcu and DH11 sensor to send data to this channel later in this tutorial.

buy nodemcu in pakistan thingspeak iot project
ThingSpeak Charts

Write title of ‘Field 1 Chart‘ as Temperature and Field 2 as Humidity and Save them. These titles will help us identify which chart shows temperature data and which one shows humidity data.

sending data from arduino to thingspeak
Editing Titles of ThingSpeak Charts

Now the Charts will look something like this:

working with IoT data in thingspeak
Titles in ThingSpeak Charts

Now Click on the ‘API Keys’ Tab to get Write and Read API keys. These API keys will be used to access your account by NodeMCU board in order to send sensor data specifically to your thingSpeak account. Each account will have different API Keys.

Take note of these API keys or simply bookmark the page so that we can refer to them later in our nodemcu programming section.

thingspeak API keys for arduino

Now you have your thingSpeak account API Keys, Let’s move on to making connections and programming our boards to get things ready.

Step 2: Making Hardware Connections

Connect DHT11 sensors with the help of jumper wires as follows.
DHT11 Signal Pin (labelled with S) -> NodeMCU D5 Pin
DHT11 VCC pin -> NodeMCU 3.3V
DHT11 GND pin -> NodeMCU GND pin

Refer to the image below to understand how these connections are made.

buy dht11 sensor in pakistan
buy arduino in pakistan
Image Credits: circuits4you.com

Hardware connections between DHT11 sensor and NodeMCU are pretty that simple. Connect NodeMCU to your Laptop/PC and Open up the Arduino IDE to write code!

If you are totally new to NodeMCU and Arduino IDE, Read How to get started with setting up Arduino IDE Environment for NodeMCU

Step 3: Programming Time

First, you need to Download and Install two Libraries in Arduino IDE, the DHT Library and the ThingSpeak Library.

DHT11 sensor library Arduino
ThingSpeak library Arduino
Adding Libraries to Arduino IDE

After Installing Libraries, Copy and Paste the following Code and Upload it to NodeMCU. Remember to replace the write API key with your own Write API key. Also replace SSID and Password with your own WiFi network name and password in Wifi.begin method.

//NodeMCU and DHT11 project with ThingSpeak
// A Project by iotechy.com

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <DHT.h>

//constants
#define DHTPIN 14     // what pin we're connected to (NodeMCU pin D5)
#define DHTTYPE DHT11   // 
DHT dht(DHTPIN, DHTTYPE); //// Initialize DHT sensor for nodemcu


WiFiClient client;
HTTPClient http;

String thingSpeakAddress= "http://api.thingspeak.com/update?";
String writeAPIKey = "SBTXH09EAYMQ53ZW"; //replace with your Write API key
String request_string;

float hum;  //Stores humidity value
float temp; //Stores temperature value

void setup() {
    dht.begin();
   WiFi.disconnect();
   WiFi.begin("SSID","PASSWORD"); //replace with your own SSID and Password
  while ((!(WiFi.status() == WL_CONNECTED))){
    delay(300);

  }
}


void loop()
{
    
    //Read data and store it to variables hum and temp
    hum = dht.readHumidity();
    temp= dht.readTemperature();
   
    thingspeakupdate();
}

void thingspeakupdate(){
     if (client.connect("api.thingspeak.com",80)) {
      request_string = thingSpeakAddress;
      request_string += "api_key=";
      request_string += writeAPIKey;
      request_string += "&";
      request_string += "field1";
      request_string += "=";
      request_string += temp;
      request_string += "field2";
      request_string += "=";
      request_string += hum;
      http.begin(request_string);
      http.GET();
      http.end();
      delay(1000);

    }
  }

Open Your ThingSpeak Channel and See IoT in action.

DHT11 sensor with thingspeak and nodemcu
IoT DHT11 sensor data in ThingSpeak

You can also use a water sprinkler to sprinkle water drops on DHT11 sensor to see how it affects humidity. You will get Live Updates in thingSpeak Charts.

How the Code Works?

So, You want to know how the code actually works. Right? Let dig in.

First, we included the necessary Libraries by writing following three Lines of code.

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <DHT.h>

In the below lines, we defined constant values for the DHT11 sensor. These constant names are already defined in DHT Library, so we cannot change their names. We defined the pin where DHT is connected by using DHTPIN constant, which is Pin no. 14 (NodeMCU Pin D5 actually maps to 14 in Arduino IDE). Read about NodeMCU Pin mapping here.

Next, we defined the type of sensor by using DHTTYPE constant, which is DHT11, of course, and then we initialize DHT sensor using DHTPIN and DHTTYPE constants.

//constants
#define DHTPIN 14     // what pin we're connected to (NodeMCU pin D5)
#define DHTTYPE DHT11   // 
DHT dht(DHTPIN, DHTTYPE);

Next, we have created an Object of WifiClient from the ESP8266WiFi library included at the start. This object will allow our NodeMCU to connect to a WiFi network so that it can communicate over the internet. We named this object as Client.

WiFiClient client;    //creating WiFiCLient Object

Similarly, we also created object of HTTPclient from ESP8266HTTPClient library.

HTTPClient http;

Next, we defined some variables which we will use later in the code. We also defined thingspeak server address and our write API key.

String thingSpeakAddress= "http://api.thingspeak.com/update?";
String writeAPIKey = "SBTXH09EAYMQ53ZW"; //replace with your Write API key
String request_string;

float hum;  //Stores humidity value
float temp; //Stores temperature value

In our setup function, we initializes DHT sensor and connect to a WiFi network by specifying WiFi network name and password. A delay of 300 milliseconds is also added to get our board ready, once it’s connected.

void setup() {
    dht.begin();
   WiFi.disconnect();
   WiFi.begin("SSID","PASSWORD"); /replace with your own SSID and Password
  while ((!(WiFi.status() == WL_CONNECTED))){
    delay(300);

  }
}

In our Loop method, we are reading temperature and storing in variable named ‘temp‘. Similarly, humidity will be stored in variable named ‘hum’ after reading from sensor. After that we are calling our custom function named thingSpeakUpdate() which will upload each temperature and humidity value to our thingspeak account.

void loop()
{
    
    //Read data and store it to variables hum and temp
    hum = dht.readHumidity();
    temp= dht.readTemperature();
 
    thingspeakupdate();
}

The thingspeakupdate() function:

In our thingspeakupdate() function, we first verify that our NodeMCU is able to connect to thingSpeak sever at address api.thingspeak.com. If the connection succeeds, we include our write API key along with temperature which will show in Field 1 Chart and Humidty – which will show up in Field 2 chart.

We merge all parameters in a single String named as request_string. This request_string will be sent over http method to thingspeak server by using http.begin().

http.GET() defines that the request will be sent using http GET method instead of POST. At last, we closes the http connection by writing http.end() and defined 1 second delay.

void thingspeakupdate(){
     if (client.connect("api.thingspeak.com",80)) {
      request_string = thingSpeakAddress;
      request_string += "api_key=";
      request_string += writeAPIKey;
      request_string += "&";
      request_string += "field1";
      request_string += "=";
      request_string += temp;
      request_string += "field2";
      request_string += "=";
      request_string += hum;
      http.begin(request_string);
      http.GET();
      http.end();
      delay(1000);

    }
  }

Conclusion:

That’s all makers! Feel free to share this post with your friends and Social media. Play, create, make and share your makings in the comments below. 🙂

This Post Has 5 Comments

  1. AffiliateLabz

    Great content! Super high-quality! Keep it up! 🙂

  2. Electronics

    Great project and very well explained. NodeMCU is really a very handy tool to start with IoT projects.

    1. admin

      Thanks so much. 🙂

  3. BrianZep

    I treasure the knowledge on your site. Many thanks!

    1. admin

      Thanks and welcome Brian 🙂

Comments are closed.