Internet of ThingsIoT Tutorials

Building a Smart Temperature and Humidity Monitoring System

A smart temperature and humidity monitoring system is a common IoT (Internet of Things) application that enables real-time monitoring of environmental conditions, such as temperature and humidity, through sensors and IoT devices. This system can be used for various applications, including smart homes, industrial environments, agriculture, and healthcare. By using IoT technologies, we can remotely access the data and trigger actions based on predefined thresholds, ensuring efficiency and convenience.

In this guide, we will walk through how to build a basic smart temperature and humidity monitoring system using common hardware components like sensors, a microcontroller, and cloud services.

Components Needed

To build a temperature and humidity monitoring system, you will need the following components:

  1. DHT11 or DHT22 Sensor:
    • These are widely used sensors that measure temperature and humidity. The DHT11 is cheaper but less accurate and has a smaller range, while the DHT22 provides better accuracy and a wider range of measurements.
  2. Microcontroller (Arduino or ESP8266/ESP32):
    • The microcontroller will act as the brain of the system. It will collect data from the sensor, process it, and send it to the cloud or a local display. Arduino boards (like Arduino Uno) are ideal for beginners, while ESP8266 or ESP32 can be used for Wi-Fi connectivity to send data to the cloud.
  3. Power Supply:
    • Power the microcontroller and sensor with a suitable power supply, such as a 5V USB power adapter or a battery.
  4. Wi-Fi Module (if using Arduino):
    • If you are using an Arduino board (without built-in Wi-Fi), you will need a Wi-Fi module, like the ESP8266, to enable cloud connectivity.
  5. Cloud Platform (AWS IoT, Google Cloud, or ThingSpeak):
    • You will need a cloud platform to store and analyze the data. Platforms like ThingSpeak, AWS IoT, or Google Cloud IoT can be used to visualize the data and set triggers or alerts.
  6. Jumper Wires and Breadboard:
    • These will help you connect the sensor to the microcontroller.

Step-by-Step Guide to Build the System

Step 1: Setting Up the Hardware

  1. Connect the DHT Sensor to the Microcontroller:
    • The DHT11 or DHT22 sensor has three pins: VCC, GND, and DATA.
    • Connect the VCC pin to the 5V power supply of the microcontroller.
    • Connect the GND pin to the ground (GND) of the microcontroller.
    • Connect the DATA pin to a digital input pin on the microcontroller (e.g., pin 2 on Arduino).
  2. Power the Microcontroller:
    • Connect your Arduino or ESP8266/ESP32 to a power source using a USB cable or a suitable adapter.
    • Ensure the Wi-Fi module is connected to the microcontroller (if applicable).

Step 2: Write the Code

