RITISH KUMAR DAS
Published © GPL3+

A Smart Fire Alarm System for Smart Handling of Fires

A smart fire alarm system that uses machine learning methods to detect fire possibilities and warns via both SMS & Buzzer Alarm. 1

BeginnerFull instructions provided2 hours585
A Smart Fire Alarm System for Smart Handling of Fires

Things used in this project

Hardware components

Male/Male Jumper Wires
Might need more or less according to the way the user implements the circuit.
×6
Male/Female Jumper Wires
Male/Female Jumper Wires
Might need more or less according to the way the user implements the circui
×2
LED (generic)
LED (generic)
One to show when the circuit is on. And the other turns on when the alarm is starts ringing
×2
Buzzer
Buzzer
Buzzer to turn on the alarm for audio warning.
×1
Through Hole Resistor, 330 kohm
Through Hole Resistor, 330 kohm
Resistors for the LED.
×2
Bolt WiFi Module
Bolt IoT Bolt WiFi Module
To implement the code onto the circuit and send/receive information to the cloud as per the code.
×1

Software apps and online services

SMS Messaging API
Twilio SMS Messaging API
Twilio App is used to send SMS alerts to the phone.
Jupyter Notebook
Jupyter Notebook
Platform used to implement the Python code.

Story

Read more

Schematics

Circuit Schematics

This is the basic schematic. Refer to this while building the circuit.

Code

Code For the Fire Alarm System

Python
Following is the code for the Fire Alarm System. Refer to the same to understand the code via the comment lines.
API_KEY = "USING THE API KEY OF YOUR DEVICE" #This is  the API key for the Bolt device used by me
DEVICE_ID = "BOLTXXXXXX" #This is the Bolt Device ID used by me
SID = "SID PROVIDED BY TWILIO" #Twilio SID signed up to send sms alerts
AUTH_TOKEN = "TOKEN PROVIDED BY TWILIO" #Twilio Authorised Token to verify the phone number
FROM_NUM = "FROM NUMBER PROVIDED BY THE TWILIO" #The Twilio Number from where the sms alert will be generated
TO_NUM = "THE NUMBER TO WHICH SMS ALERT IS SENT" #The personal number to receive the sms alert

from boltiot import Bolt , Sms
import time , json , sys , statistics
R = 5
C = 5
data_set = []
alarm_state = 0

mybolt = Bolt(API_KEY,DEVICE_ID) #importing bolt api key and devide id from conf.py
sms = Sms(SID,AUTH_TOKEN,TO_NUM,FROM_NUM)# importing twillo sid , auth ,and numbers

def check_device(): #function for checking the device 
        data = json.loads(mybolt.isOnline())
        if data["value"]=="online": #Checking whether the device is online or not
                print('Device online')
                mybolt.digitalWrite("1","HIGH") #Turning on the LED to indicate that the circuit is online
        else:
                print('Device offline.')
                sys.exit()

def get_temp(): #function for getting temp values
        data = json.loads(mybolt.analogRead("A0")) #Extracting the values from the sensor
        if data["success"]==1:
                val =float(data["value"]) #
                temp =  100 * val / 1024 #Converting the sensor values to Celsius
                return temp

        else:
                return -999

def set_limits(data_set,R,C): #function for performing z-score analysis
        if len(data_set)<R:
                return None
        if len(data_set)>R:
                data_set=data_set[len(data_set)-R:] #Storing the lsit of temperatures

        Mean = statistics.mean(data_set) #Calculating the mean of the data values
        Variance = 0
        for data in data_set:
                Variance += (data - Mean) ** 2 #Calculating the Variance 
        Zn = C * ((Variance / R) ** 0.5) #Calculating the Z score
        H = data_set[R-1] + Zn #Calculating the threshold temperature
        return H

def run_alarm(val): #function for buzzer and sms
        print('Fire possibility High. Turning on Buzzer.')
        sms.send_sms('Sudden raise in temperature. The temperature is '+str(val))
        mybolt.digitalWrite('0','HIGH')
        global alarm_state
        alarm_state = 1

def stop_alarm(val): #To stop the alarm after its stop ringing
                mybolt.digitalWrite('0','LOW')

check_device()

while 1: #indefinite loop for countinous tracking of temparature
        try:
                temp = get_temp()
                print('The temperature is ',temp)
                
                limit = set_limits(data_set,R,C)
                if not limit:
                        print('Not enough values to conduct Z-score analysis. More values required.')
                        data_set.append(temp)
                else:
                        if temp > limit:
                                run_alarm(temp)
                        else:
                                if alarm_state:
                                        stop_alarm(temp)
                                        alarm_state = 0
                        data_set.append(temp)

                time.sleep(10)

        except KeyboardInterrupt: #press ctrlc + c for stopping the loop
                print('Device Stopped.')
                mybolt.digitalWrite('1','LOW')
                mybolt.digitalWrite('0','LOW')
                sys.exit()

GitHub Link for the Code

Credits

RITISH KUMAR DAS

RITISH KUMAR DAS

1 project • 1 follower

Comments