IoTBeginnerPRJ-043

IoT Pollution Monitoring System Using ESP8266

Shows live air quality and a rolling graph on a web page hosted by the ESP8266.

Built and tested by Aboobacker sidheeq K P · Industrial Robotics Institute

Circuit diagram of the IoT Pollution Monitoring System using ESP8266 NodeMCU
Circuit diagram · ESP8266 NodeMCU
LevelBeginner
Build time2–3 hours
BoardESP8266 NodeMCU
Parts6 components

What is the IoT Pollution Monitoring System?

An air pollution monitor that measures gas levels in real time and shows them on a web page with a live graph. An MQ135 or MQ2 gas sensor feeds the ESP8266, which classifies the air as Good, Moderate or Poor and keeps the last 20 readings to draw a trend chart. Open the device’s IP address on any phone or laptop on the same Wi-Fi to see it.

How it works

  1. The gas sensor detects smoke and harmful gases in the air.
  2. The ESP8266 reads the sensor’s analog value (0–1023) every second.
  3. Each reading is stored in a rolling list of the last 20 values.
  4. The value is classified: up to 400 is Good, 401–700 is Moderate, above 700 is Poor.
  5. When you open the device’s IP address, the ESP8266 serves a page with the current value, the air quality level and a live line graph.

Parts list

ComponentQty
ESP8266 NodeMCU1
Gas Sensor (MQ135 / MQ2)1
DC-DC Buck Converter1
Lithium-ion Batteries2
Battery Case with Switch1
Jumper WiresAs needed

Software

  • Arduino IDE
  • ESP8266WiFi library
  • Chart.js (loaded by the web page)

Wiring and connections

FromConnect to
Gas sensor VCC5V
Gas sensor GNDGND
Gas sensor AOUTA0
Battery +Buck IN+
Battery −Buck IN−
Buck OUT+ (set to 5V)ESP8266 VIN
Buck OUT−GND

How to build the IoT Pollution Monitoring System

  1. Set the buck converter’s output to 5V with a multimeter before connecting the ESP8266.
  2. Wire the battery pack to the buck converter input and its 5V output to the ESP8266 VIN and GND.
  3. Connect the gas sensor to 5V, GND and A0.
  4. Enter your Wi-Fi name and password in the code.
  5. Upload the code from the Arduino IDE.
  6. Open the Serial Monitor at 115200 baud and note the IP address.
  7. Let the gas sensor warm up for a few minutes for stable readings.
  8. Open the IP address in a browser on the same Wi-Fi to see the live value and graph.

IoT Pollution Monitoring System code

Complete, tested Arduino C++ code for this project. Copy it into Arduino IDE and upload it to the ESP8266 NodeMCU.

#include <ESP8266WiFi.h>

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

WiFiServer server(80);

int gasPin = A0;
int dataArray[20];   // store last 20 readings
int indexPos = 0;

void setup()
{
  Serial.begin(115200);
  delay(1000);
  Serial.println("Starting ESP8266...");

  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println();
  Serial.println("WiFi Connected");
  Serial.print("Device IP Address: ");
  Serial.println(WiFi.localIP());

  server.begin();
  Serial.println("Web Server Started");
}

void loop()
{
  int gasValue = analogRead(gasPin);

  // Store data for graph
  dataArray[indexPos] = gasValue;
  indexPos = (indexPos + 1) % 20;

  String level = "GOOD";
  if (gasValue > 700) level = "POOR";
  else if (gasValue > 400) level = "MODERATE";

  WiFiClient client = server.available();
  if (client)
  {
    String html = "<html><body>";
    html += "<h1>Pollution Monitoring</h1>";
    html += "<p>Value: " + String(gasValue) + "</p>";
    html += "<p>Air Quality: " + level + "</p>";

    // Graph
    html += "<canvas id='chart'></canvas>";
    html += "<script src='https://cdn.jsdelivr.net/npm/chart.js'></script>";
    html += "<script>";
    html += "var ctx=document.getElementById('chart').getContext('2d');";
    html += "new Chart(ctx,{type:'line',data:{labels:[";
    for (int i = 0; i < 20; i++) html += "'" + String(i) + "',";
    html += "],datasets:[{label:'Pollution',data:[";
    for (int i = 0; i < 20; i++) html += String(dataArray[i]) + ",";
    html += "]}]}});";
    html += "</script></body></html>";

    client.print("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n");
    client.print(html);
    delay(1);
    client.stop();
  }

  delay(1000);
}

Real-world uses

  • Classrooms and labs
  • Kitchens and workshops
  • Roadside and city air checks
  • Factories

Frequently asked questions

What is a IoT Pollution Monitoring System?

An air pollution monitor that measures gas levels in real time and shows them on a web page with a live graph. An MQ135 or MQ2 gas sensor feeds the ESP8266, which classifies the air as Good, Moderate or Poor and keeps the last 20 readings to draw a trend chart.

What components are needed for the IoT Pollution Monitoring System?

You need: ESP8266 NodeMCU (1), Gas Sensor (MQ135 / MQ2) (1), DC-DC Buck Converter (1), Lithium-ion Batteries (2), Battery Case with Switch (1), Jumper Wires. Software: Arduino IDE, ESP8266WiFi library, Chart.js (loaded by the web page).

Is the IoT Pollution Monitoring System project good for beginners?

Yes. It is rated beginner and takes about 2–3 hours to build, using the ESP8266 NodeMCU. The guide on this page explains every connection and step.

Where can I get the code for the IoT Pollution Monitoring System?

The complete, tested Arduino C++ code is free on this page. Copy it into Arduino IDE and upload it to the board.

Can I buy a kit for the IoT Pollution Monitoring System?

Yes. IRI sells a kit with the parts for this project (PRJ-043). Message IRI on WhatsApp at +91 86060 82634 for the kit price and delivery details.

Industrial Robotics Institute
Logo