import smtplib
import email_conf,json,time
from boltiot import Bolt
import math, statistics
minimum_limit = 50 #the minimum threshold of light value
maximum_limit = 100 #the maximum threshold of light value
mybolt = Bolt(email_conf.API_KEY, email_conf.DEVICE_ID)
def compute_bounds(history_data,frame_size,factor):
if len(history_data)<frame_size :
return None
if len(history_data)>frame_size :
del history_data[0:len(history_data)-frame_size]
Mn=statistics.mean(history_data)
Variance=0
for data in history_data :
Variance += math.pow((data-Mn),2)
Zn = factor * math.sqrt(Variance / frame_size)
High_bound = history_data[frame_size-1]+Zn
Low_bound = history_data[frame_size-1]-Zn
return [High_bound,Low_bound]
history_data=[]
while True:
print("Reading The Sensor Value: ")
response = mybolt.analogRead('A0')
data = json.loads(response)
print ("Sensor value is: " + str(data['value']))
try:
sensor_value = int(data['value'])
#Anomaly Detection
bound =
compute_bounds(history_data,email_conf.FRAME_SIZE,email_conf.MUL_FACTOR)
if not bound:
required_data_count=email_conf.FRAME_SIZE-len(history_data)
print("Not enough data to compute Z-score. Need
",required_data_count," more data points")
history_data.append(int(data['value']))
time.sleep(10)
continue
try:
if sensor_value > bound[0] :
print("bound[0] = ",bound[0])
print ("Someone has opened the fridge door")
print("This is the response ",response)
elif sensor_value < bound[1]:
print("bound[1] = ",bound[1])
print ("Someone has opened the fridge door")
print("This is the response ",response)
history_data.append(sensor_value);
except Exception as e:
print ("Error",e)
continue
#Email send if temperature crossed threshold
if sensor_value > maximum_limit or sensor_value < minimum_limit:
print("Temperature Crosses Threshold")
smtpserver=smtplib.SMTP("smtp.gmail.com",587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo()
smtpserver.login(email_conf.gmail_user,email_conf.gmail_pwd)
header='To:'+email_conf.to+'\n'+'From
'+email_conf.gmail_user+'\n'+'Subject:Alert\n'
msg=header+'Temperature is crossed Threshold'
smtpserver.sendmail(email_conf.gmail_user,email_conf.to,msg)
print('Email Send Successfully! :)')
smtpserver.close()
except Exception as e:
print ("Error occured: Below are the details")
print (e)
time.sleep(10)
Comments