๐ŸŒŸ IoT Smart Temperature and Humidity Monitor (ESP32-Based)
๐ŸŒŸ IoT Smart Temperature and Humidity Monitor (ESP32-Based)

๐ŸŒŸ IoT Smart Temperature and Humidity Monitor (ESP32-Based)


1. Introduction

In this project, you’ll build an Internet-connected temperature and humidity monitor using an ESP32 microcontroller and a DHT22 sensor. The collected data will be sent to a cloud dashboard like ThingSpeak for real-time remote monitoring.

Real-world Applications:

  • Smart agriculture systems
  • Home and office air quality monitoring
  • Industrial environment control
  • Data centers/server room climate tracking

2. Learning Objectives

  • How to connect and read environmental sensors with ESP32
  • How to connect an ESP32 to Wi-Fi and send data to the Internet
  • Understanding HTTP GET requests for IoT
  • Building a basic IoT architecture
  • Learning best wiring and circuit practices for embedded systems

3. Tools and Components Checklist

Hardware Needed:

  • ESP32 Dev Module (e.g., ESP32 WROOM-32)
  • DHT22 (or AM2302) Temperature and Humidity Sensor
  • 10kฮฉ Resistor (for pull-up on DATA line)
  • Breadboard
  • Jumper wires
  • Micro-USB cable for programming
  • Power supply (or battery for field deployment)

Software Needed:

  • Arduino IDE (with ESP32 board manager installed)
  • DHT Sensor Library (Adafruit)
  • Adafruit Unified Sensor Library
  • ThingSpeak account (or Blynk alternative)

4. Background and Core Concepts

ESP32
A low-cost, powerful Wi-Fi and Bluetooth SoC widely used for IoT applications. Itโ€™s capable of handling sensor data and communication to the internet simultaneously.

DHT22 Sensor
A digital sensor that provides temperature and humidity readings. Itโ€™s accurate, easy to use, and sends pre-calibrated data via a single digital line.

Pull-up Resistor
A resistor that ensures the signal line remains in a valid state even when disconnected or idle. Essential for digital communication stability.

ThingSpeak
An open IoT cloud platform used for real-time data collection, visualization, and analysis.


5. Step-by-Step Guide

A. Circuit Wiring Instructions

  • Connect DHT22 VCC pin to 3.3V on the ESP32.
  • Connect DHT22 GND pin to GND on the ESP32.
  • Connect DHT22 DATA pin to GPIO4 on the ESP32.
  • Place a 10kฮฉ resistor between the DATA pin and VCC to act as a pull-up resistor.

Note: Some DHT22 modules come with an internal pull-up resistor already soldered. If your module has it, you can skip the external 10kฮฉ resistor.


B. Setting up Arduino IDE

  1. Open Arduino IDE.
  2. Go to File โ†’ Preferences.
  3. Under “Additional Board URLs,” add:
    https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json
  4. Open Boards Manager and install “esp32” by Espressif Systems.
  5. Install the following libraries via Library Manager:
    • “DHT sensor library” by Adafruit
    • “Adafruit Unified Sensor” by Adafruit
  6. Select the ESP32 Dev Module board.
  7. Choose the correct COM port.

C. Full Source Code (Fully Explained)

#include <WiFi.h>
#include "DHT.h"
#include <HTTPClient.h>

// Wi-Fi Credentials
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";

// ThingSpeak Settings
const char* server = "http://api.thingspeak.com/update";
String apiKey = "YOUR_THINGSPEAK_API_KEY";

// DHT22 Sensor Settings
#define DHTPIN 4
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(115200);
  dht.begin();

  WiFi.begin(ssid, password);
  Serial.print("Connecting to WiFi");

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println(" Connected!");
}

void loop() {
  float humidity = dht.readHumidity();
  float temperature = dht.readTemperature(); // Temperature in Celsius

  if (isnan(humidity) || isnan(temperature)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  Serial.print("Humidity: ");
  Serial.print(humidity);
  Serial.print("% Temperature: ");
  Serial.print(temperature);
  Serial.println("ยฐC");

  if (WiFi.status() == WL_CONNECTED) {
    HTTPClient http;
    String url = server;
    url += "?api_key=" + apiKey;
    url += "&field1=" + String(temperature);
    url += "&field2=" + String(humidity);

    http.begin(url);
    int httpResponseCode = http.GET();

    if (httpResponseCode > 0) {
      Serial.println("Data sent successfully to ThingSpeak!");
    } else {
      Serial.println("Error sending data.");
    }

    http.end();
  }

  delay(15000); // Delay between updates (ThingSpeak minimum is 15 seconds)
}

6. Testing and Debugging Tips

Testing Process:

  • Connect the ESP32 via USB to your computer.
  • Open Serial Monitor at 115200 baud.
  • Observe temperature and humidity readings being printed.
  • Verify that the data updates appear on your ThingSpeak channel.

Common Issues and Solutions:

  • Wi-Fi not connecting: Double-check SSID and password for typos.
  • DHT sensor not reading: Check wiring. Make sure the pull-up resistor is correctly connected.
  • ThingSpeak not updating: Ensure the API key is correct. Remember, ThingSpeak only allows updates every 15 seconds.
  • Data read shows NaN: Confirm sensor connections. Check voltage levels โ€” DHT22 must be powered at 3.3V for ESP32 compatibility.

7. Extensions and Advanced Modifications

Ideas to Expand This Project:

  • Add a small OLED display to show live readings on the device itself.
  • Send email alerts or push notifications when temperature or humidity exceeds thresholds.
  • Expand the system to support multiple DHT22 sensors using different GPIO pins.
  • Replace ThingSpeak with MQTT protocol and create a Node-RED dashboard.
  • Implement deep sleep mode to save power for a battery-powered version.
  • Add weather forecasting features by combining with public API services like OpenWeatherMap.

๐ŸŽฏ Final Words

This hands-on project builds critical skills in embedded system design, IoT communication, and cloud integration. Itโ€™s a full mini-IoT system that connects the physical world to the Internet โ€” just like real-world smart devices!

By completing this project, youโ€™ll confidently understand:

  • Sensor interfacing
  • Networking basics
  • Cloud data handling
  • Debugging and extending embedded projects