Dular Nikode
Published

Capstone_project

It's the project for sending alert when temperature exceeds the threshold and also send the gmail when the door was opened.

BeginnerProtip2 hours419
Capstone_project

Things used in this project

Story

Read more

Schematics

Connecting LM35 sensor to BOLT

This sensor is used for temperature monitoring.
VCC pin of the LM35 connects to 5v of the Bolt Wifi module.
Output pin of the LM35 connects to A0 (Analog input pin) of the Bolt Wifi module.
Gnd pin of the LM35 connects to the Gnd.

Code

temp_alert

Python
Run it using command "python3 file_name"
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)

Credits

Dular Nikode

Dular Nikode

1 project • 0 followers

Comments