#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
// Define pins
#define FLAME_SENSOR_PIN 5 // Simulating flame sensor using a push button
#define GAS_SENSOR_PIN 34 // Simulating gas sensor using a potentiometer
#define LED_PIN 2 // LED for alert
#define BUZZER_PIN 4 // Buzzer for alert
// Define Wi-Fi credentials (update with your network info)
const char* ssid = "your_SSID"; // Replace with your Wi-Fi SSID
const char* password = "your_PASSWORD"; // Replace with your Wi-Fi password
// Define Blynk authentication token (replace with the one received via email)
char auth[] = "EZ3-4DOs0LIfhcMm7KoC9jpU0fKRu6YD"; // Replace with your token
// Template Information (for Blynk IoT)
#define BLYNK_TEMPLATE_ID "TMPL3k3RX2Fkj"
#define BLYNK_TEMPLATE_NAME "fire detection"
// Initialize Blynk
BlynkTimer timer;
void setup() {
// Initialize Serial Monitor
Serial.begin(115200);
// Initialize pins
pinMode(FLAME_SENSOR_PIN, INPUT_PULLUP); // Flame sensor as input (button)
pinMode(GAS_SENSOR_PIN, INPUT); // Gas sensor as analog input
pinMode(LED_PIN, OUTPUT); // LED as output
pinMode(BUZZER_PIN, OUTPUT); // Buzzer as output
// Connect to Wi-Fi
WiFi.begin(ssid, password);
Serial.println("Connecting to Wi-Fi...");
unsigned long startAttemptTime = millis();
// Attempt to connect for 10 seconds max
while (WiFi.status() != WL_CONNECTED && millis() - startAttemptTime < 10000) {
delay(1000);
Serial.println("Attempting to connect to Wi-Fi...");
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println("Connected to Wi-Fi");
} else {
Serial.println("Failed to connect to Wi-Fi within timeout!");
return; // Exit setup if Wi-Fi connection fails
}
// Connect to Blynk server using the template-based system
Blynk.begin(auth, ssid, password, BLYNK_TEMPLATE_ID, BLYNK_TEMPLATE_NAME);
// Set up a function to send sensor data every 2 seconds
timer.setInterval(2000L, sendSensorData);
}
void loop() {
// Run Blynk and timer
Blynk.run();
timer.run();
}
void sendSensorData() {
// Read flame sensor (push button)
int flameStatus = digitalRead(FLAME_SENSOR_PIN);
// Read gas sensor (potentiometer)
int gasLevel = analogRead(GAS_SENSOR_PIN);
// Print sensor values for debugging
Serial.print("Flame Status: ");
Serial.print(flameStatus);
Serial.print(" | Gas Level: ");
Serial.println(gasLevel);
// Send data to Blynk app (send data to virtual pins)
Blynk.virtualWrite(V1, flameStatus); // Send flame status to virtual pin V1
Blynk.virtualWrite(V2, gasLevel); // Send gas level to virtual pin V2
// If flame or gas detected (adjust thresholds as needed)
if (flameStatus == LOW || gasLevel > 500) {
// Turn on LED and Buzzer
digitalWrite(LED_PIN, HIGH);
digitalWrite(BUZZER_PIN, HIGH);
// Send an alert via Blynk (using virtual pin V3 to indicate alert)
Blynk.virtualWrite(V3, 1); // V3 could be used for an alert (1 = active)
} else {
// Turn off LED and Buzzer
digitalWrite(LED_PIN, LOW);
digitalWrite(BUZZER_PIN, LOW);
// Reset alert
Blynk.virtualWrite(V3, 0); // V3 = 0 to indicate no alert
}
}