// AirGuardian Pro - Wokwi Simulation (ESP32)
// Sensors simulated by potentiometers + button
#include <ESP32Servo.h> // Wokwi can auto-fetch this
// --- Pins ---
const int PIN_MQ135 = 34; // pot #1
const int PIN_MQ6 = 35; // pot #2
const int PIN_FLAME = 25; // pushbutton to GND (INPUT_PULLUP)
const int PIN_LED_R = 18;
const int PIN_LED_G = 19;
const int PIN_LED_B = 21;
const int PIN_FAN = 22; // LED = fan indicator
const int PIN_BUZZ = 26; // ACTIVE buzzer (HIGH = ON)
const int PIN_SERVO = 23;
Servo gasValve;
// --- Thresholds (0..4095 for ESP32 ADC) ---
int AIR_WARN = 1202; // adjust during sim
int GAS_ALERT = 2800; // adjust during sim
void setColor(bool r, bool g, bool b) {
digitalWrite(PIN_LED_R, r ? HIGH : LOW);
digitalWrite(PIN_LED_G, g ? HIGH : LOW);
digitalWrite(PIN_LED_B, b ? HIGH : LOW);
}
void setup() {
Serial.begin(115200);
Serial.println("AirGuardian Mini Booting...");
pinMode(PIN_FLAME, INPUT_PULLUP);
pinMode(PIN_LED_R, OUTPUT);
pinMode(PIN_LED_G, OUTPUT);
pinMode(PIN_LED_B, OUTPUT);
pinMode(PIN_FAN, OUTPUT);
pinMode(PIN_BUZZ, OUTPUT);
gasValve.attach(PIN_SERVO);
gasValve.write(0); // gas ON position (demo)
// Start safe
setColor(false, true, false); // Green
digitalWrite(PIN_FAN, LOW);
digitalWrite(PIN_BUZZ, LOW);
Serial.println("System Ready. Monitoring...");
}
void loop() {
int air = analogRead(PIN_MQ135);
int gas = analogRead(PIN_MQ6);
bool flameDetected = (digitalRead(PIN_FLAME) == LOW);
Serial.printf("AIR=%d | GAS=%d | FLAME=%d\n", air, gas, flameDetected);
bool emergency = (gas > GAS_ALERT) || flameDetected;
if (emergency) {
// 🚨 Red state
setColor(true, false, false);
digitalWrite(PIN_FAN, HIGH);
digitalWrite(PIN_BUZZ, HIGH);
gasValve.write(90);
Serial.println("🚨 EMERGENCY! Gas/Flame Detected -> LED Red, Buzzer ON, Valve CLOSED");
} else if (air > AIR_WARN) {
// ⚠ Yellow state
setColor(true, true, false);
digitalWrite(PIN_FAN, HIGH);
digitalWrite(PIN_BUZZ, LOW);
gasValve.write(0);
Serial.println("⚠ Air Pollution High -> Fan ON, LED Yellow");
} else {
// ✅ Green state
setColor(false, true, false);
digitalWrite(PIN_FAN, LOW);
digitalWrite(PIN_BUZZ, LOW);
gasValve.write(0);
Serial.println("✅ Air Clean -> Fan OFF, LED Green");
}
delay(500);
}