Alert Relatives to Save Your Property from Fire with Twilio and Raspberry Pi

August 18, 2022
Written by
Aina Rakotoson
Contributor
Opinions expressed by Twilio contributors are their own
Reviewed by
Mia Adjei
Twilion

Alert Relatives to Save Your Property from Fire with Twilio and Raspberry Pi

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 :

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.

IR flame sensor

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.

Twilio SMS should not be used to contact emergency services. 

If you require emergency assistance, please contact emergency services directly via another method.

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:

Connections between Raspberry Pi and IR flame sensor

To learn more about the Raspberry Pi's GPIO pins, take a look at the documentation here.

Once you have connected the flame sensor to your Raspberry Pi, it should look like this:

IR flame sensor connected to Raspberry Pi

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:

mkdir fire-alert
cd fire-alert

Then, inside the fire-alert directory, create and activate a new virtual environment:

python3 -m venv venv
source venv/bin/activate

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.

#!/usr/bin/python
import RPi.GPIO as GPIO
import time

import os
from twilio.rest import Client

#GPIO SETUP
FIRE_SENSOR_CHANNEL = 21

# each phone number of your relatives
RELATIVE_NUMBERS = []

TWILIO_PHONE_NUMBER = os.environ['TWILIO_PHONE_NUMBER']

def send_twilio_sms(client, numbers):
    print("flame detected")
    for number in numbers:
        message = client.messages.create(body="Help, my property catches fire",
                                         from_=TWILIO_PHONE_NUMBER,
                                         to=number)
        print(message.sid)


def create_twilio_client():
    """
      Find your Account SID and Auth Token at twilio.com/console
      and set the environment variables. See http://twil.io/secure
    """
    account_sid = os.environ['TWILIO_ACCOUNT_SID']
    auth_token = os.environ['TWILIO_AUTH_TOKEN']
    client = Client(account_sid, auth_token)
    return client


def notify_my_relative(channel):
    client = create_twilio_client()
    send_twilio_sms(client, RELATIVE_NUMBERS)


def setting_up_gpio():
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(FIRE_SENSOR_CHANNEL, GPIO.IN)
    GPIO.add_event_detect(
        FIRE_SENSOR_CHANNEL, GPIO.BOTH,
        bouncetime=300)  # let us know when the pin goes HIGH or LOW
    GPIO.add_event_callback(
        FIRE_SENSOR_CHANNEL,
        notify_my_relative)  # assign function to GPIO PIN, Run function on change


def run():
    setting_up_gpio()
    # infinite loop
    while True:
        time.sleep(1)


if __name__ == "__main__":
    run()

In this code, you have to change the value of some variables to customize them according to your Twilio account and relatives' numbers:

Variable namesWhere to setUsage
RELATIVE_NUMBERS Inside the Python scriptShould contain each phone number you want to receive SMS when a fire occurs. Phone numbers should be in E.164 format.
TWILIO_PHONE_NUMBEROn your environment variableLearn how to get a Twilio phone number from the Twilio Console here.
TWILIO_ACCOUNT_SIDOn your environment variableSID on your Twilio account
TWILIO_AUTH_TOKENOn your environment variableAuth Token from your Twilio account

You can find your Twilio Account SID, Auth Token, and phone number in the Twilio Console:

Twilio Console account info section

You can set the environment variables by running the following commands, replacing the placeholder text (XXXXXXXXXX) with your own values:

export TWILIO_ACCOUNT_SID='XXXXXXXXXX'
export TWILIO_AUTH_TOKEN='XXXXXXXXXX'
export TWILIO_AUTH_TOKEN='XXXXXXXXXX'
export TWILIO_PHONE_NUMBER='XXXXXXXXXX'

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.

Using a loop will work for you to send SMS to a few relatives. However, if you are looking to send messages at scale, consider a Messaging Service or using Twilio Notify.

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:

pip3 install twilio RPi.GPIO

Then you can run the script with this command:

python3 twilio_fire_alert.py

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.

Flame sensor LED lights up when the lighter's flame is near

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:

python3 twilio_fire_alert.py

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/.