Skip to content

ESP8266 ยท Build guide

๐ŸŒฟ AirWatch โ€” Air Quality Monitoring System

A self-contained air quality monitor built with an ESP8266, MQ135 gas sensor, and OLED display โ€” shows live AQI, sounds a buzzer past a set threshold, and streams readings to a web dashboard over Wi-Fi.

๐ŸŒฟ AirWatch โ€” Air Quality Monitoring System
Difficulty
Intermediate
Read time
11 min
Board
ESP8266
Author
Ksp_wirespace
Published
Jul 6, 2026
Views
4

๐ŸŒฟ 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

ComponentDetails
MicrocontrollerESP8266 NodeMCU v1/v2 (or any ESP8266 module)
Gas SensorMQ135 Air Quality Sensor module
DisplaySSD1306 0.96โ€ณ OLED โ€” 128ร—64 px โ€” IยฒC address 0x3C
BuzzerActive buzzer (3.3 V or 5 V with transistor)
ResistorsTwo 10 kฮฉ resistors (voltage divider for MQ135 AOUT โ†’ A0)
Power5 V micro-USB to NodeMCU + 5 V to MQ135 VCC
MiscJumper wires, breadboard or PCB

3. Wiring

Pin Map

ComponentPinESP8266 PinNotes
MQ135AOUTA0Via 10 kฮฉ + 10 kฮฉ voltage divider
MQ135VCCVin (5 V)MQ135 heater requires 5 V
MQ135GNDGND
OLEDSDAD2 (GPIO4)IยฒC data
OLEDSCLD1 (GPIO5)IยฒC clock
OLEDVCC3.3 V
OLEDGNDGND
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.

Output
MQ135 AOUT โ”€โ”€โ”ฌโ”€โ”€ 10 kฮฉ โ”€โ”€ A0
             โ”‚
            10 kฮฉ
             โ”‚
            GND

Connect 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

  1. Open Arduino IDE โ†’ File โ†’ Preferences โ†’ Additional Board URLs
  2. Add: https://arduino.esp8266.com/stable/package_esp8266com_index.json
  3. Tools โ†’ Board Manager โ†’ search esp8266 โ†’ Install
  4. Select: Tools โ†’ Board โ†’ NodeMCU 1.0 (ESP-12E Module)
  5. Set Upload Speed: 115200, Port: your COM/ttyUSB port

4.2 Required Libraries

Install via Sketch โ†’ Include Library โ†’ Manage Libraries:

LibraryVersion
Adafruit SSD1306Latest
Adafruit GFX LibraryLatest
ArduinoJson6.x (not v5 or v7)
ESP8266WiFiBuilt-in with board package
ESP8266WebServerBuilt-in with board package

4.3 Configuration

Edit the top of air_quality_esp8266.ino before uploading:

sketch.ino
// 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

  1. Set #define CALIBRATION_MODE true in the firmware
  2. Upload to the ESP8266
  3. Take the sensor outdoors or near an open window
  4. Open Serial Monitor at 115200 baud
  5. Wait at least 5 minutes for the heater to stabilise โ€” watch the RZERO column
  6. Note the value once it stabilises (typically 20โ€“50 with voltage divider)
  7. Paste it into #define MQ135_RZERO <your value>, set CALIBRATION_MODE false, re-upload

Expected Serial Output (Calibration Mode)

Output
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 min

Tip: Take the average of readings over the last 2 minutes of the warm-up period.


6. Normal Operation

OLED Display Layout

Output
+-----------------------------+
| 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

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 | Sensitive

AQI Bands

AQI RangeCategoryDescription
0 โ€“ 50GoodAir quality is satisfactory
51 โ€“ 100ModerateAcceptable; some pollutants may be a concern for a small number of sensitive people
101 โ€“ 150SensitiveMembers of sensitive groups may experience health effects
151 โ€“ 200UnhealthyEveryone may begin to experience health effects
201 โ€“ 300Very UnhealthyHealth alert; everyone may experience more serious effects
301+HazardousHealth 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

MethodEndpointResponse
GET/Plain-text status and IP
GET/dataJSON with current readings

Sample JSON Response

config.json
{
  "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

webdashboard

Open airwatch_dashboard.html in any modern browser. It runs a demo animation automatically before connecting to hardware.

Connecting to Live Hardware

  1. Find the ESP8266 IP address on the OLED at boot or in Serial Monitor
  2. Enter the IP into the ESP IP field in the dashboard
  3. Click Connect โ€” data refreshes every 2.5 seconds

Dashboard Components

ComponentDescription
Ring GaugeAnimated circular gauge with tick marks; colour changes per AQI band
AQI NumberLarge number with matching colour glow
PPM ReadingShown inside the gauge below the AQI value
Category BadgeColour-coded pill showing the current band name
Alert BannerRed flashing banner when AQI โ‰ฅ 110
AQI ScaleSix colour-coded bands; active band highlights live
History ChartSparkline of last 40 readings with dashed alert line at 110
Stats RowCurrent / Min / Max / Avg / Reading count / Alert count

9. Firmware Reference

Key Functions

FunctionDescription
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

EventInterval
Sensor read + OLED updateEvery 2000 ms
Buzzer pulse duration300 ms
Dashboard fetch intervalEvery 2500 ms (client-side)

PPM Formula

Output
Rs    = ((VCC - Vout) / Vout) ร— RLOAD
ratio = Rs / RZERO
PPM   = 116.6020682 ร— ratio^(โˆ’2.769034857)

10. Troubleshooting

SymptomFix
AQI shows 301 constantlyRZERO needs calibration. Run calibration mode outdoors for 5+ min and update MQ135_RZERO.
AQI too high right after power-onNormal โ€” MQ135 heater needs 2โ€“5 min warm-up. Wait before trusting readings.
OLED shows nothingCheck SDAโ†’D2 and SCLโ†’D1. Verify IยฒC address is 0x3C. Some clones use 0x3D.
Wi-Fi never connectsCheck WIFI_SSID / WIFI_PASSWORD. ESP8266 only supports 2.4 GHz networks.
Dashboard shows OFFLINEConfirm the IP.
sketch.ino
/*
 * ============================================================
 *  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

  1. Record the raw output and its units before converting it.
  2. Allow the sensor to stabilize, then take repeated readings in the same location.
  3. Compare quantitative measurements against an appropriate reference instrument; do not derive a calibration factor from one observation.
  4. Document sensor model, warm-up time, calibration method, units, and test conditions when sharing results.
  5. 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.

Build Sheet

Take this build with you

We'll email you the parts list for ๐ŸŒฟ AirWatch โ€” Air Quality Monitoring System, with parts information, available SolderHub Simulator links, and source-code links, so you can order and build later. You'll also get new builds as they're published.

By subscribing you agree to our Privacy Policy. Unsubscribe anytime.

Topicsesp8266aqiaqi-systemactive-buzzer

Questions & Answers