• Ankita Sharma

SMS Alert for Fire using Raspberry Pi - IoT

Updated: Dec 16, 2020

A fire outbreak is a major tragedy that has led to various casualties returning into both financial loss as well as human life. Detecting fire at an early stage can help in saving lives. That is why Fire detectors play a very important role in many organizations, industries, hospitals, institutions, etc. A Fire detector not only detects fire/flame but also alerts the user whenever a fire is detected so that necessary measures can be taken.


In this article, we will learn how to interface Fire Sensor with the Raspberry Pi board to get SMS alerts when a flame/fire is detected.


Hardware Requirements :


The following components are required for the interfacing -

Components Required
Components Required

Raspberry Pi 3B+:


Raspberry Pi 3B+
Raspberry Pi 3B+

Raspberry Pi ( Rpi ), is a low-cost, credit card-sized computer that runs on the Linux Operating System. It provides a set of #GPIO ( General Purpose Input / Output Pins ) which allows the interfacing of hardware components. It has inbuilt WiFi and Bluetooth capabilities making it a better candidate for #IoT projects and Developments.


Buy RaspberryPi 3B+ board from here.


Fire Sensor :


Fire Sensor
Fire Sensor

Fire #Sensors, also known as Flame Sensors are designed to detect the presence of fire/flames. They consist of four pins - Ao ( Analog Output ), Do ( Digital Output ), GND ( Ground ), and Vcc ( Power Supply ). For Analog Pin ( Ao ), an ADC ( Analog-to-Digital Converter ) needs to be used for getting the desired output while the Digital Output Pin ( Do ) directly gives the output in terms of 0 and 1.


Buy Fire Sensor from here.


Jumper Wires :


Jumper Wires
Jumper Wires

Jumper Wires are connecting wires having connector pins at each end, allowing them to connect two points without soldering. They typically come in three versions - male-to-male, male-to-female, and female-to-female. Here, we will be using female-to-female #Jumper Wires for interfacing the sensor with a Raspberry Pi board.


Buy Jumper Wires from here.


USB Cable :

USB Cable
USB Cable

#USB ( Universal Serial Bus ) Cable is used to connect Raspberry Pi to the system ( For eg: Laptops ).


Buy USB Cable from here.


Micro SD card:

16 GB Micro SD card
16 GB Micro SD card

Micro #SD card acts as a hard drive for Raspberry Pi and stores information. SD is an abbreviation for Secure Digital. The faster you read data from the micro SD card, the faster will be the booting and loading of programs to Raspberry Pi. That is why it is always recommended to use Class 10 micro SD cards.


Buy 16 GB Micro SD cards from here.


Software Requirements :


Raspberry Pi Imager:

Raspberry Pi Imager
Raspberry Pi Imager

Raspberry Pi imager is a quick and easy to install RPi Operating System which allows the interfacing of hardware devices with Softwares. It needs to be installed into the system for making the #Raspberry Pi board recognizable. It can be installed with the help of an SD Card Reader.


Python IDLE:

Python IDLE IDE
Python IDLE IDE

Python #IDLE ( Integrated Development Learning Environment ) is an integrated development environment for #Python, used to execute Python scripts.


Sending and Receiving SMS with Nexmo:


Nexmo uses API's allowing the user to send and receive SMS worldwide. The following steps are needed for setting up nexmo to get SMS alerts -

  1. Open the nexmo website and then click on sign up.

  2. Submit the required information for sign-up, a code will be sent to the mobile number you entered.

  3. After signing up, You will see the #API key and API secret, note them as you need to enter the API information into the code.

Click here to visit the website: Nexmo


Installations:


1. For installing RPi imager, type the given-below command in your terminal window.

sudo apt install rpi-imager 

2. For installing Python IDLE, type the given command in your terminal window.

sudo apt-get install idle3 

3. For installing #Nexmo Python Library, type the below command in the terminal.

pip install nexmo 

Circuit Connections:


The circuit connections for interfacing Flame Sensor with Raspberry Pi are as follows -

