The main functionality of this project is monitoring the temperature of the surroundings and performing Z score analysis to detect anomalies. A LM35 temperature sensor is connected to the analog pin of the bolt module that monitors the temperature. By conducting Z score analysis, this device can detect sudden increase in temperature and alert the user using an sms using Twilio api.
2. CREATING THE PROJECT2.1. MAKING CIRCUIT CONNECTIONS
A LM35 temperature sensor is connected to the bolt module. The output pin is connected to the analog pin (A0) of the module. Also two LED's and buzzer connected to the Bolt Wifi Module. One LED indicates that the device is on and its anode is connected to pin 1 of the module and the other LED's anode is connected along with the buzzer 's anode to pin 0 of the module. When an anomaly is detected, the pin 0 will be set to HIGH so that both the LED and the buzzer is switched on, raising an alarm.
2.2. USING TWILIO
- Visit twilio.com and create a free trial account.
- Verify your email id and your phone number.
- Go to the dashboard and click Get a Trial Number.
- Now you can find that Twilio would have assigned you a number.
2.3. CODING
This project is completely based on Python and you need to install the boltiot library first. It is recommended to carry out this project in an ubuntu server. To install boltiot library enter,
pip install boltiot
The code is divided into two parts - conf.py and fire_alarm.py.
2.3.1. CONFIGURATION FILE
API_KEY = "YOUR_API_KEY"
DEVICE_ID = "BOLTXXXXXXX"
SID = "FOUND_IN_TWILIO_ACCOUNT"
AUTH_TOKEN = "FOUND_IN_TWILIO_ACCOUNT"
FROM_NUM = "FOUND_IN_TWILIO_ACCOUNT"
TO_NUM = "YOUR_NUMBER"
Modify the above code as per your device. The API KEY is found in your Bolt Cloud under the API section.
The device id is found in the devices section in the bolt cloud. The SID and the AUTH_TOKEN are found in the twilio dashboard. The FROM_NO is the twilio trial number found in the dashboard and the TO_NUM is your number to which sms will be sent.
2.3.2. MAIN SOURCE CODE
from boltiot import Bolt , Sms
import time , conf , json , sys , statistics
r = 5
c = 5
data_set = []
alarm_state = 0
mybolt = Bolt(conf.API_KEY,conf.DEVICE_ID)
sms = Sms(conf.SID,conf.AUTH_TOKEN,conf.TO_NUM,conf.FROM_NUM)
def check_device():
data = json.loads(mybolt.isOnline())
if data["value"]=="online":
print('Device online')
mybolt.digitalWrite("1","HIGH")
else:
print('Device offline.')
sys.exit()
def get_temp():
data = json.loads(mybolt.analogRead("A0"))
if data["success"]==1:
val =float(data["value"])
temp = 100 * val / 1024
return temp
else:
return -999
def set_limits(data_set,r,c):
if len(data_set)<r:
return None
if len(data_set)>r:
data_set=data_set[len(data_set)-r:]
Mn = statistics.mean(data_set)
Variance = 0
for data in data_set:
Variance += (data - Mn) ** 2
Zn = c * ((Variance / r) ** 0.5)
H = data_set[r-1] + Zn
return H
def run_alarm(val):
print('Risk of a possible firm.Raising alarm.')
sms.send_sms('Sudden raise in temperature.Can be possibly a fire.The temperature is '+str(val))
mybolt.digitalWrite('0','HIGH')
global alarm_state
alarm_state = 1
def stop_alarm(val):
mybolt.digitalWrite('0','LOW')
check_device()
while 1:
try:
temp = get_temp()
print('The temperature is ',temp)
limit=set_limits(data_set,r,c)
if not limit:
print('Not enough value to conduct Z-score analysis.Need more values.')
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:
print('Device Stopped.')
mybolt.digitalWrite('1','LOW')
mybolt.digitalWrite('0','LOW')
sys.exit()
In the above code, the variable r refers to the data frame and c refers to the multiplication factor required for calculating the Z-score. Based on surroundings, the value must be adjusted for accurate results.
The function check_device() is used to check whether the device is connected to the bolt cloud. The get_temp() function returns the temperature of the surroundings. The sensor value read from the sensor is converted into the corresponding temperature using the formula temp = (val* 100)/1024.
The set_limits(data_set, r, c) function is used to calculate the Z-score. When an anomaly is detected, an sms is sent using the send_sms function called in he run_alarm() function defined in the boltiot library. Consequently bolt api requests are sent and the warning led and buzzer are set off.
The temperature is checked once in every 10 seconds using an infinite while loop and Ctrl + C is used to stop the code execution.
3. DEMONSTRATION
Comments