Alert Relatives to Save Your Property from Fire with Twilio and Raspberry Pi
Time to read: 4 minutes
When facing a danger like fire, everything can go wrong really fast, and you can quickly lose control.
In these situations, calling for help is crucial. A simple SMS can save your property.
We can automate it with Twilio Programmable SMS and Raspberry Pi.
In this article, you will learn how to build a system to alert your relatives by SMS if a fire occurs on your property.
What do you need?
Embedded electronics have evolved in these last few decades. Flames can now be detected with an electronic module. These modules can easily be connected to a Raspberry Pi which can be used like a small computer, and you can run programs written with high-level programming languages on it.
To build the fire detection system, you’ll need :
- 1 Raspberry Pi: A Raspberry Pi 3 Model B is used in this article, but you can use any version of the board.
- If you are using Raspberry Pi for the first time, you can learn how to set up your Raspberry Pi here.
- 1 IR (Infrared) Flame sensor module: This module detects flame and sends a signal to your Raspberry Pi.
- Several jumper wires to connect the sensor to the Raspberry Pi.
- 1 Twilio account: You should be able to send SMS. Here is a link to create one if you don’t have an account: Twilio | Try Twilio Free.
- The short Python script described in this article.
How does the system work?
An IR flame sensor module comes with a YG1006 phototransistor, a high-speed and high-sensitivity transistor. The module can detect a fire source or any bright light source.
This sensor detects IR (infrared) light wavelengths between 760 nm – 1100 nm that are emitted from the fire's flame or light source.
There are two versions of the module on the market: one with a DO (digital output) and one without. The version with DO is used in this article.
When a flame occurs near the sensor, a signal will be sent out on the DO pin. This is what your Raspberry Pi will catch, and you can react to this signal programmatically. A Python program running on the Raspberry Pi will listen to the signal, and you can do whatever you want to react to this. This article shows you how to send an SMS alert to your relatives with Twilio Programmable SMS.
Mounting the electronic schema
Now that you understand how the system works, you can mount the electronic schema.
For that, you’ll need jumper wires to connect the Raspberry Pi with the flame sensor.
From the sensor to the Raspberry Pi you have to join:
- + Sensor pin to the 5V pin
- G to the Ground pin
- DO to the GPIO21
Here is a diagram that shows the connections:
Once you have connected the flame sensor to your Raspberry Pi, it should look like this:
Write Python code on your Raspberry Pi
The hardware part of the system is ready now. It’s time to make the software part. Connect to your Raspberry Pi via SSH protocol or via a direct keyboard.
Create a new directory called fire-alert and change into the directory:
Then, inside the fire-alert directory, create and activate a new virtual environment:
Create a file with the name twilio_fire_alert.py and write the following code inside.
This is the Python script that will intercept the signal from the sensor on the GPIO 21 pin and send an SMS alert with Twilio Programmable SMS.
In this code, you have to change the value of some variables to customize them according to your Twilio account and relatives' numbers:
Variable names | Where to set | Usage |
---|---|---|
RELATIVE_NUMBERS | Inside the Python script | Should contain each phone number you want to receive SMS when a fire occurs. Phone numbers should be in E.164 format. |
TWILIO_PHONE_NUMBER | On your environment variable | Learn how to get a Twilio phone number from the Twilio Console here. |
TWILIO_ACCOUNT_SID | On your environment variable | SID on your Twilio account |
TWILIO_AUTH_TOKEN | On your environment variable | Auth Token from your Twilio account |
You can find your Twilio Account SID, Auth Token, and phone number in the Twilio Console:
You can set the environment variables by running the following commands, replacing the placeholder text (XXXXXXXXXX
) with your own values:
There are three main functions in the Python script: run()
, setting_up_gpio()
, and notify_my_relative()
.
run()
is the entry point of the script. It starts by setting up the GPIO on the Raspberry Pi by putting notify_my_relative()
as an event callback on the sensor channel.
So when a signal from the fire sensor is received on the GPIO 21, the notify_my_relative()
function is called. Then it will set up a Twilio client and send an SMS with it.
Run your Python code
To run the Python code, first you need to install the Twilio and RPi.GPIO dependencies in your Python environment.
This can be done with the pip package manager:
Then you can run the script with this command:
Testing
Before putting the system in your service, you can do a simple test to check if everything works as you expect.
Test the sensor
As your sensor is connected to the Raspberry Pi, take a lighter and light it near the sensor.
The sensor detects flame 80 cm around it at an angle of 0 to 60 degrees.
Before you light a flame, you should see one LED already turned on. You should see the second LED switch on when the flame from your lighter is the zone of detection.
Test the SMS notification
Open a terminal on your Raspberry Pi and run your Python script with this command, if it's not already running:
Now light the lighter next to your sensor. You should see the following message on your terminal: flame detected.
Check if SMS is received on your relatives' mobile phone (or, test it with your own mobile number as well).
Conclusion
Now that you have completed this tutorial, fire detection is something you can do by yourself.
A simple Raspberry Pi coupled with Twilio Programmable SMS can help you to send automatic notifications to your relatives when a fire is detected on your property. You can focus on getting out of the fire when your relatives are coming with additional help.
Put the system in a strategic place where fire can occur and save your property from the disaster.
About me: I’m a lead Python developer with 8 years of experience, passionate about electronics, IoT, and vintage mechanics. You can find me on Twitter or read my blog here: http://www.curiousmanjournal.ml/.
Related Posts
Related Resources
Twilio Docs
From APIs to SDKs to sample apps
API reference documentation, SDKs, helper libraries, quickstarts, and tutorials for your language and platform.
Resource Center
The latest ebooks, industry reports, and webinars
Learn from customer engagement experts to improve your own communication.
Ahoy
Twilio's developer community hub
Best practices, code samples, and inspiration to build communications and digital engagement experiences.