#include <Wire.h>
#include <WiFi.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
#include "ThingSpeak.h"
#define DHTPIN 2 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT11 // DHT 11
#define MQ_PIN 4 // Analog pin connected to the MQ-135 sensor
#define BUZZER_PIN 12 // Digital pin connected to the buzzer
#define LCD_ADDRESS 0x27 // I2C address of the LCD
#define LCD_COLS 16 // Number of columns on the LCD
#define LCD_ROWS 2 // Number of rows on the LCD
const char* WIFI_NAME = "Wokwi-GUEST";
const char* WIFI_PASSWORD = "";
const int myChannelNumber = 2319977;
const char* myApiKey = "HD66D1WA9P9FW64C";
const char* server = "api.thingspeak.com";
LiquidCrystal_I2C lcd(LCD_ADDRESS, LCD_COLS, LCD_ROWS);
DHT dht(DHTPIN, DHTTYPE);
const int aqi_ppm = 0;
WiFiClient client;
void setup() {
Serial.begin(9600);
lcd.init();
lcd.backlight();
dht.begin();
pinMode(BUZZER_PIN, OUTPUT);
// Connect to Wi-Fi
WiFi.begin(WIFI_NAME, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED){
delay(1000);
Serial.println("Wifi not connected");
}
Serial.println("Wifi connected !");
Serial.println("Local IP: " + String(WiFi.localIP()));
// Initialize ThingSpeak
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client);
}
void loop() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("AIR QUALITY");
lcd.setCursor(0, 1);
lcd.print("MONITORING SYSTEM");
float temperature = dht.readTemperature(); // Read temperature
float humidity = dht.readHumidity(); // Read humidity
float airQuality = analogRead(MQ_PIN); // Read air quality value
// Convert analog reading to voltage
float voltage = airQuality * (5.0 / 1023.0);
// Convert voltage to ppm using the sensitivity of the MQ-135 sensor
float aqi_ppm = 116.6020682 * pow(voltage, -2.769034857);
// Update ThingSpeak fields
ThingSpeak.setField(1, temperature); // Field for temperature data on ThingSpeak
ThingSpeak.setField(2, humidity); // Field for humidity data on ThingSpeak
ThingSpeak.setField(2, Air Quality ); // Field for humidity data on ThingSpeak
int x = ThingSpeak.writeFields(myChannelNumber, myApiKey);
// Display values on the LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(temperature);
lcd.print(" C");
lcd.setCursor(0, 1);
lcd.print("Humidity: ");
lcd.print(humidity);
lcd.print(" %");
delay(100);
// Check for sensor reading errors
if (isnan(temperature) || isnan(humidity) || isnan(airQuality)) {
Serial.println("Failed to read from sensor!");
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Sensor error!");
tone(BUZZER_PIN, 1000, 500); // Sound the buzzer for half a second to indicate an error
} else {
// Determine air quality status and control buzzer
if ((aqi_ppm >= 0) && (aqi_ppm <= 50)) {
lcd.print("AQI Good");
digitalWrite(BUZZER_PIN, LOW); // Turn off the buzzer
} else if ((aqi_ppm > 50) && (aqi_ppm <= 100)) {
lcd.print("AQI Moderate");
tone(BUZZER_P IN, 1000, 200); // Sound the buzzer for moderate air quality
} else if ((aqi_ppm > 100) && (aqi_ppm <= 200)) {
lcd.print("AQI Unhealthy");
tone(BUZZER_PIN, 1000, 200); // Sound the buzzer for unhealthy air quality
} else if ((aqi_ppm > 200) && (aqi_ppm <= 300)) {
lcd.print("AQI V.Unhealthy");
tone(BUZZER_PIN, 1000, 200); // Sound the buzzer for very unhealthy air quality
} else {
lcd.print("Hazardous");
tone(BUZZER_PIN, 1000, 200); // Sound the buzzer for hazardous air quality
}
}
// Wait for 1 second
// Print sensor readings and AQI to serial monitor
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" C");
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println(" %");
Serial.print("Air Quality: ");
Serial.print(aqi_ppm);
Serial.println(" ppm");
delay(2000); // Wait for 2 seconds before taking the next reading
}