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.
#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.