Como construir um alarme de incêndio com alertas SMS e WhatsApp

How to build a fire alarm with SMS and WhatsApp alerts

Fire alarms are essential in homes and buildings, mitigating fire risks and ensuring safety. These alarms aim to safeguard lives and property by alerting and initiating emergency procedures.

In this project we will build a fire alarm system using ESP32, a flame sensor and an MQ2 gas sensor. This device will sound a buzzer and flash an LED if it detects smoke or any sign of fire. It will immediately send an SMS and WhatsApp notification to the user.

Required components

  1. ESP32 x1
  2. Flame sensor x1
  3. Gas sensor MQ-2 x1
  4. Doorbell x1
  5. LEDx1
  6. 330Ω x2 resistors
  7. Push Button x1

Circuit Connections
To build this fire alarm device, we will interface the flame sensor and MQ2 gas sensor with ESP32. The flame sensor detects smoke and fire and the MQ2 gas sensor detects flammable gases. We will use DOIT ESP32 DevKit V1 as ESP32.

Let's start.

  • To interface the flame sensor, connect its output pin to GPIO36 of ESP32.
  • To interface the MQ2 gas sensor, connect its digital output pin to GPIO39 of the ESP32.
  • Then, connect the power and ground pins of both sensors to the 3V3 and GND pins of the ESP32, respectively.
  • Note: the analog output pin of the MQ2 sensor remains disconnected.
  • Connect the buzzer to GPIO23 of the ESP32.
  • Connect the anode of the LED to the GPIO22 of the ESP32 and the cathode to the 3.3V of the ESP32 through the series resistor.
  • Connect a button to GPIO21 of the ESP32.

Since the ESP32 has built-in WiFi functionality, there is no need to connect any Internet-enabling components.

Here is the circuit diagram of ESP32 based fire alarm and warning system.

Whatabot API
We will use the Whatabot API to send a fire risk warning via WhatsApp. Check out this project to learn how to use the Whatabot API with ESP32.

Twilio API
We will use the Twilio API to send an SMS alert about fire risk. Check out this project to learn how to use the Twilio API with ESP32.

Arduino Sketch

How it works
This alarm uses a flame sensor to detect fire and an MQ2 gas sensor to detect flammable gases. The gas sensor must be calibrated by adjusting the potentiometer on its distribution board. This ensures that a signal is emitted as soon as it detects a specific concentration of flammable gases.

Both sensors have a digital output and output a LOW signal if fire or gas is detected. The MQ2 gas sensor also has an analog output, but it is not used for this project.

When the fire alarm is turned on, it reads signals from the flame and smoke sensors. If the output from either sensor reads LOW, the fire alarm will be triggered. The buzzer is on and the LED flashes. SMS and WhatsApp notifications about fire risk are sent to the user. The buzzer and LED remain on until the button is pressed to turn off the alarm.

The code
The outline begins by importing the WiFi.h, HTTPClient.h, UrlEncode.h, and twilio.hpp libraries.

  • The WiFi.h library is required to connect to the WiFi hotspot.
  • HTTPClient.h is required to process the HTTP GET request.
  • UrlEncode.h is required to encode the URL according to web standards.
  • The twilio library provides access to the functions required to send SMS messages via HTTP requests.

Variables are declared to store the SSID and password of the WiFi hotspot. Variables are also declared to store the mobile number for WhatsApp and API keys (received when registering the mobile number with Whatabot).

To work with the Twilio API, variables must be declared for the account SID, authentication token, sender number (with country code), and recipient number (with country code). This app uses the same mobile number to send and receive an SMS alert.

Variables are then declared for the pin assignments of the flame and gas sensors, the buzzer, the LED, and the push button. A variable is also declared to store the alarm status.

In the setup function, the pins that connect the flame and gas sensors and the button are configured as digital inputs. The pins connecting the buzzer and LED are configured as digital outputs.

The buzzer and LED are turned off by sending a HIGH signal. The baud rate is set to 115,200 bps for sending debug messages to the console. ESP32 is connected to WiFi via user credentials.

In the loop function, the ESP32 reads the signals from the flame and smoke sensors. If a LOW reading is received from any of the sensors, the alarm will be raised via the raiseAlarm function. The user-defined function send_whatsapp_message controls notifications sent to the user's WhatsApp number via the Whatabot API. The user-defined function, send_sms_alert, controls SMS alerts sent to the user's mobile number via the Twilio API. These notifications are only sent if the fire alarm is triggered.

Results
Here are the console messages…

Here is a video demonstration of how the fire alarm works.

Back to blog

Leave a comment

Please note, comments need to be approved before they are published.