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

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.
| Component | Qty |
|---|---|
| ESP8266 NodeMCU | 1 |
| Gas Sensor (MQ135 / MQ2) | 1 |
| DC-DC Buck Converter | 1 |
| Lithium-ion Batteries | 2 |
| Battery Case with Switch | 1 |
| Jumper Wires | As needed |
| From | Connect to |
|---|---|
| Gas sensor VCC | 5V |
| Gas sensor GND | GND |
| Gas sensor AOUT | A0 |
| Battery + | Buck IN+ |
| Battery − | Buck IN− |
| Buck OUT+ (set to 5V) | ESP8266 VIN |
| Buck OUT− | GND |
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);
}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.
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).
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.
The complete, tested Arduino C++ code is free on this page. Copy it into Arduino IDE and upload it to the board.
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.