#define BLYNK_TEMPLATE_ID "TMPL6kWN92xgM"
#define BLYNK_TEMPLATE_NAME "Automated Air purifier"
#define BLYNK_AUTH_TOKEN "29-TfEOHXuD37x_ERtbiYVxHfZMiodqj"

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <BlynkSimpleEsp32.h>
#include <WiFi.h>
#include <ESP32Servo.h>
#include <DHTesp.h>

// Define the pins for the DHT22 sensor
#define DHTPIN 2  // Replace with the actual pin connected to DHT22
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);

LiquidCrystal_I2C lcd(0x27, 16, 2);  // 0x27 is the I2C address of the LCD
const int ledPin = 4;   // Replace with the actual pin connected to the LED
const int mq135Pin = 12;
const int purificationPin = 5; // Pin connected to the air purification system

Servo AirPurifier;
int Position = 0;
char ssid[] = "Wokwi-GUEST";
char pass[] = "";

BlynkTimer timer;

// Gas value received from the MQ-135 chip
int gasValue = 0;

BLYNK_WRITE(V4) {
  int newValue = param.asInt();
  gasValue = newValue;
}

void sendData() {
  float temperature = dht.readTemperature();
  float humidity = dht.readHumidity();

  Blynk.virtualWrite(V1, temperature);
  Blynk.virtualWrite(V2, humidity);
  Blynk.virtualWrite(V3, gasValue);
}

void updateGasValue(int newValue) {
  gasValue = newValue;
}

void setup() {
  AirPurifier.attach(5);
  lcd.init();
  lcd.backlight();
  dht.begin();
  pinMode(ledPin, OUTPUT);

  WiFi.begin(ssid, pass);
  while (WiFi.status() != WL_CONNECTED) {
    delay(250);
  }

  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);

  Blynk.virtualWrite(V1, 0);
  Blynk.virtualWrite(V2, 0);
  Blynk.virtualWrite(V3, gasValue);

  timer.setInterval(3000L, sendData);

  Serial.println("Setup complete");
}

void loop() {
  Blynk.run();
  timer.run();

  float temperature = dht.readTemperature();
  float humidity = dht.readHumidity();
  int gasValue = analogRead(mq135Pin);

  String airLevel = (temperature >= 22 && temperature <= 30 && humidity > 30 && humidity < 60) ? "Good" :
                    (temperature >= 30 && temperature <= 40 && humidity >= 60 && humidity <= 70) ? "Normal" : "Bad";

  String gasLevel = (gasValue >= 0 && gasValue <= 1364) ? "Good" :
                    (gasValue >= 1365 && gasValue <= 2730) ? "Normal" : "Bad";

  String airQuality = ((airLevel == "Good" || airLevel == "Normal") && (gasLevel == "Good" || gasLevel == "Normal")) ? "Good Air Quality" : "Bad Air Quality";

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Temp: " + String(temperature) + " C");
  lcd.setCursor(0, 1);
  lcd.print("Humidity: " + String(humidity) + " %");
  delay(2000);

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Air Level: " + airLevel);
  delay(2000);

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Gas Level: ");
  lcd.setCursor(0, 1);
  lcd.print("Gas Value: ");
  delay(2000);

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Air Quality: ");
  lcd.setCursor(0, 1);
  lcd.print(airQuality);
  delay(2000);

  if (airQuality == "Bad Air Quality") {
    digitalWrite(ledPin, HIGH); // Turn on the LED
    for (Position = 0; Position <= 180; Position += 1) {
      AirPurifier.write(Position);
      delay(7);
    }
    for (Position = 180; Position >= 0; Position -= 1) {
      AirPurifier.write(Position);
      delay(7);
    }
     
  } else {
    digitalWrite(ledPin, LOW); // Turn off the LED
    //digitalWrite(purificationPin, LOW); // Turn off the air purification system
  }
}
$abcdeabcde151015202530fghijfghij
mq-135Breakout