Suppose you are at your work place and somebody breaks into your house. The moment the intruder opens your safe, the light intensity inside the safe changes and you will be notified about the safe being opened through an SMS and necessary actions can be taken.
The Light Detecting Resistor (LDR) is connected to the bolt wifi module and is powered with a 5v supply. The LDR takes 10 readings initially with a time gap of 10 seconds to input the normal light intensity conditions around the safe. Using Z-score analysis the sudden change in light intensity (light from the surroundings enters when the safe is opened ) is detected and using twilio's automated messaging service SMS is sent regarding the activity to the registered mobile number.
2. Anti-Theft SystemCircuit Connections
Given below is the circuit connection of the LDR with the bolt wifi module for the Anti-Theft System based on the light intensity of the surroundings.
Here are the steps for making the hardware connections:
- Step 1: Insert one lead of the LDR into the Bolt Module's 3v3 Pin.
- Step 2: Insert other lead of the LDR into the A0 pin.
- Step 3: Insert one leg of the 10k Ohm resistor into the GND pin.
- Step 4: Insert the other leg of the resistor also into the A0 pin.
- Warning!! Make sure that at no point do the 3.3V (or even 5V) and GND pins or wires coming out of them touch each other. If you short power to Ground without a resistor even accidentally, the current drawn might be high enough to destroy the Bolt module.
Configuration File
The Python coding for this project has been done in windows 10.
Before we start coding of the automating controlling of LED brightness 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'
FRAME_SIZE=10 //Frame Size for Z score analysis.
MUL_FACTOR=3 //Multiplication factor for Z score analysis.
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.
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, which is as for now taken as 10, 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.
Creating an Account on Twilio
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.
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: Click on "Products".
Step 6: Now enable the SMS services by clicking on two checkboxes for Programmable SMS and Phone Numbers.
Once you have done this, scroll to the bottom of the screen and click on "Continue".
Step 7: Now, you will need to give a name for your project. I have given the name as My Project. Click on "Continue" once you have entered the project name.
Step 8: Click on "Skip this step" when it asks you to Invite a Teammate.
Step 9: Your project should be created at this point. Click on "Project Info" to view the account credentials which is required for your projects.
Step 10: 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 11: From the drop-down menu, choose "Programmable SMS". Now click on Get Started
button to generate phone number.
Step 12: Click on Get a number
button.
Step 13: Then a popup will appear. Click on Choose this number
button.
Step 14: Then a popup will appear which will have the final number. Copy this number and save to notepad for future references.
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.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 ("Anomaly Detected; Sending Sms")
response = sms.send_sms("Someone turned on the lights")
print("This is the response ",response)
elif sensor_value < bound[1]:
print ("Anomaly Detected; Sending 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)
Output Screenshots
The first 10 inputs is used to determine the standard lighting conditions inside the safe. When there is a sudden change in light intensity, the z-score algorithm detects an anomaly and sends an SMS using Twilio account.
Comments
Please log in or sign up to comment.