Cirkit Designer Logo
Cirkit Designer
Your all-in-one circuit design IDE
Home / 
Project Documentation

Wi-Fi-Enabled Air Quality Monitoring System with MQ135 and NodeMCU ESP8266

Image of Wi-Fi-Enabled Air Quality Monitoring System with MQ135 and NodeMCU ESP8266

Circuit Documentation

Summary

The circuit in question is designed to monitor air quality using an MQ135 sensor and display the readings on a 16x2 LCD. The NodeMCU V3 ESP8266 microcontroller is used to process the sensor data and control the display. The circuit also includes a buzzer for alerting when air quality levels are dangerous, and a potentiometer to adjust the LCD contrast. A resistor is used to provide backlight for the LCD. The NodeMCU V3 ESP8266 is also capable of sending the air quality data to a remote server via Wi-Fi.

Component List

MQ135 Air Quality Sensor

  • Pins: VCC, GND, A0, D0
  • Description: Used to measure the air quality by detecting various gases in the air.

NodeMCU V3 ESP8266

  • Pins: A0, GND, VU, S3, S2, S1, SC, S0, SK, 3V3, EN, RST, Vin, D0, D1, D2, D3, D4, D5, D6, D7, D8, RX, TX
  • Description: A microcontroller with Wi-Fi capability for processing sensor data and controlling other components in the circuit.

16X2 LCD

  • Pins: VSS, VDD, V0, RS, RW, E, D0, D1, D2, D3, D4, D5, D6, D7, A, K
  • Description: A display used to show the air quality readings and status messages.

Resistor

  • Pins: pin1, pin2
  • Description: A 200 Ohm resistor used to provide backlight for the LCD.

Potentiometer

  • Pins: GND, Output, VCC
  • Description: Used to adjust the contrast of the LCD display.

Buzzer

  • Pins: POSITIVE, NEGATIVE
  • Description: An audible alert device that is activated when air quality reaches dangerous levels.

Wiring Details

MQ135 Air Quality Sensor

  • VCC: Connected to the power supply net.
  • GND: Connected to the ground net.
  • A0: Connected to A0 on the NodeMCU V3 ESP8266.

NodeMCU V3 ESP8266

  • Vin: Connected to the power supply net.
  • GND: Connected to the ground net.
  • A0: Connected to A0 on the MQ135 sensor.
  • D0-D7: Connected to D7-D0 on the 16X2 LCD respectively.
  • D8: Connected to the POSITIVE pin of the Buzzer.

16X2 LCD

  • VDD: Connected to the power supply net.
  • VSS, K: Connected to the ground net.
  • V0: Connected to the Output of the Potentiometer.
  • RS, E, D4-D7: Connected to D5, D4, D3-D0 on the NodeMCU V3 ESP8266 respectively.
  • A: Connected to pin1 of the Resistor.

Resistor

  • pin1: Connected to the A pin of the 16X2 LCD.
  • pin2: Connected to the power supply net.

Potentiometer

  • VCC: Connected to the power supply net.
  • GND: Connected to the ground net.
  • Output: Connected to V0 on the 16X2 LCD.

Buzzer

  • POSITIVE: Connected to D8 on the NodeMCU V3 ESP8266.
  • NEGATIVE: Connected to the ground net.

Documented Code

#include <ESP8266WiFi.h>
#include <LiquidCrystal.h>  // include the library code:

// LCD interface pin association with the NodeMCU pin number
const int rs = D5, en = D4, d4 = D3, d5 = D2, d6 = D1, d7 = D0;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

const int aqsensor = A0;  // MQ135 connected to A0 pin of NodeMCU

int gled = D6;  // Green LED connected to pin D6
int rled = D7;  // Red LED connected to pin D7
int buz = D8;   // Buzzer connected to pin D8

String apiKey = "YOUR_API_KEY";     // Enter your Write API key here
const char *ssid =  "YOUR_WIFI_SSID";     // Enter your WiFi Name
const char *pass =  "YOUR_WIFI_PASSWORD"; // Enter your WiFi Password
const char* server = "api.thingspeak.com";
WiFiClient client;

void setup() {
  pinMode(gled, OUTPUT);
  pinMode(aqsensor, INPUT);
  pinMode(rled, OUTPUT);
  pinMode(buz, OUTPUT);
  
  Serial.begin(115200);
  lcd.clear();
  lcd.begin(16,2);

  Serial.println("Connecting to ");
  lcd.print("Connecting.... ");
  Serial.println(ssid);
  WiFi.begin(ssid, pass);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print(".");
    lcd.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.print("IP Address: ");
  delay(5000);
  Serial.println(WiFi.localIP());
  delay(5000);
}

void loop() {
  int ppm = analogRead(aqsensor); // Read MQ135 output

  Serial.print("Air Quality: ");
  Serial.println(ppm);

  lcd.setCursor(0,0);
  lcd.print("Air Quality: ");
  lcd.print(ppm);
  delay(1000);

  if (client.connect(server,80)) {
    String postStr = apiKey;
    postStr +="&field1=";
    postStr += String(ppm);
    postStr += "\r\n\r\n";

    client.print("POST /update HTTP/1.1\n");
    client.print("Host: api.thingspeak.com\n");
    client.print("Connection: close\n");
    client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n");
    client.print("Content-Type: application/x-www-form-urlencoded\n");
    client.print("Content-Length: ");
    client.print(postStr.length());
    client.print("\n\n");
    client.print(postStr);
    Serial.print("Air Quality: ");
    Serial.print(ppm);
    Serial.println(" PPM. Sent to Thingspeak.");
  }
  if(ppm <= 200) {
    digitalWrite(gled, LOW);
    digitalWrite(rled, LOW);
    digitalWrite(buz, LOW);
    lcd.setCursor(1,1);
    lcd.print("AQ Level Normal");
    Serial.println("AQ Level Normal");
  } else if (ppm > 200 && ppm < 350) {
    digitalWrite(gled, HIGH);
    digitalWrite(rled, LOW);
    digitalWrite(buz, LOW);
    lcd.setCursor(1,1);
    lcd.print("AQ Level Medium");
    Serial.println("AQ Level Medium");
  } else {
    lcd.setCursor(1,1);
    lcd.print("AQ Level Danger!");
    Serial.println("AQ Level Danger!");
    digitalWrite(gled, LOW);
    digitalWrite(rled, HIGH);
    digitalWrite(buz, HIGH);
  }

  client.stop();
  Serial.println("Waiting...");
  delay(1000);
}

Note: Replace YOUR_API_KEY, YOUR_WIFI_SSID, and YOUR_WIFI_PASSWORD with your actual API key, Wi-Fi SSID, and password before uploading the code to the NodeMCU.