🌍 ESP32-Based IoT Environmental Monitoring System
🌍 ESP32-Based IoT Environmental Monitoring System file TuV6ztLSSPk3pQMK7aQYsA

🌍 ESP32-Based IoT Environmental Monitoring System


1. 🧭 Introduction

In this project, you’ll build a Wi-Fi-enabled environmental monitoring system using the ESP32 microcontroller. It will read:

  • Temperature and Humidity using the DHT22 sensor
  • Air Quality using the MQ135 gas sensor
  • Send the collected data to the cloud in real-time using ThingSpeak

This enables remote environmental monitoring via the web or a mobile dashboard. It’s perfect for smart agriculture, indoor air quality monitoring, and student projects involving climate science and IoT.


2. 🎯 Learning Objectives

By the end of this project, you will be able to:

  • Interface analog and digital sensors with the ESP32
  • Process environmental data from DHT22 and MQ135
  • Connect the ESP32 to Wi-Fi and transmit sensor data to the cloud
  • Create a reliable IoT monitoring system with web-based visualization
  • Understand the basics of cloud API communication using HTTP GET

3. 🧰 Tools and Components

To build this project, you’ll need:

  • One ESP32 development board (with onboard Wi-Fi)
  • One DHT22 sensor (for temperature and humidity)
  • One MQ135 gas sensor (for air quality)
  • One 10k ohm resistor (for the DHT22 data pin pull-up)
  • Breadboard and jumper wires for connections
  • A USB cable for uploading code and powering the ESP32
  • A computer with the Arduino IDE installed
  • Access to a Wi-Fi network
  • A ThingSpeak account for cloud data storage and visualization

4. πŸ“˜ Background Knowledge

ESP32 is a microcontroller with built-in Wi-Fi and Bluetooth, making it ideal for Internet of Things projects.

DHT22 is a digital sensor that outputs calibrated temperature and humidity readings. It’s more accurate and stable than its sibling, the DHT11.

MQ135 is an analog air quality sensor that detects a range of gases, including CO2, NH3, alcohol, and smoke. It outputs an analog voltage corresponding to gas concentration.

ThingSpeak is a free IoT analytics platform that allows you to visualize sensor data online. It supports REST API and provides public/private data channels.


5. πŸ›  Step-by-Step Guide

Step 1: Wiring the Components

  • Connect the VCC of DHT22 to the 3.3V pin on the ESP32
  • Connect the GND of DHT22 to GND on the ESP32
  • Connect the DATA pin of DHT22 to GPIO 4 on the ESP32
  • Place a 10k ohm resistor between the DHT22’s VCC and DATA pins
  • Connect the VCC of MQ135 to the 5V pin on the ESP32
  • Connect the GND of MQ135 to GND
  • Connect the analog output (AO) of MQ135 to GPIO 36 (ADC0)

Step 2: Preparing the Arduino IDE

  • Install the ESP32 board support through the Boards Manager
  • Install the following libraries:
    • DHT sensor library by Adafruit
    • Adafruit Unified Sensor
    • HTTPClient

Step 3: Arduino Sketch

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

#define DHTPIN 4
#define DHTTYPE DHT22
#define MQ135_PIN 36

const char* ssid = "YOUR_WIFI_SSID";
const char* password = "YOUR_WIFI_PASSWORD";

String server = "http://api.thingspeak.com/update";
String apiKey = "YOUR_THINGSPEAK_WRITE_API_KEY";

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("\nConnected to WiFi!");
}

void loop() {
  float temperature = dht.readTemperature();
  float humidity = dht.readHumidity();
  int airQuality = analogRead(MQ135_PIN);

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

  Serial.print("Temperature: ");
  Serial.print(temperature);
  Serial.print(" Β°C, Humidity: ");
  Serial.print(humidity);
  Serial.print(" %, Air Quality: ");
  Serial.println(airQuality);

  if (WiFi.status() == WL_CONNECTED) {
    HTTPClient http;
    String url = server + "?api_key=" + apiKey +
                 "&field1=" + String(temperature) +
                 "&field2=" + String(humidity) +
                 "&field3=" + String(airQuality);
    http.begin(url);
    int httpResponseCode = http.GET();
    if (httpResponseCode > 0) {
      Serial.println("Data sent to ThingSpeak successfully.");
    } else {
      Serial.print("Error sending data. HTTP code: ");
      Serial.println(httpResponseCode);
    }
    http.end();
  }

  delay(20000); // Wait 20 seconds before sending again
}

Replace "YOUR_WIFI_SSID", "YOUR_WIFI_PASSWORD", and "YOUR_THINGSPEAK_WRITE_API_KEY" with your actual values.


6. πŸ§ͺ Testing and Debugging

  • Use the Serial Monitor to observe temperature, humidity, and air quality values
  • If DHT22 returns NaN, double-check your wiring and resistor placement
  • If you’re not seeing data on ThingSpeak, ensure the API key is correct and delay is at least 15 seconds
  • Use a multimeter to confirm that 3.3V and 5V lines are providing power
  • Ensure the Wi-Fi credentials are typed correctly and the ESP32 is within range

7. πŸš€ Project Extensions

Once you have the basic version working, consider expanding it:

  • Add a BMP280 sensor for atmospheric pressure and altitude
  • Send notifications to a phone using Telegram Bot API
  • Host your own web interface on the ESP32 itself with charts
  • Add an OLED display to show local sensor readings
  • Enable deep sleep mode for power-saving on battery
  • Use MQTT protocol instead of HTTP for scalability and speed

Let me know if you want a visual schematic diagram or a custom PCB layoutβ€”I can generate both for you!