Shraddha Chitte
Published © GPL3+

Capstone Project

Automatically warns when the refrigerator door is left open using Z-score analysis and anomaly detection.

AdvancedProtip4 hours531
Capstone Project

Things used in this project

Hardware components

Bolt WiFi Module
Bolt IoT Bolt WiFi Module
×1
Male/Female Jumper Wires
Male/Female Jumper Wires
×3
USB-A to Mini-USB Cable
USB-A to Mini-USB Cable
×1
LM35 Sensor
×1
Refrigerator
×1

Software apps and online services

Bolt Cloud
Bolt IoT Bolt Cloud
VmWare Workstation
Mailgun

Story

Read more

Schematics

Documentation

Connections and Set-Up

20190722_234531_qsgDCJydqL.jpg

20190722_234543_J3iVbZSmMs.jpg

Code

Prediction_Graph.js

JavaScript
setChartLibrary('google-chart');
setChartTitle('Polynomial Regression');
setChartType('predictionGraph');
setAxisName('time_stamp','temp');
mul(0.0977);
plotChart('time_stamp','temp');

email_conf.py

Python
API_KEY = "Your bolt API key"
DEVICE_ID = "Your bolt device id"
MAILGUN_API_KEY = "Your mailgun Api key"
SANDBOX_URL = "Your Sandbox Url"
SENDER_EMAIL = "Sender's Email"
RECIPIENT_EMAIL = "Recipient's Email"
FRAME_SIZE = "10"
MUL_FACTOR = "6"

anomaly_detection.py

Python
import email_conf
from boltiot import Email,Bolt
import json, time, math, statistics
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]
  
minimum_temp = 9
maximum_temp = 14

  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 retrieving 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,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 calculate Z-score.Need ",required_data_count," more data points.")
      history_data.append(int(data['value']))
      time.sleep(10)
      continue
      
    try:
      temp = sensor_value/10.24  
      if temp > maximum_temp :
        print ("Temperature increased suddenly.Sending an email.")
        print("Current temperature is : "+str(temp)+" Degrees Celsius")
        response = mailer.send_email("Alert","Someone opened the refrigerator door")
        print("Response: ",response)
      elif temp < minimum_temp :
        print ("Temperature decreased suddenly.Sending an email.")
        print("Current temperature is : "+str(temp)+" Degrees Celsius")
        response = mailer.send_email("Alert","Something wrong has happened.Check the refrigerator door.")
        print("Response: ",response)
      history_data.append(sensor_value);
    except Exception as e:
      print ("Error occured: ",e)
    time.sleep(10)
      

Credits

Shraddha Chitte

Shraddha Chitte

4 projects • 1 follower

Comments