
IoT-Based Environmental Monitoring System Using ESP32
This project uses the ESP32 microcontroller to monitor environmental parameters such as temperature, humidity, and air quality. It then uploads the data to a cloud platform for real-time visualization. The goal is to create a reliable IoT monitoring system with a user-friendly interface for remote data access.
1. Introduction
Monitoring environmental conditions is critical in various sectors like agriculture, smart homes, and industrial environments. This project demonstrates how to design a compact IoT solution using the ESP32, DHT22 (for temperature and humidity), and MQ-135 (for air quality) sensors. The collected data will be sent to a cloud service (such as ThingSpeak or Blynk) for real-time monitoring.
Key Features:
- Monitors temperature, humidity, and air quality in real-time.
- Data is logged to the cloud for remote access and visualization.
- Low power consumption, with sleep mode implementation.
2. Learning Objectives
By completing this project, you will learn:
- How to interface multiple sensors with the ESP32.
- How to connect the ESP32 to a cloud IoT platform.
- How to program the ESP32 for data acquisition and transmission.
- Power management for IoT devices.
3. Tools and Components
Hardware:
- ESP32 Dev Board
- DHT22 Sensor – Temperature and humidity sensor
- MQ-135 Sensor – Air quality sensor
- 10kΩ Resistors (for pull-up configuration)
- Breadboard and jumper wires
- Power supply (5V)
Software:
- Arduino IDE (with ESP32 support installed)
- ThingSpeak or Blynk IoT Platform
- Libraries:
WiFi.h,DHT.h, andHTTPClient.h
4. Background and Definitions
ESP32
The ESP32 is a low-cost, low-power system-on-chip microcontroller with integrated Wi-Fi and Bluetooth. It’s highly suitable for IoT applications due to its versatility.
DHT22 Sensor
A digital sensor for measuring temperature and humidity. It offers high accuracy and reliability compared to other DHT variants.
MQ-135 Sensor
An air quality sensor used to detect a wide range of gases, including ammonia, sulfide, and benzene vapor, making it ideal for air quality monitoring.
5. Step-by-Step Guide
Step 1: Circuit Design
The circuit connects the DHT22 and MQ-135 sensors to the ESP32:
- DHT22 Connections:
- VCC → 3.3V
- GND → GND
- Data → GPIO 4 (with a 10kΩ pull-up resistor)
- MQ-135 Connections:
- VCC → 5V
- GND → GND
- Analog Output → GPIO 34
- ESP32 Power Supply: Connect the ESP32 to a 5V power source through USB or an external power supply.
Diagram Description:
- Draw the ESP32 in the center with connections radiating to the DHT22 and MQ-135.
- Use different colored lines to represent power, ground, and data connections.
I can generate this circuit diagram if you want it visualized.
Step 2: Programming the ESP32
- Set Up the Arduino IDE for ESP32
Ensure that the ESP32 board is added to the Arduino IDE. Install the necessary libraries (DHT.h,WiFi.h,HTTPClient.h). - Full Source Code:
#include <WiFi.h>
#include <HTTPClient.h>
#include <DHT.h>
#define DHTPIN 4 // GPIO pin where the DHT22 is connected
#define DHTTYPE DHT22
#define MQ135_PIN 34 // Analog pin for MQ-135 sensor
DHT dht(DHTPIN, DHTTYPE);
// Replace with your Wi-Fi credentials
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";
// ThingSpeak or your IoT endpoint
const char* serverName = "http://api.thingspeak.com/update?api_key=YOUR_API_KEY";
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("\nWi-Fi connected.");
dht.begin();
}
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;
}
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
String serverPath = serverName + "&field1=" + String(temperature) + "&field2=" + String(humidity) + "&field3=" + String(airQuality);
http.begin(serverPath.c_str());
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
} else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
http.end();
} else {
Serial.println("Wi-Fi Disconnected");
}
delay(30000); // Send data every 30 seconds
}
6. Testing and Debugging Tips
- Sensor Reading Errors: If the DHT22 fails to provide readings, check the pull-up resistor and connections.
- Wi-Fi Connection Issues: Ensure your credentials are correct and that the ESP32 is within Wi-Fi range.
- Cloud Data Not Updating: Verify the API key and URL endpoint for your IoT platform.
7. Extensions
Here are some ideas for extending the project:
- Add a PM2.5 Sensor for monitoring fine particulate matter.
- Use OLED Display to show real-time data locally.
- Mobile Notifications with Blynk for threshold alerts.
- Low Power Mode: Implement deep sleep to conserve battery life.