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)#importing bolt api key and devide id from conf.py
sms = Sms(conf.SSID,conf.AUTH_TOKEN,conf.TO_NUM,conf.FROM_NUM)# importing twillo ssid , auth ,and numbers
def check_device(): # function for checking the 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():# function for getting temp values
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):# function for z-score
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): #function for buzzor and sms
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: # 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 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: # press ctrlc + c for stopping the loop
print('Device Stopped.')
mybolt.digitalWrite('1','LOW')
mybolt.digitalWrite('0','LOW')
sys.exit()
Comments