ESP32 Indoor Environment Monitoring System

The ESP32 Indoor Environment Monitoring System project demonstrates how to monitor temperature, humidity, light level, and motion in a room using an ESP32 development board with sensors such as DHT22, BH1750, and a PIR motion sensor. The ESP32 can send alerts to Telegram or a cloud service, and optionally control lights, fans, or relays to improve comfort and safety based on the environment conditions.

About the Indoor Environment Monitoring System

This system continuously reads temperature, humidity, light level, and motion in a room. When conditions fall outside user‑defined thresholds (for example, high humidity, low light, or unwanted motion), the ESP32 can trigger an alert and automatically turn on lights, fans, or other devices via relays. The data can also be displayed on a local OLED or sent to a web‑based or cloud dashboard.

Components Needed

  • ESP32 development board (e.g. ESP32 DevKit‑v1)
  • DHT22 temperature and humidity sensor
  • BH1750 light sensor (I2C)
  • PIR motion sensor (HC‑SR501 style)
  • OLED or small LCD (optional, for local display)
  • Relay module (optional, for lights or fan control)
  • Jumper Wires
  • Breadboard

Circuit Setup

1. Connect DHT22

  • VCC → 3.3 V or 5 V (check module)
  • GND → GND
  • DATA → GPIO 4 (with 4.7 kΩ pull‑up to 3.3 V)

2. Connect BH1750 (I2C)

  • VCC → 3.3 V
  • GND → GND
  • SCL → GPIO 22
  • SDA → GPIO 21

3. Connect PIR Motion Sensor

  • VCC → 5 V (or 3.3 V, depending on module)
  • GND → GND
  • OUT → GPIO 13 (digital pin)

4. Connect Relay (Optional)

  • VCC → 5 V (if needed)
  • GND → GND
  • IN → GPIO 12 (or another GPIO)

How the System Works

The ESP32 reads temperature and humidity from the DHT22, illuminance from the BH1750, and motion state from the PIR sensor. It then compares the values with user‑defined thresholds and can send alerts via Telegram, HTTP, or MQTT when conditions are bad. Optionally, it can drive a relay to turn on lights or a fan if the room is dark or too warm, or send an alarm if motion is detected when the house is supposed to be empty.

Program: ESP32 Indoor Environment Monitoring System (Local Control)
#include <WiFi.h>
#include <HTTPClient.h>
#include <DHT.h>
#include <Wire.h>
#include <Adafruit_BH1750.h>

// Change to your Wi‑Fi credentials
const char* ssid = "YOUR_WIFI_SSID";
const char* password = "YOUR_WIFI_PASSWORD";

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

// BH1750 setup
Adafruit_BH1750 lightMeter;

// PIR and relay pins
#define PIR_PIN 13
#define RELAY_PIN 12

// Thresholds (example values)
#define HIGH_TEMP 30.0
#define HIGH_HUMIDITY 80.0
#define LOW_LIGHT 50.0    // lux

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

  // Connect to Wi‑Fi
  WiFi.begin(ssid, password);
  Serial.print("Connecting to ");
  Serial.println(ssid);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print(".");
  }
  Serial.println("\nWiFi connected");

  dht.begin();
  Wire.begin();
  if (lightMeter.begin(BH1750::CONTINUOUS_HIGH_RES_MODE)) {
    Serial.println("BH1750 initialized");
  } else {
    Serial.println("BH1750 not found!");
  }

  pinMode(PIR_PIN, INPUT);
  pinMode(RELAY_PIN, OUTPUT);
  digitalWrite(RELAY_PIN, LOW);
}

void loop() {
  // Read DHT22
  float humidity = dht.readHumidity();
  float temperature = dht.readTemperature();

  // Read BH1750
  float lux = lightMeter.readLightLevel();

  // Read PIR
  int motion = digitalRead(PIR_PIN);

  // Debug output
  Serial.print("Temp: ");
  Serial.print(temperature);
  Serial.print("°C, Humidity: ");
  Serial.print(humidity);
  Serial.print("%, Lux: ");
  Serial.print(lux);
  Serial.print(", Motion: ");
  Serial.println(motion ? "YES" : "NO");

  // Simple control logic
  bool shouldLight = (lux < LOW_LIGHT);
  bool shouldFan = (temperature > HIGH_TEMP);
  bool shouldAlarm = (motion && temperature > HIGH_TEMP); // example

  digitalWrite(RELAY_PIN, shouldLight ? HIGH : LOW);

  // Optional: send alert or HTTP request here
  // e.g., notify Telegram or update a cloud dashboard

  delay(2000);
}

Instructions

1. Circuit Setup:

Wire the DHT22, BH1750, PIR sensor, relay, and optional OLED to the ESP32 using the connections described above. Ensure the DHT22 pull‑up resistor is present and the I2C pins for the BH1750 match your code.

2. Wi‑Fi and Alert Setup:

Update the SSID and password in the code. If using Telegram or another cloud service, initialize the appropriate library and add the alert‑sending code where the comment suggests.

3. Libraries Installation:

Install the `DHT sensor library` from Adafruit, `Adafruit BH1750`, and any required cloud/Telegram or MQTT libraries from the Arduino IDE Library Manager.

4. Upload and Test:

Upload the code to the ESP32 and open the Serial Monitor. Check that the ESP32 connects to Wi‑Fi and that the terminal prints temperature, humidity, light level, and motion state every 2 seconds. Verify that the relay turns the light or fan on and off according to the thresholds.

Applications

  • Smart‑Home Comfort Control: Automatically turn on fans or lights when the room is warm or dark, or send alerts when humidity is high to prevent mold.
  • Security Monitoring: Use the PIR sensor to detect motion during unsafe hours and send alerts to a phone or logging system.
  • Office or Study‑Room Environment: Monitor temperature and light in a workspace and adjust devices to keep the environment comfortable for long‑term stays.

Troubleshooting

  • DHT22 reading NaN or 0? Check wiring, power, and the 4.7 kΩ pull‑up resistor on the data line.
  • BH1750 not responding? Verify the I2C address and wiring; ensure the sensor is not covered or in complete darkness.
  • PIR triggers continuously or not at all? Adjust the onboard potentiometers, avoid placing the sensor near heat sources, and keep its view unobstructed.

Best Practices and Notes

  • Mount the DHT22 away from direct sunlight or heaters to avoid skewed temperature and humidity readings.
  • Place the BH1750 where it can see the main light source but is not directly under a lamp to prevent over‑sensitivity.
  • Secure the PIR sensor in a corner or doorway to cover the maximum movement path while keeping the wiring tidy and safe.

Extensions and Ideas

You can extend this project by adding an OLED to show live temperature, humidity, and light level, by logging data to an SD card or cloud service, or by connecting the system to a mobile app or voice‑assistant for smart‑home integration and remote control.