IoT-Based Smart Temperature & Humidity Monitoring System Using ESP32
IoT-Based Smart Temperature & Humidity Monitoring System Using ESP32 file YNkU4Doditn7QEPJW81hzc

IoT-Based Smart Temperature & Humidity Monitoring System Using ESP32

1. Introduction

This project builds an IoT-based environmental monitoring system that collects temperature and humidity data using the DHT22 sensor. The ESP32 sends this data to the cloud via MQTT or HTTP, allowing real-time monitoring through a web dashboard or mobile app.

Real-World Applications

  • Smart agriculture (greenhouse monitoring)
  • Industrial storage condition tracking
  • Home automation for HVAC optimization
  • Research data logging for environmental studies

2. Learning Objectives

By completing this project, you will:

  • Learn how to interface ESP32 with a DHT22 sensor.
  • Understand IoT data transmission via MQTT or HTTP.
  • Build a web dashboard for real-time monitoring.
  • Gain insights into circuit design and power management for IoT applications.

3. Tools and Components

Hardware Required

  • ESP32 Dev Board: Microcontroller with Wi-Fi & Bluetooth.
  • DHT22 Sensor: Temperature & Humidity sensor.
  • 10kΩ Resistor: Used as a pull-up resistor for the DHT22.
  • 5V Power Supply: USB adapter or battery.
  • Jumper Wires: For making connections.
  • OLED Display (optional): 0.96″ I2C OLED for local display.

Software Required

  • Arduino IDE: To program the ESP32.
  • ESP32 Board Package: Installed in Arduino IDE.
  • Blynk or Thingspeak: IoT cloud dashboard.
  • MQTT Broker: For data communication.

4. Circuit Diagram and Wiring

Connections

  • Connect the VCC pin of the DHT22 to 3.3V on the ESP32.
  • Connect the GND pin of the DHT22 to GND of the ESP32.
  • Connect the DATA pin of the DHT22 to GPIO4 on the ESP32.
  • Use a 10kΩ pull-up resistor between VCC and DATA.
  • If using an OLED display, connect SDA to GPIO21 and SCL to GPIO22.

5. Step-by-Step Guide

Step 1: Setting Up ESP32 in Arduino IDE

  1. Open Arduino IDE and go to File → Preferences.
  2. Add this URL in the “Additional Board Manager URLs”: https://dl.espressif.com/dl/package_esp32_index.json
  3. Go to Tools → Board → ESP32 Dev Module and select the correct COM port.

Step 2: Install Required Libraries

  • DHT Sensor Library: DHT sensor library by Adafruit
  • Adafruit Unified Sensor Library
  • PubSubClient (for MQTT)
  • Blynk Library (if using Blynk)

6. Code Implementation

Upload the following code to your ESP32:

#include <WiFi.h>
#include <DHT.h>
#include <HTTPClient.h>  // For HTTP POST to Thingspeak
#include <PubSubClient.h>  // For MQTT
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define DHTPIN 4          // GPIO pin for DHT22 data
#define DHTTYPE DHT22     // Type of sensor
#define OLED_SDA 21       // OLED SDA
#define OLED_SCL 22       // OLED SCL

const char* ssid = "Your_SSID";
const char* password = "Your_PASSWORD";

DHT dht(DHTPIN, DHTTYPE);
WiFiClient espClient;
PubSubClient client(espClient);

#define MQTT_BROKER "broker.hivemq.com"
#define MQTT_TOPIC "esp32/dht22"

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  Serial.print("Connecting to Wi-Fi");

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

  dht.begin();
  client.setServer(MQTT_BROKER, 1883);
}

void loop() {
  float temp = dht.readTemperature();
  float hum = dht.readHumidity();

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

  Serial.print("Temperature: ");
  Serial.print(temp);
  Serial.print("°C, Humidity: ");
  Serial.print(hum);
  Serial.println("%");

  if (!client.connected()) {
    reconnectMQTT();
  }
  String payload = "{\"temperature\":" + String(temp) + ", \"humidity\":" + String(hum) + "}";
  client.publish(MQTT_TOPIC, payload.c_str());

  delay(5000);
}

void reconnectMQTT() {
  while (!client.connected()) {
    Serial.print("Connecting to MQTT...");
    if (client.connect("ESP32Client")) {
      Serial.println("Connected!");
    } else {
      Serial.print("Failed, rc=");
      Serial.print(client.state());
      Serial.println(" Trying again in 5 seconds...");
      delay(5000);
    }
  }
}

7. Testing and Debugging

Common Issues and Fixes

  1. ESP32 not connecting to Wi-Fi
    • Check the SSID and password in the code.
    • Ensure the Wi-Fi is 2.4GHz (ESP32 does not support 5GHz).
  2. DHT22 sensor showing NaN values
    • Ensure the 10kΩ pull-up resistor is correctly connected.
    • Check the correct GPIO pin in the code.
  3. MQTT not publishing data
    • Verify that the MQTT broker is running.
    • Check MQTT broker’s logs for errors.

8. Extensions and Enhancements

  • OLED Display Integration: Display real-time temperature and humidity on an OLED screen.
  • Battery-Powered Operation: Use a LiPo battery with TP4056 for a portable version.
  • Multiple Sensor Nodes: Deploy multiple ESP32 units for a distributed sensor network.
  • Cloud Database Logging: Store data in Firebase or SQL database.
  • Mobile Notifications: Send alerts when temperature/humidity exceeds thresholds.

9. Conclusion

This project successfully demonstrates IoT-based environmental monitoring using the ESP32, DHT22, and MQTT. The system allows remote access to real-time temperature and humidity data, which can be expanded for home automation, industrial monitoring, or smart agriculture.

Would you like assistance in setting up a real-time web dashboard for visualization? 🚀