Now, you need to write the code to read data from the DHT sensor and send it to a cloud platform.

  1. Install Required Libraries:
    • For Arduino, you can use the DHT sensor library and Wi-Fi library (for ESP8266 or ESP32) to interface with the sensor and connect to Wi-Fi.

    In the Arduino IDE, go to Sketch > Include Library > Manage Libraries, and search for the following libraries:

    • DHT sensor library by Adafruit.
    • Wi-Fi library (for ESP8266 or ESP32).
  2. Sample Arduino Code:
    #include <DHT.h>
    #include <ESP8266WiFi.h>  // For ESP8266, use ESP32WiFi.h for ESP32
    #include <ThingSpeak.h>
    
    // DHT Sensor Settings
    #define DHTPIN 2          // Pin connected to the DHT sensor
    #define DHTTYPE DHT22     // Use DHT11 or DHT22 based on your sensor
    
    DHT dht(DHTPIN, DHTTYPE);
    
    // Wi-Fi Settings
    const char *ssid = "yourSSID";
    const char *password = "yourPassword";
    
    // ThingSpeak Settings
    unsigned long myChannelNumber = yourChannelNumber;
    const char *myWriteAPIKey = "yourWriteAPIKey";
    
    WiFiClient client;
    
    void setup() {
      Serial.begin(9600);
      dht.begin();
    
      // Connect to Wi-Fi
      WiFi.begin(ssid, password);
      while (WiFi.status() != WL_CONNECTED) {
        delay(1000);
        Serial.println("Connecting to WiFi...");
      }
      Serial.println("Connected to WiFi");
    
      ThingSpeak.begin(client);
    }
    
    void loop() {
      // Read temperature and humidity from DHT sensor
      float temperature = dht.readTemperature();  // Temperature in Celsius
      float humidity = dht.readHumidity();        // Humidity in percentage
    
      // Check if any readings failed
      if (isnan(temperature) || isnan(humidity)) {
        Serial.println("Failed to read from DHT sensor!");
        return;
      }
    
      // Print the readings to the serial monitor
      Serial.print("Temperature: ");
      Serial.print(temperature);
      Serial.print(" °C\t");
      Serial.print("Humidity: ");
      Serial.print(humidity);
      Serial.println(" %");
    
      // Send data to ThingSpeak
      ThingSpeak.setField(1, temperature);
      ThingSpeak.setField(2, humidity);
      ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
    
      // Wait for a minute before next reading
      delay(60000);
    }
    

    In this code:

    • The DHT sensor reads the temperature and humidity.
    • The microcontroller connects to Wi-Fi and then sends the data to ThingSpeak (or any other cloud platform you choose) every minute.
    • Replace yourSSID, yourPassword, yourChannelNumber, and yourWriteAPIKey with your actual Wi-Fi credentials and ThingSpeak channel information.

Step 3: Upload the Code

  1. Connect the microcontroller to your computer via USB.
  2. Select the correct board and port from the Tools menu in the Arduino IDE.
  3. Click on the Upload button to upload the code to the microcontroller.

Step 4: Set Up Cloud Platform (ThingSpeak)

  1. Create a ThingSpeak account at ThingSpeak.
  2. After logging in, create a new Channel to store your temperature and humidity data.
  3. Copy the Channel ID and Write API Key provided by ThingSpeak and paste them into your code.

Step 5: Monitor Data on the Cloud

Once the system is up and running, it will start sending data to ThingSpeak every minute. You can log in to your ThingSpeak account and view a real-time graph of the temperature and humidity data.

  1. Go to ThingSpeak > Channel to see your channel.
  2. You can also create alerts or automate actions based on the temperature and humidity data, such as turning on a fan if the temperature exceeds a certain threshold.

Step 6: (Optional) Add a Display or Alarm

If you want to add a local display or alarm to your system, you can connect an LCD screen or LED to the microcontroller. Here’s how you can do that:

  • LCD Screen: Use an LCD 16×2 display to show real-time temperature and humidity data locally.
  • Buzzer/Relay: Use a buzzer or relay to trigger an alarm if the temperature or humidity goes beyond a specified threshold.

Step 7: (Optional) Power the System with Battery

If you want to make the system portable, you can use a battery as the power supply. A Li-ion battery or a solar panel could be used for long-term monitoring in outdoor environments, such as for monitoring humidity in a greenhouse.


Conclusion

Building a smart temperature and humidity monitoring system is a great way to learn about IoT and cloud integration. By using basic components like a DHT sensor, an Arduino or ESP8266/ESP32 microcontroller, and a cloud platform like ThingSpeak, you can create a fully functional system that collects, stores, and visualizes environmental data in real time. This system can be expanded to include more sensors, automation features, and advanced data analytics as you grow your IoT skills.

Harshvardhan Mishra

Hi, I'm Harshvardhan Mishra. I am a tech blogger and an IoT Enthusiast. I am eager to learn and explore tech related stuff! also, I wanted to deliver you the same as much as the simpler way with more informative content. I generally appreciate learning by doing, rather than only learning. Thank you for reading my blog! Happy learning! Follow and send tweets me on @harshvardhanrvm. If you want to help support me on my journey, consider sharing my articles, or Buy me a Coffee!

Harshvardhan Mishra has 68 posts and counting. See all posts by Harshvardhan Mishra

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.