AI-Based Real-Time Air Pollution Monitoring and Prediction

Below is a complete, structured, end-to-end documentation for your project: 🌍 AI-Based Real-Time Air Pollution Monitoring & Prediction System (ESP32 + IoT + n8n Automation + Telegram Voice Alerts + ThingSpeak + Google Sheets + AI Prediction)
Below is your complete project documentation converted into a single PHP file format (ready for a website/webpage display). You can save this as: air_pollution_iot_project.php and run it on XAMPP / WAMP / Laravel / any PHP server. ✅ PHP FILE (FULL WEBPAGE FORMAT) AI IoT Air Pollution Monitoring System

🌍 AI-Based Real-Time Air Pollution Monitoring System

ESP32 + IoT + n8n Automation + Telegram Alerts + ThingSpeak + AI Prediction


1. 🚀 Project Overview

This system monitors air quality using ESP32 sensors and sends real-time data to cloud platforms. It uses AI to predict pollution levels and sends Telegram alerts with voice notifications.

2. 🧰 Components List

  • ESP32 Development Board
  • MQ-135 Air Quality Sensor
  • DHT11 Temperature & Humidity Sensor
  • Jumper Wires
  • WiFi Connection
  • ThingSpeak + Google Sheets + Telegram Bot

3. 🏗 System Architecture

MQ Sensors → ESP32 → WiFi → ThingSpeak
                     ↓
                 n8n Automation
                     ↓
 Google Sheets + Telegram Alerts + AI Prediction

4. ⚡ Circuit Diagram (Connections)

  • MQ135 → GPIO 34
  • DHT11 → GPIO 4
  • VCC → 5V
  • GND → GND

5. 💻 ESP32 Code

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

#define DHTPIN 4
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);

const char* ssid = "YOUR_WIFI";
const char* password = "YOUR_PASSWORD";

const char* server = "api.thingspeak.com";
String apiKey = "YOUR_API_KEY";

int mq135Pin = 34;

void setup() {
  Serial.begin(115200);
  dht.begin();
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }
}

void loop() {
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  int airValue = analogRead(mq135Pin);

  float AQI = map(airValue, 0, 4095, 0, 500);

  WiFiClient client;
  if (client.connect(server, 80)) {
    String url = "/update?api_key=" + apiKey +
                 "&field1=" + String(AQI) +
                 "&field2=" + String(t) +
                 "&field3=" + String(h);

    client.print("GET " + url + " HTTP/1.1\r\nHost: api.thingspeak.com\r\nConnection: close\r\n\r\n");
  }

  delay(15000);
}

6. 🔁 n8n Workflow Logic

Webhook → Google Sheets → IF AQI Check → Telegram Alert

7. 🤖 Telegram Bot Setup

  • Open Telegram → BotFather
  • Create bot using /newbot
  • Copy API Token

8. 📊 Google Sheets Setup

  • Create sheet with columns: Time, AQI, Temperature, Humidity
  • Enable Google Sheets API
  • Connect in n8n

9. ☁️ ThingSpeak Setup

  • Create channel
  • Fields: AQI, Temp, Humidity
  • Use API Key in ESP32 code

10. 🧠 AI Prediction Logic

from sklearn.linear_model import LinearRegression
import pandas as pd

data = pd.read_csv("aqi.csv")

X = data[["temperature", "humidity", "aqi"]]
y = data["future_aqi"]

model = LinearRegression()
model.fit(X, y)

11. 🔊 Voice Alerts

  • Use Google TTS API or ElevenLabs API
  • Convert alert text → voice
  • Send via Telegram Bot

12. 🚨 AQI Alert Levels

  • 0–50 → Good
  • 51–100 → Moderate
  • 101–150 → Unhealthy for sensitive people
  • 151–200 → Unhealthy
  • 200+ → Hazardous

13. 🚀 Future Enhancements

  • Mobile App Dashboard
  • LSTM AI Forecasting
  • GPS Pollution Mapping
  • Edge AI on ESP32-S3

✔ Project Completed: AI Agentic IoT Air Pollution System

🚀 What you get from this PHP file ✔ Full website documentation ✔ Clean UI for project presentation ✔ Ready for college submission ✔ Works on XAMPP / hosting server ✔ Includes code + architecture + AI + IoT flow If you want next upgrade 👇 I can also convert this into: 🌐 Professional portfolio website (React + PHP backend) 📱 Mobile-friendly dashboard (Bootstrap UI) 🤖 Live real-time dashboard with charts (Chart.js) 🔥 Admin panel + login system 📡 Live ESP32 data visualization web app Just tell 👍

Comments