Capstone Project
Temperature detector : it gives you an alert whenever the temperature inside a fridge rises above 20 degrees (approx).
Components required: a power source, Bolt WiFi module and an LM35.
Build the setup using the above parts. Make it work using the codes provide below and obtain a graph.
For the code, use temp as the variable name and write the following code to get a graph:
setChartLibrary('google-chart');
setChartType('lineGraph');
plotChart("time_stamp", "temp");
Now, install the system in the fridge:
From there, observe the graph for limits of the alert system. Here is my graph:
This graph shows that after 1 hr, the door was opened once.
This graph shows that after 2 hours, the door was opened twice.
From the graphs, the limits I put was 50 and 200.
Below is the Python code for receiving an alert message on your phone or via email:
import email_conf
from boltiot import Email, Bolt
import json, time
minimum_limit= 300 #the minimum threshold of light value
maximum_limit= 600 #the maximum threshold of light value
mybolt= Bolt(email_conf.API_KEY, email_conf.DEVICE_ID)
mailer= Email(email_conf.MAILGUN_API_KEY, email_conf.SANDBOX_URL, email_conf.SENDER_EMAIL, email_conf.RECIPIENT_EMAIL)
while True:
print ("Reading sensor value")
response = mybolt.analogRead('A0')
data = json.loads(response)
print ("Sensor value is: " + str(data['value']))
try:
sensor_value = int(data['value'])
if sensor_value > maximum_limit or sensor_value <minimum_limit:
print("Making request to Mailgun to send an email")
response =mailer.send_email("Alert", "The Current temperature sensor valueis " +str(sensor_value))
response_text = json.loads(response.text)
print("Response received from Mailgun is: " + str(response_text['message']))
except Exception as e:
print ("Error occured: Below are the details")
print (e)
time.sleep(10)
now, forthe z score analysis the python code will be
Thus, the capstone project is complete with all the objectives.import conf, json, time, math, statisticsfrom boltiot import Sms, Boltdef 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]mybolt = Bolt(conf.API_KEY, conf.DEVICE_ID)sms = Sms(conf.SSID, conf.AUTH_TOKEN, conf.TO_NUMBER, conf.FROM_NUMBER)history_data=[]while True: response = mybolt.analogRead('A0') data = json.loads(response) if data['success'] != 1: print("There was an error while retriving the data.") print("This is the error:"+data['value']) time.sleep(10) continue print ("This is the value "+data['value']) sensor_value=0 try: sensor_value = int(data['value']) except e: print("There was an error while parsing the response: ",e) continue bound = compute_bounds(history_data,conf.FRAME_SIZE,conf.MUL_FACTOR) if not bound: required_data_count=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 ("The light level increased suddenly. Sending an SMS.") response = sms.send_sms("Someone turned on the lights") print("This is the response ",response) elif sensor_value < bound[1]: print ("The light level decreased suddenly. Sending an SMS.") response = sms.send_sms("Someone turned off the lights") print("This is the response ",response) history_data.append(sensor_value); except Exception as e: print ("Error",e) time.sleep(10)






Comments