Circuit Connections
Circuit Connections
  1. GND pin of flame sensor is connected to GND pin of Raspberry Pi.

  2. The Vcc pin of the flame sensor is connected to +5V of Raspberry Pi.

  3. The D0 pin ( Digital Output ) of the flame sensor is connected to #GPIO 4 of the Raspberry Pi.


Code Explanation :


The code explanation for this design are as follows -

import nexmo

Firstly, importing the nexmo module to establish a connection over the nexmo server to get SMS alerts.

import time

Now, importing time module to introduce a time delay in our program

import RPi.GPIO as GPIO

Importing RPi.GPIO to make our Raspberry Pi recognizable for interfacing. It is basically used to declare input and output pins.

channel=4

The above command line assigns PIN to the channel variable.

GPIO.setmode(GPIO.BCM)

The above command line sets GPIO numbering to transfer data. For GPIO numbering, choose BCM.

GPIO.setup(channel,GPIO.IN)

The above command line assigns the channel ( i.e. GPIO 4 ) as an input pin.

client = nexmo.Client(key='xxxxxxxx', secret='xxxxxxxxx')

The above command line initializes client connection to the Nexmo server. API key and secret needs to be passed as a parameter to nexmo.Client function.

def send_sms(channel): #the texting portion
    print "Sending text"
    client.send_message({
        'from': 'Raspberry Pi',
        'to': 'Your_Mobile_Number',
        'text': 'Fire at your place !!',
    })
    print "Text sent"

The above command line defines a function send_sms. Here, the channel is passed as a parameter to this function. The client.send_message function is responsible to send SMS alerts to your mobile phones.

GPIO.add_event_detect(channel, GPIO.BOTH, bouncetime=30)  # let us know when the pin goes HIGH or LOW

The above command line tells us when the pin goes HIGH or LOW.

GPIO.add_event_callback(channel, send_sms)  # assign function to GPIO PIN, Run function on change

The above command line calls the send_sms function based on the values detected on the channel.

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

An infinite loop will be executed until we want the sensor to be working.


Complete Code :


import nexmo
import time
import RPi.GPIO as GPIO

channel=4
GPIO.setmode(GPIO.BCM)
GPIO.setup(channel,GPIO.IN)

client = nexmo.Client(key='xxxxxxxx', secret='xxxxxxxxx')

def send_sms(channel): #the texting portion
    print "Sending text"
    client.send_message({
        'from': 'Raspberry Pi',
        'to': 'Your_Mobile_Number',
        'text': 'Fire at your place !!',
    })
    print "Text sent"

GPIO.add_event_detect(channel, GPIO.BOTH, bouncetime=30)  # let us know when the pin goes HIGH or LOW
GPIO.add_event_callback(channel, send_sms)  # assign function to GPIO PIN, Run function on change

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

To get complete code for this interfacing, download the document given below :

fire_sms (Main)
.txt
TXT • 681B

Note:

  1. Please change the file extension to .py(python) file format before you upload the code to Raspberry Pi.

  2. Do not forget to change nexmo.Client( ) parameter with your API information.

  3. Mention your registered mobile number in the code under the send_sms function.


Working :


Once the hardware connections are completed and the code is written in the #IDE, connect the hardware device to the system ( Laptop ) and upload the code to the hardware.


SSH Terminal
SSH Terminal

After saving the code file under the above-mentioned extension ( i.e .py ), open the SSH (Secure Socket Shell ) terminal.


Running code file on SSH terminal
Running code file on SSH terminal

Now, RUN the code file by writing python .py on the SSH terminal window as shown in the above image.


Fire detector using Raspberry Pi
Fire detector using Raspberry Pi
Getting SMS Alerts
Getting SMS Alerts

Whenever a #fire/flame is detected, the Do ( Digital Output ) pin of the #flame sensor lights up, and an SMS is sent to the registered mobile number.



Do watch the video given below to get a better understanding through the practical explanation.


Video By - Sai Parimal


Conclusion :


This article gives a brief overview of #interfacing flame sensor on Raspberry Pi and getting #SMS alerts whenever a flame/fire is detected so that necessary measures can be taken. While sensors do the task of detecting fire/flame, the combination of the Internet here makes the tasks a lot more beneficial. This is just a #prototype with huge applications in the home

the automation system, smoke detection systems, industries, etc.

See Also :