Email Fire Detection and Alert System Using an ESP32 Microcontroller

by yuvrajkaniyar in Circuits > Arduino

49 Views, 0 Favorites, 0 Comments

Email Fire Detection and Alert System Using an ESP32 Microcontroller

dh11se.jpg
WhatsApp Image 2024-12-07 at 09.23.40_b0b3cf0f.jpg

The system automatically detects fire and notifies the user via email. This system is useful for applications in home automation, industrial safety, or any environment where fire detection is critical.


Working Principle :

Fire Detection:

  1. A flame sensor is connected to the ESP32.
  2. It continuously monitors for the presence of fire. The sensor outputs a digital LOW signal when fire is detected.

Email Alert:

  1. Upon fire detection, the ESP32 uses the ESP_Mail_Client library to connect to an SMTP server.
  2. It sends an email alert to a predefined recipient.


Supplies

  1. ESP32 Microcontroller: Acts as the core system for monitoring the flame sensor and sending emails.
  2. Flame Sensor: Detects the presence of fire or flames. Here we used DHT11 sensor for operation.
  3. Power Supply: Powers the ESP32 and the sensor.
  4. Wi-Fi Connectivity: For sending email notifications.
  5. Software Requirements: Arduino IDE with required libraries (WiFi and ESP_Mail_Client).

Hardware Setup

1.Connect Flame Sensor:

  1. Connect the signal pin of the flame sensor to GPIO 5 of the ESP32.
  2. Connect VCC and GND of the sensor to a 3.3V power pin and ground pin of the ESP32.

2.ESP32 Connection:

  1. Ensure the ESP32 is powered via USB or a suitable power source.


Software Setup

Libraries Installation:

  1. Install the following libraries in Arduino IDE:
  2. WiFi.h (for Wi-Fi connectivity).
  3. ESP_Mail_Client.h (for sending emails).


Arduino IDE Configuration:

  1. Select the correct ESP32 board and port in the Arduino IDE.


Steps to Create a Gmail App Password

WhatsApp Image 2024-12-07 at 08.35.04_e063a3e2.jpg
WhatsApp Image 2024-12-07 at 08.35.30_2f54a8d7.jpg
WhatsApp Image 2024-12-07 at 08.35.36_b2bbd112.jpg
  1. Log in to Your Google Account:
  2. Go to Google Account Settings.
  3. Use your Gmail credentials to log in.
  4. Enable Two-Factor Authentication (2FA):
  5. In the account settings, navigate to "Security".
  6. Under "Signing in to Google", enable 2-Step Verification.
  7. Follow the prompts to set up 2FA using your phone or other methods.
  8. Access the App Password Section:
  9. After enabling 2FA, return to the "Security" section.
  10. Scroll down to "Signing in to Google" and click "App Passwords".
  11. Generate an App Password:
  12. Choose an app and device from the dropdown menus.
  13. For example, select "Other", and name it something meaningful, like "ESP32 Project."
  14. Click "Generate" to create a 16-character password.
  15. Copy the Password:
  16. A yellow box will display the App Password.
  17. Copy this password and use it in your ESP32 project code where sender_password is required.
  18. Secure the Password:
  19. Store it in a safe place. Do not share it or expose it publicly.


Code Implementation:

  1. Wi-Fi Setup:
  2. Replace WIFI_SSID and WIFI_PASSWORD with your Wi-Fi credentials to connect the ESP32 to the internet.
  3. SMTP Server Setup:
  4. The SMTP server for Gmail (smtp.gmail.com) and port (465) are configured.
  5. Provide valid Gmail credentials (sender_email and sender_password which you got on previous step).
  6. Email Configuration:
  7. The email message is customized to alert about the fire.

Upload Code:

  1. Upload the code to the ESP32 using the Arduino IDE.


#include <WiFi.h>
#include <ESP_Mail_Client.h>

// WiFi configuration
#define WIFI_SSID "YUVARAJSWIFI"
#define WIFI_PASSWORD "12345678"

// SMTP server configuration
#define SMTP_server "smtp.gmail.com"
#define SMTP_Port 465

// Sender email credentials
#define sender_email "yuvrajfortestandrewards@gmail.com"
#define sender_password "yyygwljiplvytunh"

// Recipient email address
#define Recipient_email "yuvrajkaniyar@gmail.com"
#define Recipient_name "Yuvraj"

// Create an SMTPSession instance
SMTPSession smtp;

// Flame sensor pin
#define FLAME_SENSOR_PIN 5

// Threshold for fire detection
#define FIRE_DETECTED LOW

void setup() {
 Serial.begin(115200);
 Serial.println();

 // Initialize flame sensor pin
 pinMode(FLAME_SENSOR_PIN, INPUT);

 // Connect to WiFi
 Serial.print("Connecting to WiFi...");
 WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
 while (WiFi.status() != WL_CONNECTED) {
  Serial.print(".");
  delay(200);
 }
 Serial.println("\nWiFi connected.");
 Serial.print("IP address: ");
 Serial.println(WiFi.localIP());
 Serial.println();

 // Enable debug messages for SMTP
 smtp.debug(1);
}

void loop() {
 // Check flame sensor status
 if (digitalRead(FLAME_SENSOR_PIN) == FIRE_DETECTED) {
  Serial.println("Fire detected! Sending alert email...");
  sendFireAlertEmail();
  delay(30000); // Wait for 30 seconds before checking again
 }
}

void sendFireAlertEmail() {
 // Configure SMTP session
 ESP_Mail_Session session;
 session.server.host_name = SMTP_server;
 session.server.port = SMTP_Port;
 session.login.email = sender_email;
 session.login.password = sender_password;
 session.login.user_domain = "";

 // Create an email message
 SMTP_Message message;
 message.sender.name = "Fire Alert System";
 message.sender.email = sender_email;
 message.subject = "Fire Alert! Immediate Attention Required";
 message.addRecipient(Recipient_name, Recipient_email);

 // Email body
 String htmlMsg = "<div style=\"color:#ff0000;\"><h1>Fire Alert!</h1><p>A fire has been detected by the ESP32 system. Immediate action is required.</p></div>";
 message.html.content = htmlMsg.c_str();
 message.html.charSet = "us-ascii";
 message.html.transfer_encoding = Content_Transfer_Encoding::enc_7bit;

 // Connect to SMTP server and send email
 if (!smtp.connect(&session)) {
  Serial.println("Error connecting to SMTP server: " + smtp.errorReason());
  return;
 }

 if (!MailClient.sendMail(&smtp, &message)) {
  Serial.println("Error sending Email: " + smtp.errorReason());
 } else {
  Serial.println("Fire alert email sent successfully!");
 }

 // Close the SMTP session
 smtp.closeSession();
}

Testing the System

WhatsApp Image 2024-12-07 at 09.17.45_a62d2992.jpg
WhatsApp Image 2024-12-07 at 09.18.19_0814ca3a.jpg
WhatsApp Image 2024-12-07 at 09.18.47_2b93ff86.jpg
  1. Power up the ESP32.
  2. Ensure it connects to the Wi-Fi network.
  3. Simulate a flame in front of the flame sensor.
  4. The ESP32 will detect the fire and send an email alert to the recipient.


Code Explanation

  1. Wi-Fi Initialization:
  2. Connects the ESP32 to the internet.
  3. Flame Sensor Input:
  4. Monitors the digital output of the flame sensor.
  5. If the signal is LOW, fire is detected.
  6. Email Alert:
  7. Creates an SMTP session and sends an email with a pre-defined subject and message body.
  8. Uses secure login credentials to authenticate and send emails.