🔥 Lesson 3.4 – Complete Fire Alert System Code & Detailed Explanation
In this lesson, we will write the complete ESP32 program for our IoT-Based Fire Alert System and understand how each part of the code works.
The system will:
✅ Detect fire using a Flame Sensor
✅ Turn ON an LED when fire is detected
✅ Activate a Buzzer
✅ Send Fire Status to Blynk Cloud
✅ Display Status Messages on Dashboard
✅ Allow Remote Monitoring
💻 Complete ESP32 Fire Alert System Code
/********************************************************
IoT Based Fire Alert System
ESP32 + Flame Sensor + LED + Buzzer + Blynk
********************************************************/
#define BLYNK_TEMPLATE_ID "Your_Template_ID"
#define BLYNK_TEMPLATE_NAME "Fire Alert System"
#define BLYNK_AUTH_TOKEN "Your_Auth_Token"
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
// WiFi Credentials
char ssid[] = "Your_WiFi_Name";
char pass[] = "Your_WiFi_Password";
// Pin Definitions
#define FLAME_SENSOR 27
#define LED_PIN 26
#define BUZZER_PIN 25
void setup()
{
Serial.begin(115200);
// Pin Modes
pinMode(FLAME_SENSOR, INPUT);
pinMode(LED_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
// Initial State
digitalWrite(LED_PIN, LOW);
digitalWrite(BUZZER_PIN, LOW);
Serial.println("==================================");
Serial.println("🔥 Fire Alert System Starting...");
Serial.println("==================================");
// Connect to Blynk
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
Serial.println("✅ Connected to Blynk Cloud");
}
void loop()
{
Blynk.run();
int flameStatus = digitalRead(FLAME_SENSOR);
if (flameStatus == LOW)
{
Serial.println("🔥 FIRE DETECTED!");
digitalWrite(LED_PIN, HIGH);
digitalWrite(BUZZER_PIN, HIGH);
Blynk.virtualWrite(V0, 1);
Blynk.virtualWrite(V1, "🔥 Fire Detected");
}
else
{
Serial.println("✅ No Fire");
digitalWrite(LED_PIN, LOW);
digitalWrite(BUZZER_PIN, LOW);
Blynk.virtualWrite(V0, 0);
Blynk.virtualWrite(V1, "✅ System Safe");
}
delay(1000);
}
📝 Code Explanation
🔹 1) Blynk Configuration
#define BLYNK_TEMPLATE_ID "Your_Template_ID"
#define BLYNK_TEMPLATE_NAME "Fire Alert System"
#define BLYNK_AUTH_TOKEN "Your_Auth_Token"
These values are copied from the Blynk Console.
Purpose
✅ Identifies your project
✅ Connects ESP32 to Blynk Cloud
✅ Authenticates the device
🔹 2) Required Libraries
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
WiFi.h
Used to connect ESP32 to a WiFi network.
BlynkSimpleEsp32.h
Used to communicate with Blynk Cloud.
🔹 3) WiFi Credentials
char ssid[] = "Your_WiFi_Name";
char pass[] = "Your_WiFi_Password";
Replace these values with your WiFi details.
Example:
char ssid[] = "MyHomeWiFi";
char pass[] = "12345678";
🔹 4) Pin Definitions
#define FLAME_SENSOR 27
#define LED_PIN 26
#define BUZZER_PIN 25
These lines define the GPIO pins used in the project.
| Device | ESP32 GPIO |
|---|---|
| Flame Sensor | GPIO 27 |
| LED | GPIO 26 |
| Buzzer | GPIO 25 |
🔹 5) setup() Function
void setup()
The setup function runs only once when ESP32 starts.
Start Serial Communication
Serial.begin(115200);
This starts communication between ESP32 and the computer.
📌 Serial Monitor must also be set to 115200 baud.
Configure Pin Modes
pinMode(FLAME_SENSOR, INPUT);
pinMode(LED_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
Purpose
- Flame Sensor → Input
- LED → Output
- Buzzer → Output
Set Initial State
digitalWrite(LED_PIN, LOW);
digitalWrite(BUZZER_PIN, LOW);
At startup:
💡 LED OFF
🔔 Buzzer OFF
Display Startup Messages
Serial.println("🔥 Fire Alert System Starting...");
Shows project startup information on the Serial Monitor.
Connect to Blynk
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
This command:
✅ Connects ESP32 to WiFi
✅ Connects ESP32 to Blynk Cloud
🔹 6) loop() Function
void loop()
Runs continuously after setup() finishes.
Keep Blynk Running
Blynk.run();
Required for communication between ESP32 and Blynk Cloud.
Without this line, dashboard updates will not work.
Read Flame Sensor
int flameStatus = digitalRead(FLAME_SENSOR);
Reads the current state of the flame sensor.
Possible values:
HIGH = No Fire
LOW = Fire Detected
🔹 7) Fire Detection Logic
if (flameStatus == LOW)
If the flame sensor detects fire, the code inside the IF block executes.
Fire Alert Actions
digitalWrite(LED_PIN, HIGH);
digitalWrite(BUZZER_PIN, HIGH);
Actions performed:
💡 LED turns ON
🔔 Buzzer turns ON
Send Data to Blynk
Blynk.virtualWrite(V0, 1);
Sends fire status to Virtual Pin V0.
1 = Fire Detected
Send Status Message
Blynk.virtualWrite(V1, "🔥 Fire Detected");
Displays a readable message on the dashboard.
🔹 8) No Fire Condition
else
Runs when no flame is detected.
Turn OFF Alerts
digitalWrite(LED_PIN, LOW);
digitalWrite(BUZZER_PIN, LOW);
Result:
💡 LED OFF
🔔 Buzzer OFF
Update Dashboard
Blynk.virtualWrite(V0, 0);
0 = No Fire
Send Safe Message
Blynk.virtualWrite(V1, "✅ System Safe");
Displays a safe status on the dashboard.
🔹 9) Delay Function
delay(1000);
Delay time:
1000 milliseconds = 1 second
The system updates every second.
📱 Dashboard Data
| Virtual Pin | Purpose |
|---|---|
| V0 | Fire Status (0 or 1) |
| V1 | Status Message |
🔄 Program Flow
ESP32 Starts
↓
Connect to WiFi
↓
Connect to Blynk Cloud
↓
Read Flame Sensor
↓
Fire Detected?
↓
┌─────────────┬─────────────┐
│ YES │ NO │
▼ ▼
LED ON LED OFF
Buzzer ON Buzzer OFF
V0 = 1 V0 = 0
V1 = Fire V1 = Safe
│ │
└───────┬─────┘
▼
Repeat Forever
🎯 Lesson Summary
In this lesson, we created the complete IoT Fire Alert System using ESP32, Flame Sensor, LED, Buzzer, and Blynk Cloud. We learned how the ESP32 detects fire, activates local alerts, sends real-time data to Blynk, and displays readable status messages on the dashboard using Virtual Pins V0 and V1. In the next lesson, we will test the project, verify the dashboard, and troubleshoot common issues.