๐ฟ AirWatch โ Air Quality Monitoring System
ESP8266 + MQ135 + SSD1306 OLED + Active Buzzer + Web Dashboard
A self-contained air quality monitor that reads COโ-equivalent gas concentration, displays live AQI on an OLED, fires a buzzer alert at AQI โฅ 110, and streams real-time data to a green-themed web dashboard over Wi-Fi.
1. Overview
AirWatch reads air quality using an MQ135 sensor every 2 seconds, converts the raw ADC value into a COโ PPM estimate, then maps that to a standard AQI index. The result is shown on a 128ร64 OLED display and served as JSON over Wi-Fi to a browser dashboard.
When AQI crosses 110 (lower boundary of the Sensitive Groups band), an active buzzer fires a single 300 ms beep. The buzzer re-arms automatically once the AQI drops back below 110, so it will beep again if the threshold is crossed a second time.
Key Features
- Real-time MQ135 sensor reading every 2 seconds
- COโ PPM calculation using the MQ135 power-law curve
- PPM โ AQI conversion across six standard EPA bands
- Live OLED display: AQI, PPM, category, and alert state
- Single-beep buzzer alert at AQI โฅ 110 (auto re-arms on recovery)
- Wi-Fi HTTP server serving JSON at
GET /data - Green-themed animated web dashboard with ring gauge, history chart, and stats
- Built-in calibration mode for accurate RZERO determination
2. Hardware Requirements
| Component | Details |
|---|---|
| Microcontroller | ESP8266 NodeMCU v1/v2 (or any ESP8266 module) |
| Gas Sensor | MQ135 Air Quality Sensor module |
| Display | SSD1306 0.96โณ OLED โ 128ร64 px โ IยฒC address 0x3C |
| Buzzer | Active buzzer (3.3 V or 5 V with transistor) |
| Resistors | Two 10 kฮฉ resistors (voltage divider for MQ135 AOUT โ A0) |
| Power | 5 V micro-USB to NodeMCU + 5 V to MQ135 VCC |
| Misc | Jumper wires, breadboard or PCB |
3. Wiring
Pin Map
| Component | Pin | ESP8266 Pin | Notes |
|---|---|---|---|
| MQ135 | AOUT | A0 | Via 10 kฮฉ + 10 kฮฉ voltage divider |
| MQ135 | VCC | Vin (5 V) | MQ135 heater requires 5 V |
| MQ135 | GND | GND | |
| OLED | SDA | D2 (GPIO4) | IยฒC data |
| OLED | SCL | D1 (GPIO5) | IยฒC clock |
| OLED | VCC | 3.3 V | |
| OLED | GND | GND | |
| Buzzer | + | D5 (GPIO14) | Active buzzer: HIGH = ON |
| Buzzer | โ | GND |
โ ๏ธ Voltage Divider โ Critical for ESP8266
The ESP8266 ADC (A0) accepts a maximum of 1.0 V. The MQ135 AOUT pin can output up to 5 V.
MQ135 AOUT โโโฌโโ 10 kฮฉ โโ A0
โ
10 kฮฉ
โ
GNDConnect two 10 kฮฉ resistors in series between AOUT and GND. Tap A0 from the midpoint. This is already reflected in the firmware as #define VCC 1.0.
4. Software Setup
4.1 Arduino IDE & Board
- Open Arduino IDE โ File โ Preferences โ Additional Board URLs
- Add:
https://arduino.esp8266.com/stable/package_esp8266com_index.json - Tools โ Board Manager โ search
esp8266โ Install - Select: Tools โ Board โ NodeMCU 1.0 (ESP-12E Module)
- Set Upload Speed: 115200, Port: your COM/ttyUSB port
4.2 Required Libraries
Install via Sketch โ Include Library โ Manage Libraries:
| Library | Version |
|---|---|
| Adafruit SSD1306 | Latest |
| Adafruit GFX Library | Latest |
| ArduinoJson | 6.x (not v5 or v7) |
| ESP8266WiFi | Built-in with board package |
| ESP8266WebServer | Built-in with board package |
4.3 Configuration
Edit the top of air_quality_esp8266.ino before uploading:
// Wi-Fi credentials
const char* WIFI_SSID = "YourNetworkName";
const char* WIFI_PASSWORD = "YourPassword";
// Calibration
#define CALIBRATION_MODE false // true = calibration, false = normal run
#define MQ135_RZERO 30.0 // paste your calibrated value here
#define MQ135_RLOAD 10.0 // load resistor on MQ135 board (kฮฉ)
#define VCC 1.0 // ESP8266 ADC reference (1 V after voltage divider)5. Calibration
The MQ135_RZERO constant is unique to each sensor and must be measured in clean air. Skipping this step will give inflated PPM and AQI readings.
Step-by-step
- Set
#define CALIBRATION_MODE truein the firmware - Upload to the ESP8266
- Take the sensor outdoors or near an open window
- Open Serial Monitor at 115200 baud
- Wait at least 5 minutes for the heater to stabilise โ watch the RZERO column
- Note the value once it stabilises (typically 20โ50 with voltage divider)
- Paste it into
#define MQ135_RZERO <your value>, setCALIBRATION_MODE false, re-upload
Expected Serial Output (Calibration Mode)
RAW: 236 | Rs: 6.85 kOhm | RZERO: 28.43 <-- use this after 5 min
RAW: 237 | Rs: 6.81 kOhm | RZERO: 28.27 <-- use this after 5 min
RAW: 235 | Rs: 6.89 kOhm | RZERO: 28.61 <-- use this after 5 minTip: Take the average of readings over the last 2 minutes of the warm-up period.
6. Normal Operation
OLED Display Layout
+-----------------------------+
| AIR QUALITY MONITOR |
|_____________________________|
| 87 AQI |
| 580 ppm |
|_____________________________|
| Moderate RAW: 236 |
+-----------------------------+When AQI โฅ 110, the bottom line shows !! Sensitive !! instead of the plain category.
Serial Monitor Output
RAW: 236 | PPM: 580.0 | AQI: 87.0 | Moderate
RAW: 241 | PPM: 620.3 | AQI: 95.2 | Moderate
RAW: 255 | PPM: 714.8 | AQI: 104.9 | SensitiveAQI Bands
| AQI Range | Category | Description |
|---|---|---|
| 0 โ 50 | Good | Air quality is satisfactory |
| 51 โ 100 | Moderate | Acceptable; some pollutants may be a concern for a small number of sensitive people |
| 101 โ 150 | Sensitive | Members of sensitive groups may experience health effects |
| 151 โ 200 | Unhealthy | Everyone may begin to experience health effects |
| 201 โ 300 | Very Unhealthy | Health alert; everyone may experience more serious effects |
| 301+ | Hazardous | Health warnings of emergency conditions |
Buzzer Behaviour
- Fires one 300 ms beep the moment AQI first reaches or exceeds 110
- Re-arms once AQI drops below 110 โ will beep again if threshold is crossed again
- Does not beep continuously while AQI stays above 110
7. Web API
The ESP8266 runs an HTTP server on port 80. Its IP address is printed to both the OLED and Serial Monitor at boot.
Endpoints
| Method | Endpoint | Response |
|---|---|---|
| GET | / | Plain-text status and IP |
| GET | /data | JSON with current readings |
Sample JSON Response
{
"aqi": 87,
"ppm": 580,
"category": "Moderate",
"alert": false,
"ip": "192.168.1.42"
}The response includes Access-Control-Allow-Origin: * so any browser tab or local HTML file can fetch it without CORS errors.
8. Web Dashboard
Open airwatch_dashboard.html in any modern browser. It runs a demo animation automatically before connecting to hardware.
Connecting to Live Hardware
- Find the ESP8266 IP address on the OLED at boot or in Serial Monitor
- Enter the IP into the ESP IP field in the dashboard
- Click Connect โ data refreshes every 2.5 seconds
Dashboard Components
| Component | Description |
|---|---|
| Ring Gauge | Animated circular gauge with tick marks; colour changes per AQI band |
| AQI Number | Large number with matching colour glow |
| PPM Reading | Shown inside the gauge below the AQI value |
| Category Badge | Colour-coded pill showing the current band name |
| Alert Banner | Red flashing banner when AQI โฅ 110 |
| AQI Scale | Six colour-coded bands; active band highlights live |
| History Chart | Sparkline of last 40 readings with dashed alert line at 110 |
| Stats Row | Current / Min / Max / Avg / Reading count / Alert count |
9. Firmware Reference
Key Functions
| Function | Description |
|---|---|
rawToPPM(int raw) | Converts ADC value to COโ-equivalent PPM using MQ135 power-law curve |
ppmToAQI(float ppm) | Maps PPM to AQI across six linear segments |
aqiCategory(float aqi) | Returns string category name for an AQI value |
updateDisplay(int raw) | Redraws OLED with AQI, PPM, category, and alert state |
singleBeep() | Drives BUZZER_PIN HIGH for 300 ms then LOW |
handleData() | HTTP handler โ serialises state to JSON with CORS header |
mapf(x, in_min, ...) | Float-precision version of Arduino map() |
Timing
| Event | Interval |
|---|---|
| Sensor read + OLED update | Every 2000 ms |
| Buzzer pulse duration | 300 ms |
| Dashboard fetch interval | Every 2500 ms (client-side) |
PPM Formula
Rs = ((VCC - Vout) / Vout) ร RLOAD
ratio = Rs / RZERO
PPM = 116.6020682 ร ratio^(โ2.769034857)10. Troubleshooting
| Symptom | Fix |
|---|---|
| AQI shows 301 constantly | RZERO needs calibration. Run calibration mode outdoors for 5+ min and update MQ135_RZERO. |
| AQI too high right after power-on | Normal โ MQ135 heater needs 2โ5 min warm-up. Wait before trusting readings. |
| OLED shows nothing | Check SDAโD2 and SCLโD1. Verify IยฒC address is 0x3C. Some clones use 0x3D. |
| Wi-Fi never connects | Check WIFI_SSID / WIFI_PASSWORD. ESP8266 only supports 2.4 GHz networks. |
| Dashboard shows OFFLINE | Confirm the IP. |
/*
* ============================================================
* AirWatch โ ESP8266 + MQ135 + DHT11 + SSD1306 + Buzzer
* ============================================================
* Wiring:
* MQ135 AOUT โ A0 (via 10kฮฉ+10kฮฉ voltage divider)
* DHT11 DATA โ D6 (GPIO12)
* OLED SDA โ D2 (GPIO4)
* OLED SCL โ D1 (GPIO5)
* Buzzer + โ D5 (GPIO14)
*
* Libraries (Arduino Library Manager):
* - ESP8266WiFi (built-in)
* - ESP8266WebServer (built-in)
* - Adafruit_SSD1306
* - Adafruit_GFX
* - DHT sensor library (by Adafruit)
* - ArduinoJson v6
* ============================================================
*/
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <DHT.h>
#include <ArduinoJson.h>
// โโ Wi-Fi credentials โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
const char* WIFI_SSID = ""; // <-- change this
const char* WIFI_PASSWORD = ""; // <-- change this
// โโ Hardware pins โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
#define MQ135_PIN A0
#define BUZZER_PIN 14 // GPIO14 d5
#define DHT_PIN 12 // GPIO12 d6
#define DHT_TYPE DHT11
// โโ OLED โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// โโ Sensors & server โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
DHT dht(DHT_PIN, DHT_TYPE);
ESP8266WebServer server(80);
// โโ MQ135 calibration โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
#define CALIBRATION_MODE false
#define MQ135_RZERO 28.4
#define MQ135_RLOAD 10.0
#define VCC 1.0
#define ADC_MAX 1023.0
// โโ Global state โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
float g_aqi = 0;
float g_ppm = 0;
String g_cat = "Good";
float g_temp = 0;
float g_hum = 0;
bool g_buzzerFired = false;
unsigned long g_lastRead = 0;
const unsigned long READ_INTERVAL = 2000;
// โโ Helpers โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
float mapf(float x, float a, float b, float c, float d) {
return (x - a) * (d - c) / (b - a) + c;
}
float rawToPPM(int raw) {
float vout = (raw / ADC_MAX) * VCC;
if (vout <= 0.001) vout = 0.001;
float rs = ((VCC - vout) / vout) * MQ135_RLOAD;
float ratio = rs / MQ135_RZERO;
return 116.6020682 * pow(ratio, -2.769034857);
}
float ppmToAQI(float ppm) {
float aqi;
if (ppm < 400) aqi = mapf(ppm, 0, 400, 0, 50);
else if (ppm < 700) aqi = mapf(ppm, 400, 700, 50, 100);
else if (ppm < 1000) aqi = mapf(ppm, 700, 1000, 100, 150);
else if (ppm < 2000) aqi = mapf(ppm, 1000, 2000, 150, 200);
else if (ppm < 5000) aqi = mapf(ppm, 2000, 5000, 200, 300);
else aqi = 301;
return constrain(aqi, 0, 500);
}
String aqiCat(float aqi) {
if (aqi <= 50) return "Good";
if (aqi <= 100) return "Moderate";
if (aqi <= 150) return "Sensitive";
if (aqi <= 200) return "Unhealthy";
if (aqi <= 300) return "V.Unhealthy";
return "Hazardous";
}
// โโ OLED โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
/*
Layout (128x64):
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ AIR QUALITY MONITOR โ row 0 (size 1)
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ line y=9
โ 87 AQI โ row 14 (size 3 + label)
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ line y=40
โ Moderate โ row 43 (size 1)
โ 28.5ยฐC 65%RH โ row 53 (size 1)
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
*/
void updateDisplay(int raw) {
display.clearDisplay();
// Title
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.print("AIR QUALITY MONITOR");
display.drawLine(0, 9, 127, 9, SSD1306_WHITE);
if (CALIBRATION_MODE) {
float vout = (raw / ADC_MAX) * VCC;
if (vout <= 0.001) vout = 0.001;
float rs = ((VCC - vout) / vout) * MQ135_RLOAD;
float rzero = rs * pow(3.6, 1.0 / 2.769);
display.setCursor(0, 14); display.print("CALIBRATION MODE");
display.setCursor(0, 26); display.print("RAW: "); display.print(raw);
display.setCursor(0, 38); display.print("Rs: "); display.print(rs, 2);
display.setCursor(0, 50); display.print("R0: "); display.print(rzero, 2);
display.display();
return;
}
// Big AQI number
display.setTextSize(3);
display.setCursor(4, 13);
display.print((int)g_aqi);
// AQI label
display.setTextSize(1);
display.setCursor(70, 17);
display.print("AQI");
// PPM small
display.setCursor(70, 29);
display.print((int)g_ppm);
display.print("ppm");
// Divider
display.drawLine(0, 40, 127, 40, SSD1306_WHITE);
// Category (with alert marker)
display.setTextSize(1);
display.setCursor(0, 43);
if (g_aqi >= 110) display.print("!");
display.print(g_cat);
// Temp & Humidity
display.setCursor(0, 54);
if (isnan(g_temp) || isnan(g_hum)) {
display.print("DHT11: read error");
} else {
display.print(g_temp, 1);
display.print((char)247); // degree symbol
display.print("C ");
display.print(g_hum, 0);
display.print("%RH");
}
display.display();
}
// โโ Buzzer โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
void singleBeep() {
digitalWrite(BUZZER_PIN, HIGH);
delay(300);
digitalWrite(BUZZER_PIN, LOW);
}
// โโ HTTP /data โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
void handleData() {
StaticJsonDocument<300> doc;
doc["aqi"] = (int)g_aqi;
doc["ppm"] = (int)g_ppm;
doc["category"] = g_cat;
doc["alert"] = (g_aqi >= 110);
doc["temp"] = isnan(g_temp) ? -1 : round(g_temp * 10) / 10.0;
doc["humidity"] = isnan(g_hum) ? -1 : round(g_hum);
doc["ip"] = WiFi.localIP().toString();
String json;
serializeJson(doc, json);
server.sendHeader("Access-Control-Allow-Origin", "*");
server.send(200, "application/json", json);
}
void handleRoot() {
server.send(200, "text/plain",
"AirWatch ESP8266\nGET /data for JSON\nIP: " + WiFi.localIP().toString());
}
// โโ Setup โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
void setup() {
Serial.begin(115200);
delay(100);
pinMode(BUZZER_PIN, OUTPUT);
digitalWrite(BUZZER_PIN, LOW);
dht.begin();
Wire.begin(4, 5);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println("OLED init failed!");
while (true);
}
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
if (CALIBRATION_MODE) {
Serial.println("=== CALIBRATION MODE โ place in fresh air, wait 5 min ===");
display.setTextSize(1);
display.setCursor(0, 0); display.print("CALIBRATION MODE");
display.setCursor(0, 12); display.print("Fresh air needed!");
display.display();
return;
}
display.setTextSize(1);
display.setCursor(0, 0); display.print("Connecting WiFi...");
display.display();
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("Connecting");
while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); }
Serial.println("\nConnected! IP: " + WiFi.localIP().toString());
display.clearDisplay();
display.setCursor(0, 0); display.print("WiFi Connected!");
display.setCursor(0, 12); display.print(WiFi.localIP().toString());
display.display();
delay(2000);
server.on("/", handleRoot);
server.on("/data", handleData);
server.begin();
Serial.println("HTTP server started");
}
// โโ Loop โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
void loop() {
if (!CALIBRATION_MODE) server.handleClient();
unsigned long now = millis();
if (now - g_lastRead >= READ_INTERVAL) {
g_lastRead = now;
int raw = analogRead(MQ135_PIN);
// DHT11 โ read every cycle (it handles its own 1s minimum internally)
float t = dht.readTemperature();
float h = dht.readHumidity();
if (!isnan(t)) g_temp = t;
if (!isnan(h)) g_hum = h;
if (CALIBRATION_MODE) {
float vout = (raw / ADC_MAX) * VCC;
if (vout <= 0.001) vout = 0.001;
float rs = ((VCC - vout) / vout) * MQ135_RLOAD;
float rzero = rs * pow(3.6, 1.0 / 2.769);
Serial.printf("RAW: %d | Rs: %.2f kOhm | RZERO: %.2f\n", raw, rs, rzero);
updateDisplay(raw);
return;
}
g_ppm = rawToPPM(raw);
g_aqi = ppmToAQI(g_ppm);
g_cat = aqiCat(g_aqi);
Serial.printf("RAW: %d | PPM: %.1f | AQI: %.1f | %s | Temp: %.1fยฐC | Hum: %.0f%%\n",
raw, g_ppm, g_aqi, g_cat.c_str(), g_temp, g_hum);
// Buzzer
if (g_aqi >= 110 && !g_buzzerFired) {
singleBeep();
g_buzzerFired = true;
} else if (g_aqi < 110) {
g_buzzerFired = false;
}
updateDisplay(raw);
}
}AirWatch validation and troubleshooting
Understand the sensor output
Record the exact sensor model and what it measures. Gas sensors, particulate sensors, and temperature/humidity sensors are not interchangeable. A broad air-quality label does not by itself mean the device calculates a regulatory AQI. If a sensor responds to several gases, treat its output as a relative response unless it has been calibrated for a specific target.
Placement and stabilization
Follow the sensor datasheet for warm-up time. Keep the sensing element exposed to room air but protected from water and dust deposits. Avoid placing it beside a heater, cooking plume, open window, or warm voltage regulator because local heat and airflow can distort readings.
Validate readings
- Record the raw output and its units before converting it.
- Allow the sensor to stabilize, then take repeated readings in the same location.
- Compare quantitative measurements against an appropriate reference instrument; do not derive a calibration factor from one observation.
- Document sensor model, warm-up time, calibration method, units, and test conditions when sharing results.
- Label thresholds as project-defined indicators unless they come from a documented applicable standard.
Electrical checks
Verify each module supply and current requirement. Sensor heaters must not be powered from a microcontroller GPIO. Check analog-output voltage against the controller ADC limits and add suitable level conditioning when needed. Keep connections secure and investigate supply noise if readings change when relays or fans switch.
Troubleshooting
- Fixed values: check power, interface wiring, initialization, and the selected input or bus address.
- Startup drift: allow stabilization and check supply dips or self-heating.
- Sudden jumps during switching: inspect grounding, supply noise, and signal routing.
- Misleading safe/unsafe labels: display raw readings and explain the threshold instead of implying certified measurement.
A hobby monitor is useful for learning and observing trends; it should not replace certified alarms or professional instruments for health and safety decisions.
