// Template and device credentials for Blynk
#define BLYNK_TEMPLATE_ID "TMPL6YAS5_ss3"
#define BLYNK_TEMPLATE_NAME "IoT Smart Exhaust Fan"
#define BLYNK_PRINT Serial // Directs Blynk messages to the serial port
#include <WiFi.h> // Include WiFi library for ESP32
//#include <BlynkSimpleEsp32.h> // Disabled for Wokwi simulation
#include "DHT.h" // Include DHT sensor library
// WiFi credentials for Wokwi simulation
const char auth[] = "SIMULATION_MODE"; // Placeholder for Blynk token
const char ssid[] = "Wokwi-GUEST"; // Wokwi default SSID
const char pass[] = ""; // No password for Wokwi WiFi
const int channel = 6; // Wokwi channel
// Pin configuration
#define DHTPIN 22 // DHT sensor pin
#define DHTTYPE DHT11 // Type of DHT sensor
const int gasSensorPin = 34; // Gas sensor pin
const int relayPin = 23; // Relay module pin
const int gasThreshold = 20; // Gas level threshold for triggering the relay
DHT dht(DHTPIN, DHTTYPE); // Initialize DHT sensor
bool manualMode = false; // Flag to track if the relay is in manual mode
void setup()
{
Serial.begin(115200); // Start serial communication at 115200 baud rate
dht.begin(); // Initialize DHT sensor
// Connect to Wokwi WiFi
Serial.println("Connecting to WiFi...");
WiFi.begin(ssid, pass, channel);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println("\nConnected to Wokwi WiFi");
pinMode(relayPin, OUTPUT); // Set relay pin as output
pinMode(gasSensorPin, INPUT); // Set gas sensor pin as input
digitalWrite(relayPin, HIGH); // Active LOW relay OFF initially
delay(2000); // Wait for 2 seconds before proceeding
}
// Function to handle manual relay control (Simulation only)
void setManualRelayControl(int relayControl)
{
manualMode = (relayControl == 1);
if (manualMode)
{
digitalWrite(relayPin, LOW); // Active LOW relay ON
}
else
{
digitalWrite(relayPin, HIGH); // Active LOW relay OFF
}
Serial.print("Manual Mode Relay Status: ");
Serial.println(relayControl ? "ON" : "OFF");
}
void loop()
{
int sensorValue = analogRead(gasSensorPin); // Read gas sensor value
int gas_percentage = map(sensorValue, 0, 4095, 0, 100); // Convert to percentage
float humidity = dht.readHumidity(); // Read humidity from DHT sensor
float temperature = dht.readTemperature();// Read temperature from DHT sensor
// Debug raw MQ-2 reading
Serial.print("Raw MQ-2 Value: ");
Serial.println(sensorValue);
// Check if sensor readings are valid
if (isnan(humidity) || isnan(temperature))
{
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
// Print sensor values to the Serial Monitor
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println("%");
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println("°C");
Serial.print("Gas Percentage: ");
Serial.print(gas_percentage);
Serial.println("%");
// Automatic control logic based on gas levels
if (!manualMode)
{
if (gas_percentage > gasThreshold)
{
digitalWrite(relayPin, LOW); // Active LOW relay ON
Serial.println("⚠️ Gas detected! Relay ON");
}
else
{
digitalWrite(relayPin, HIGH); // Active LOW relay OFF
Serial.println("✅ Safe level. Relay OFF");
}
}
// Simulation: Replace Blynk.virtualWrite with Serial outputs
Serial.print("Send to Dashboard -> Gas: ");
Serial.print(gas_percentage);
Serial.print("% | Temp: ");
Serial.print(temperature);
Serial.print("°C | Humidity: ");
Serial.print(humidity);
Serial.println("%");
Serial.println("-----------------------------");
delay(1000); // Wait for a second before next loop iteration
}