This project helps to detect whether the refrigerator door has been opened by analysing the sudden rise in temperature. The project employs a LM35 temperature sensor the gives the analog value based on the temperature around it. The sudden change is detected using Z-score analysis and an alert system has been set up using Twilio-SMS messaging API to notify the operator.
It also focuses on logging of the temperature data on the Bolt Cloud.
2. Demonstration3.WorkingIn this section, we will learn about the functioning of this project.
- 3.1 Circuit connections for LM35 temperature sensor
Connect the "supply" pin of the sensor to the 5V output of the Bolt module, "ground" pin of the sensor to the "GND" port on the Bolt module and connect the "output" pin of the sensor to the 'A0' pin on the Bolt module. Also connect the the Bolt module to the usb-A to mini usb cable to connect to the power supply.
- 3.2 Configuration File
The python coding for this project has been done in Ubuntu (Linux). Before we start coding of the temperature sensor in python, we need to make a configuration file which will have the specific keys for each user/device. We will import this file in our main code and use the various attributes. The advantage of this is that each user will only have to change the contents the configuration file to use the product.
The following is the configuration file (named as conf.py):
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 = 'This is your Bolt Cloud accout API key'
DEVICE_ID = 'This is the ID of your Bolt device'
The API key and Device ID of the Bolt module can be determined as follows:
>>Connect your Bolt Device to the Bolt Cloud as per instructions given at https://cloud.boltiot.com/.
>>The following screen will appear after that.The Bolt Device ID is highlighted in yellow.
>>Go to the API section to know the API Key.
- 3.3 Manipulating LM35 output value
The analog output of the LM35 temperature sensor is not in standard temperature units. In order to obtain the correct value of the temperature, we divide the the output of the sensor by a factor of 10.24.
temp= sensor_val/10.24
- 3.4 Detection of Sudden Change in Light Intensity (Z-Score Analysis)
Z-score analysis is used for anomaly detection. Anomaly here means a variable's value (light intensity of the surroundings) going beyond a certain range of values. The range of values is called bounds (upper bound and lower bound). These bounds are calculated using the input values, frame size and multiplication factor. The frame size is the minimum number of input values needed for Z-score analysis and the multiplication factor determines the closeness of the bounds to the input values curve.
Given above is the formula to calculate the bounds. Here the input is represented as 'Vi', 'r' denotes the frame size and 'C' is the multiplication factor. Firstly we calculate the mean (Mn) of the input values (for every new input, the mean is calculated again). The variation of each input value (from the mean) is given as (Vi - Mn)^2. The Z-score (Zn) is calculated as shown above ( square root of the mean of the variation of each input value multiplied by the multiplication factor). The bounds are represented as 'Tn' and the upper bound is calculated as (Vi + Zn) and the lower bound is calculated as (Vi - Zn).
The frame size and multiplication factor are determined using trial-and-error method.
- 3.5 Creating Twilio account
Step 1: Open https://www.twilio.com/ in browser.
Step 2: Click on Get a Free API Key
button to sign up.
Step 3: Fill all the necessary details in SIGN UP form. Below is the screenshot of filled sign up form.
Step 4: To verify they will ask for your phone number. Choose India as an option in the dropdown and then enter your phone number.
Step 5:Select the 'programmable sms' option.
Step 6:You can view the Account SID and Auth token on this page. The Auth token is not visible by default, you can click on "view" button to make the Auth token visible as shown below. Copy both and save them somewhere securely.
Step 7:Click on Get a number
button.
Step 8:Then a popup will appear. Click on Choose this number
button.
Step 9: Then a popup will appear which will have the final number. Copy this number and save to notepad for future references.
- 3.6 Complete code
import conf, json, time, math, statistics
from boltiot import Sms, Bolt
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(conf.API_KEY, conf.DEVICE_ID)
sms = Sms(conf.SID, conf.AUTH_TOKEN, conf.TO_NUMBER, conf.FROM_NUMBER)
history_data=[]
frame=5
factor=2
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
sensor_value=0
try:
sensor_value = int(data['value'])/10.24
except e:
print("There was an error while parsing the response: ",e)
continue
print ("This is the current temperature ", sensor_value)
bound = compute_bounds(history_data,frame,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'])/10.24)
time.sleep(10)
continue
try:
if sensor_value > bound[0] :
print ("The temperature has increased suddenly. Sending an SMS.")
response = sms.send_sms("Someone has opened the fridge door")
print("This is the response ",response)
history_data.append(sensor_value);
except Exception as e:
print ("Error",e)
time.sleep(10)
The temperature data collected from the sensor can be sent to the Bolt Cloud and represented in the for of a graph for analysis purposes.
Step 1: Go to cloud.boltiot.com and create a new product. While creating the product, choose product type as Output Device and interface type as GPIO. After creating the product, select the recently created product and then click on configure icon.
Step 2: Move to the code tab.
Step 3:Select the file type as".js".
Step 4:Write the code and then save the product.
- Code
setChartLibrary('google-chart');
setChartType('predictionGraph');
setChartTitle('Temperature values');
setAxisName('Time','Temperature');
setCrosshair(true);
mul(0.097);
plotChart('time_stamp','temp');
Step 5:Link the product to the desired Bolt module
Step 6:You can view the data logged by clicking on "View this device".
Comments
Please log in or sign up to comment.