Anubhav Kumar
Published

SMS alert System on Phone Via LDR

Secure privacy of your room during lockdown. Build a SMS alert system on your phone, If someone turns ON/OFF Light

IntermediateFull instructions provided345
SMS alert System on Phone Via LDR

Things used in this project

Hardware components

Bolt WiFi Module
Bolt IoT Bolt WiFi Module
×1
LDR, 5 Mohm
LDR, 5 Mohm
×1
USB-A to Micro-USB Cable
USB-A to Micro-USB Cable
×1
Resistor 10k ohm
Resistor 10k ohm
×1

Software apps and online services

SMS Messaging API
Twilio SMS Messaging API
Bolt Cloud
Bolt IoT Bolt Cloud

Story

Read more

Schematics

Schematic Diagram

Code

Configuration.py

Python
SSID = 'AC***************************(This is the Twilio Account SID)' 
AUTH_TOKEN = 'd4*#*#*#*#*#*#*#####*#(This is your Auth Token of Twilio)' 
FROM_NUMBER = 'This is the trial number of Twilio'
TO_NUMBER = 'This is your Number. +91XXXXXXXXXX'
API_KEY = '42******-6***-4***-9***-8b4141******'
DEVICE_ID = 'BOLT********'
FRAME_SIZE = 10
MUL_FACTOR = 6

Main_Code.py

Python
#Here we are importing some important librarires to help in our code
import conf, json, time, math, statistics

#Import Bolt Library
from boltiot import Sms, Bolt

#Define a fucntion for checking any suddeness in light
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]

#Give info like your api key, device ID, your number, Twilio Number etc.
mybolt = Bolt(conf.API_KEY, conf.DEVICE_ID)
sms = Sms(conf.SSID, conf.AUTH_TOKEN, conf.TO_NUMBER, conf.FROM_NUMBER)
history_data=[]

#Make an infinite loop which will keep running until you stop with CTRL + C
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 ("The light level increased suddenly. Sending an SMS.")
            response = sms.send_sms("Someone turned on the lights")
            print("This is the response ",response)
        elif sensor_value < bound[1]:
            print ("The light level decreased suddenly. Sending an 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)

Credits

Anubhav Kumar
1 project • 0 followers
Pursuing Bachelor in Technology in Electronics and Communication. Learning Web Development(Front end) and Internet of Things.
Thanks to Purnanga Das.

Comments