To create a device that helps to achieve the following:
- While the manufacturer is allowed to maintain the temperature of the tablets between, -40 and -30 degrees Celsius, the temperature of the tablets should never remain between -33 and -30 degrees for longer than 20 minutes at a time.
- Also, the manufacturer should maintain a log of when the cooling chamber for the production of the tablets is opened.
The objectives of the Capstone project are as follows.
A. Build the circuit for temperature monitoring system, using the Bolt and LM35 sensor.
- VCC pin of the LM35 connects to 5v of the Bolt Wifi module. (White wire)
- Output pin of the LM35 connects to A0 (Analog input pin) of the Bolt Wifi module.(Grey wire)
- GNd pin of the LM35 connects to the Gnd. (Purple wire)
B. Create a product on the Bolt Cloud, to monitor the data from the LM35, and link it to your Bolt.
C. Write the product code, required to run the polynomial regression algorithm on the data sent by the Bolt.
With this objective in mind, Mr. Nigel managed to satisfy the first condition set by the Government. Using the prediction data, he was able to take early action, whenever the graph predicted that the temperature would be maintained within the -33 and -30 degrees Celsius range for longer than 20 minutes.
Code :
setChartLibrary('google-chart');
setChartTitle('Polynomial Regression');
setChartType('predictionGraph');
setAxisName('time_stamp','temp');
mul(0.0977);
plotChart('time_stamp','temp');
D. Keep the temperature monitoring circuit inside your fridge with the door of the fridge closed, and let the system record the temperature readings for about 2 hours.
E. Using the reading that you received in the 2 hours, set boundaries for the temperature within the fridge.
F. Write a python code which will fetch the temperature data, every 10 seconds, and send out an email alert, if the temperature goes beyond the temperature thresholds you decided on in objective "E".
Open ubuntu server.
Create a file to store credentials:
sudo nano email_conf.py
Enter the below code.
MAILGUN_API_KEY = 'This is the private API key which you can find on your Mailgun Dashboard'
SANDBOX_URL= 'You can find this on your Mailgun Dashboard'
SENDER_EMAIL = 'This would be test@your SANDBOX_URL'
RECIPIENT_EMAIL = 'Enter your Email ID Here'
API_KEY = 'This is your Bolt Cloud account API key'
DEVICE_ID = 'This is the ID of your Bolt device'
FRAME_SIZE = 10
MUL_FACTOR = 6
Create the main code file:
sudo nano capstone_project.py
Enter the below code.
import email_conf, json, time, math, statistics
from boltiot import Email, Bolt
max_limit = 52
min_limit = -52
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'])
if sensor_value > max_limit or sensor_value < min_limit:
print("Making request to Mailgun to send an email")
temperature = (100*sensor_value)/1024
response = mailer.send_email("Alert!!", "The temperature of the refrigerator is " +str(temperature))
response_text = json.loads(response.text)
print("Response received from Mailgun is: " + str(response_text['message']))
except e:
print("There was an error while parsing the response: ",e)
continue
G. Modify the python code, to also do a Z-score analysis and print the line “Someone has opened the fridge door” when an anomaly is detected.
H. Tune the Z-score analysis code, such that, it detects an anomaly when someone opens the door of the fridge.
Final Code:
import email_conf, json, time, math, statistics
from boltiot import Email, Bolt
max_limit = 52
min_limit = -52
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]
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)
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'])
if sensor_value > max_limit or sensor_value < min_limit:
print("Making request to Mailgun to send an email")
temperature = (100*sensor_value)/1024
response = mailer.send_email("Alert!!", "The temperature of the refrigerator is " +str(temperature))
response_text = json.loads(response.text)
print("Response received from Mailgun is: " + str(response_text['message']))
except e:
print("There was an error while parsing the response: ",e)
continue
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] or sensor_value < bound[1]:
print ("Someone has opened the refrigerator door.")
history_data.append(sensor_value);
except Exception as e:
print ("Error",e)
time.sleep(10)
Output:To run the code:
sudo python3 capstone_project.py
Comments
Please log in or sign up to comment.