IoT Fire Alarm with ESP32

A dummies guide to making a cheap DIY but useful fire alarm under 6 USD. We use the popular development board ESP32 Dev as our microcontroller for this project. I like this board since it is versatile and cheap. With onboard WiFi and Bluetooth, it makes IoT projects a breeze.

Things you need for this project:

  1. ESP32 Dev Board (ESP32-WROOM-32)

  2. Micro USB Data cable

  3. Flame Sensor KY-026

  4. Buzzer 5mm

  5. DHT11 Temperature sensor

  6. Jumper Cables

  7. Blynk App (Free)


The first step would be to set up your board. First, ensure your micro USB cable is a data cable. Once plugged in, your board should light up. The next step is to set up your Arduino development software to the right board.

Linking to the right board. Look for your board's specific name from the tools menu from the toolbar. In our case; it is the ESP32 Dev Module.

Go to Tools > Board Manager > ESP32 Arduino > "ESP32 Dev Module"

Now we need to install the required libraries to run our project with Blynk. Click on Sketch from the toolbar and follow the steps below.

Go to Sketch > Include Library > 'Blynk'
Go to Sketch > Include Library > 'ESP32 Library'
//The second is only required if you are missing ESP32 libraries.
//To check, search ESP32 and see if installed is highlighted

With all our required libraries installed, the next step is to set up our blynk app. After signing in create your first project. A single project can host multiple sensors and functions however only one board. Blynk gives you the option to tie up multiple projects into a single app at a premium.

Add the following widgets 
An "LED" > Set it to Virtual Pin "V0" 
A "Push Notification" > Set it to @HIGH

After creating your project with its title, Blynk will send an Authentication Token, this token is very important in for your project to work. A token is issued for every project you create with Blynk.

Creating our program in Arduino software (aka Arduino IDE). Open a new file and save it. Delete the existing text in the file. After saving, connect your flame sensor to the appropriate pins. It should light up if correctly matched.

The first part is to make sure our board connects to our WiFi, take note of your WiFi name and password. Remember these are case sensitive. Connecting to the WiFi allows our ESP32 board to communicate with Blynk anywhere in the world to your smart phone. To communicate with Blynk we also require the authentication token. Replace the text inside the quotes with your information.

#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
BlynkTimer timer;

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "Insert authentication token here";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Insert Wifi name here";
char pass[] = "Insert password here";

The next part is to define the sensors, values and pins. replace the pins with the appropriate pin numbers in your setup. Delete the "Buzzer" if you are not using one. Where I have placed 00 replace it with your pin number. You can identify the pin on your board by the letters on it (D15 etc.)

#define BUZZER 00 // Use buzzer for alert 
#define FlamePin 00// This is for input pin
int Flame;
boolean state = false;
 
void setup() {
    // Debug console
 Serial.begin(115200);
 Blynk.begin(auth, ssid, pass);
 pinMode(FlamePin, INPUT);
 pinMode(BUZZER, OUTPUT);
 timer.setInterval(1000L, sendUptime);
 
}

Here we tell the microcontroller when to alert us, as well as to trigger the buzzer or any other sensor or peripheral you may have. It is important to remember to understand the limitation of your sensor. Our current sensor while sensitive can not cover large areas and has a 60 degree angle of perception.

void sendUptime() {
  
 Flame = digitalRead(FlamePin);
 if (Flame== LOW)
  {
    Blynk.notify("Alert Fire Detected");
    digitalWrite(BUZZER, HIGH);
     
    Serial.print("Fire Detected");
   
  }
 else
 {
 Serial.println("No flame");
 digitalWrite(BUZZER, LOW);
  }
}

Now the last part for us to ensure we loop the above parts.

void loop()
{
  Blynk.run();
  timer.run();
}

The final sketch should look something like below. You can copy the below code and paste it into your sketch. You can also add other sensors such as a DHT11 temperature sensor to monitor temperature levels and alert if it goes above a certain limit. While Blynk allows you to have push notifications you can add email addresses as well or share the project so a friend/family may use it too.

#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
BlynkTimer timer;

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "Insert authentication token here";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Insert Wifi name here";
char pass[] = "Insert password here";

#define BUZZER 00 // Use buzzer for alert 
#define FlamePin 00// This is for input pin
int Flame;
boolean state = false;
 
void setup() {
    // Debug console
 Serial.begin(115200);
 Blynk.begin(auth, ssid, pass);
 pinMode(FlamePin, INPUT);
 pinMode(BUZZER, OUTPUT);
 timer.setInterval(1000L, sendUptime);
 
}

void sendUptime() {
  
 Flame = digitalRead(FlamePin);
 if (Flame== LOW)
  {
    Blynk.notify("Alert Fire Detected");
    digitalWrite(BUZZER, HIGH);
     
    Serial.print("Fire Detected");
   
  }
 else
 {
 Serial.println("No flame");
 digitalWrite(BUZZER, LOW);
  }
}

void loop()
{
  Blynk.run();
  timer.run();
}
Fire Alarm IoT Project - Darknet

Write a comment ...

Write a comment ...