How to make an IoT based Temperature and Humidity monitoring system using Nodemcu and Thingspeak

Hello, welcome back. In this tutorial, we will learn how to make a Temperature and Humidity monitoring system using the Thingspeak web application. It mainly uses the Nodemcu board and DHT11 sensor. Also, this project belongs to IoT technology. We have also presented this project using the Blynk app in a previous tutorial. For that, click this link.

What is the Thingspeak?

Thingspeak is a web application designed for data collection, visualization, and analysis. This is primarily designed for IoT technology. Also, we can monitor the data received through the sensors in real-time. This is an open-source IoT application so we can use it as we want. Also, this application can be used with various development boards like Arduino, Nodemcu, and raspberry pi.

The process of this system

When power On this system, the Nodemcu board connect to the algorithm development system called MATLAB via the Thingspeak cloud. Through this, the values obtained from the DHT11 sensor can be viewed on the thingspeak interface as it is real time visualize.

OK, let’s do this project step by step. The required components are given below.

Disclosure: These Amazon links are Affiliate links. As an Amazon Associate, I earn from qualifying purchases.

Step 1

Firstly, identify these components.

Step 2

Secondly, connect these components. To do this, use the circuit diagram below.

Step 3

Thirdly, let’s set up the Thingspeak application step by step. For that, follow the steps below.

  • First, go to the Thingspeak website.
  • Then, click the Get start button and create a new account on Thingspeak.
  • Now, includes your Email address, Location, and name. Then, click the “Continue” button. The next step is to click “Continue”
  • Now, open your email address and click the “Verify email” button. Then close this tab.
  • Next, come back to the previous tab and click the “Continue” button. After, enter the password as you like.
  • OK, now the account is ready. Then click the “OK” button to continue.
  • Now let’s create a new channel. To do this, click on the “New Channel” button and name it as you like. After, enable Field 1 and Field 2 and name them as you like. Then, click the “Save channel” button.
  • Now, we can see the Thingspeak project interface.
  • Then, include two Gauge widgets for this interface. Set up these as follows.
  • OK, thingspeak application set up is done.

Step 4

Now, let’s creates the program for this project. It is as follows.

/*Temperature and Humidity monitoring system with Thingspeak
 * https://srituhobby.com
 */
 
#include <ESP8266WiFi.h>
#include "DHT.h"

String apiKey = "";
const char *ssid =  "";
const char *pass =  "";
const char* server = "api.thingspeak.com";

DHT dht(D2, DHT11);
WiFiClient client;

void setup() {
  Serial.begin(115200);
  delay(10);
  dht.begin();
  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)) {
    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("\t");
    Serial.print("Humidity: ");
    Serial.println(h);

  }
  client.stop();
  delay(1000);
}

Code explanation

Firstly, libraries are included.

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

Secondly, includes the Thingspeak API code and WIFI connection details.

String apiKey = "GY1KDV78P7KALP12";
const char *ssid =  "SriTu Hobby";
const char *pass =  "123456";
const char* server = "api.thingspeak.com";

Next, objects are created for these libraries.

DHT dht(D2, DHT11);//sensor PIN and sensor type
WiFiClient client;

In the setup function,

void setup() {
//The serial monitor is begin
  Serial.begin(115200);
  delay(10);
//The DHT11 sensor is begin
  dht.begin();
//The WIFI library is begin
  WiFi.begin(ssid, pass);
// With this code we can see how the Nodemcu board connects with Wi-Fi
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
}

In the loop function,

void loop() {
//These codes obtain temperature and humidity values
  float h = dht.readHumidity();
  float t = dht.readTemperature();
//This code allows you to see if the sensor is working
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
//These codes send values to the Thingspeak project
  if (client.connect(server, 80)) {
    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);
//Temperature and humidity values are printed on the serial monitor
    Serial.print("Temperature: ");
    Serial.print(t);
    Serial.print("\t");
    Serial.print("Humidity: ");
    Serial.println(h);

  }
  client.stop();
  delay(1000);
}

Step 5

Now, select board and port, After, upload this code to the Nodemcu board.

Step 6

Then, go to the Thingspeak interface and click on the “Personal View” tab. OK, enjoy this project. The full video guide is given below. So, we will meet in the next tutorial.

How to make an IoT based Temperature and Humidity monitoring system using Nodemcu and Thingspeak

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *