🏠 IoT Temperature and Humidity Monitor Using ESP32
🏠 IoT Temperature and Humidity Monitor Using ESP32

🏠 IoT Temperature and Humidity Monitor Using ESP32


🌟 Project Overview

Project: IoT Temperature and Humidity Monitor
Controller: ESP32 (Wi-Fi + Bluetooth)
Sensors: DHT22 (Temperature & Humidity)
Cloud: MQTT (Broker + Dashboard)
Display: OLED Display (optional)
Purpose: Collect, display, and remotely monitor environmental conditions in real time.


🎯 Learning Objectives

  • Interface sensors with ESP32.
  • Send data over Wi-Fi using MQTT.
  • Build a simple cloud dashboard.
  • Design and wire a complete IoT circuit.
  • Understand basic PCB design for IoT devices.

🛠️ Tools and Components

Hardware:

  • ESP32 Dev Board (e.g. DOIT ESP32 DEVKIT V1)
  • DHT22 sensor (temperature & humidity)
  • 4.7kΩ resistor (for DHT22 pull-up)
  • OLED Display (SSD1306, optional)
  • Breadboard and jumper wires
  • Micro USB cable

Software:

  • Arduino IDE with ESP32 core installed
  • MQTT broker (e.g. Mosquitto, Adafruit IO, or ThingsBoard)
  • MQTT client dashboard (e.g. MQTT Explorer, Node-RED)

📚 Background/Definitions

  • ESP32
    A powerful Wi-Fi + Bluetooth microcontroller ideal for IoT applications. It features multiple GPIOs, ADCs, and PWM outputs.
  • DHT22
    A digital sensor that outputs calibrated temperature and humidity data with high accuracy. Requires a pull-up resistor on its data pin.
  • MQTT
    A lightweight publish/subscribe protocol for IoT, enabling devices to send and receive messages through a broker.
  • OLED Display
    A small screen (e.g. 128×64) using I2C to display real-time data locally.

🛠️ Step-by-Step Guide

1️⃣ Circuit Design and Wiring

ESP32 to DHT22:

  • Connect 3V3 from the ESP32 to VCC on the DHT22.
  • Connect GND from the ESP32 to GND on the DHT22.
  • Connect GPIO4 on the ESP32 to the Data Pin on the DHT22.
  • Add a 4.7kΩ resistor between the Data Pin and VCC (3V3) to stabilize the signal.

ESP32 to OLED Display (optional):

  • Connect 3V3 from the ESP32 to VCC on the OLED.
  • Connect GND from the ESP32 to GND on the OLED.
  • Connect GPIO22 on the ESP32 to SCL on the OLED.
  • Connect GPIO21 on the ESP32 to SDA on the OLED.

2️⃣ Annotated Circuit Diagram (Description)

  • ESP32 3V3 supplies power to both the DHT22 and the OLED.
  • ESP32 GND connects to both GND pins.
  • DHT22 Data Pin connects to GPIO4, with a 4.7kΩ pull-up resistor between the Data Pin and VCC.
  • OLED SCL/SDA connect to GPIO22 and GPIO21 respectively.

3️⃣ Source Code

Here’s a simplified Arduino sketch with comments explaining each part:

#include <WiFi.h>
#include <PubSubClient.h>
#include <DHT.h>
#include <Wire.h>
#include <Adafruit_SSD1306.h>

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

// MQTT Broker
const char* mqtt_server = "BROKER_IP";

// Pins
#define DHTPIN 4
#define DHTTYPE DHT22

// OLED
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

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

void setup() {
  Serial.begin(115200);
  dht.begin();
  
  // Wi-Fi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting...");
  }
  Serial.println("Wi-Fi connected");

  // MQTT
  client.setServer(mqtt_server, 1883);

  // OLED
  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println("OLED failed");
    for (;;);
  }
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);
}

void loop() {
  if (!client.connected()) {
    reconnect();
  }
  client.loop();

  float temp = dht.readTemperature();
  float hum = dht.readHumidity();

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

  // Display on OLED
  display.clearDisplay();
  display.setCursor(0,0);
  display.printf("Temp: %.1f C\nHum: %.1f %%", temp, hum);
  display.display();

  // Publish via MQTT
  String payload = String("{\"temperature\":") + temp + ",\"humidity\":" + hum + "}";
  client.publish("home/room1/env", payload.c_str());

  delay(5000); // Read every 5 seconds
}

void reconnect() {
  while (!client.connected()) {
    Serial.println("Connecting to MQTT...");
    if (client.connect("ESP32Client")) {
      Serial.println("connected");
    } else {
      delay(5000);
    }
  }
}

🧩 Testing and Debugging Tips

  • Check sensor output via Serial Monitor before adding Wi-Fi/MQTT.
  • Verify Wi-Fi credentials and ensure the router is within range.
  • Test the MQTT broker using a local client (e.g. MQTT Explorer) before integrating with ESP32.
  • Confirm that the pull-up resistor is in place to stabilize the DHT22 data signal.
  • Double-check the OLED I2C address if it fails to initialize.

🚀 Extensions

  • Integrate with Home Assistant to automate HVAC or dehumidifiers.
  • Add more DHT22 sensors in different rooms for distributed monitoring.
  • Log data to an SD card or a cloud database like Firebase for historical analysis.
  • Use IFTTT or push notifications to alert on thresholds.
  • Design a custom PCB to eliminate the breadboard and make the system more compact and reliable.