Rishab Baliga
Published

Smart Refrigerator

This system is to get an alert when the refrigerator or freezer door is opened.

IntermediateFull instructions provided410
Smart Refrigerator

Things used in this project

Hardware components

Bolt WiFi Module
Bolt IoT Bolt WiFi Module
×1
LM35 IC
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Solderless Breadboard Half Size
Solderless Breadboard Half Size
×1

Software apps and online services

ubuntu server
Bolt Cloud
Bolt IoT Bolt Cloud
SMS Messaging API
Twilio SMS Messaging API
Mailgun

Story

Read more

Schematics

Circuit Diagram

Code

temp_sensor.py

Python
import conf, json, time, math, statistics
from boltiot import Sms, Bolt, Email

def compute_bounds(history,frame_size,factor):
   if len(history)<frame_size:
       return None
   if len(history)>frame_size:
       del history[0:len(history)-frame_size]

   Mean=statistics.mean(history)
   Variance=0
   
   for data in history:
       Variance += math.pow((data-Mean),2)
   Zn = factor * math.sqrt(Variance/frame_size)

   High_bound = history[frame_size-1]+Zn
   Low_bound = history[frame_size-1]-Zn
   return [High_bound,Low_bound]
  
mybolt = Bolt(conf.API_KEY, conf.DEVICE_ID)
email = Email(conf.MAILGUN_API_KEY, conf.SANDBOX_URL, conf.SENDER_EMAIL, conf.RECIPIENT_EMAIL)
sms = Sms(conf.SSID, conf.AUTH_TOKEN, conf.TO_NUMBER, conf.FROM_NUMBER)
history=[]
  
while True:
   response = mybolt.analogRead('A0')
   data = json.loads(response)
   if data['success'] != 1:
       print("Error retriving data.")
       print(data['value'])
       time.sleep(5)
       continue
   sensor_value1 = int(data['value'])
   sensor_value1 = sensor_value1/10.24
   
   print ("The current recorded temperature: "+ str(sensor_value1)+" degree celsious")
   print("Sensor reading: "+data['value'])
   
   sensor_value=0
   
   try:
       sensor_value = int(data['value'])
   except e:
       print("Error parsing response: ",e)
       continue
   bound = compute_bounds(history,conf.FRAME_SIZE,conf.MUL_FACTOR)
   
   if not bound:
       required_data_count=conf.FRAME_SIZE-len(history)
       print("Insufficient data. "+str(required_data_count)+" more data points needed")
       history.append(int(data['value']))
       time.sleep(5)
       continue
   try:
       if sensor_value>bound[0] :
           sensor_value1 = sensor_value/10.24
           print ("Temparature increased suddenly. Sending email now")
           response = sms.send_sms("Anamoly Detected", "Someone Opened the refrigerator door. The present temperature: " +str(sensor_value1)+ " degree Celsius")
           response = email.send_sms("Anamoly Detected", "Someone Opened the refrigerator door. The present temperature: " +str(sensor_value1)+ " degree Celsius")
       history.append(sensor_value);
   
   except Exception as e:
       print ("Error",e)
  
   time.sleep(5)

conf.py

Python
MAILGUN_API_KEY = 'Mailgun private API'
SANDBOX_URL = 'sandbox url'
SENDER_EMAIL = 'test@sandbox url'
RECIPIENT_EMAIL = 'email id'
SID = 'You can find SID in your Twilio Dashboard' 
AUTH_TOKEN = 'You can find  on your Twilio Dashboard' 
FROM_NUMBER = 'This is the no. generated by Twilio. You can find this on your Twilio Dashboard'
TO_NUMBER = 'This is your number. Make sure you are adding +91 in beginning'
API_KEY = 'Bolt API key'
DEVICE_ID = 'BOLT Device ID'
FRAME_SIZE = 3
MUL_FACTOR = 6

Credits

Rishab Baliga

Rishab Baliga

1 project • 1 follower

Comments