Upagyya Dixit
Published © GPL3+

Notified Via Buzzer, SMS and Email about lights of your room

When someone will turn ON/OFF your room's light then you will immediately get an SMS/Email alert and It will also produce sound of buzzer.

IntermediateFull instructions provided16 hours643
Notified Via Buzzer, SMS and Email about lights of your room

Things used in this project

Hardware components

Bolt WiFi Module
Bolt IoT Bolt WiFi Module
×1
Photo resistor
Photo resistor
×1
Buzzer
Buzzer
×1
Breadboard (generic)
Breadboard (generic)
×1
Male/Female Jumper Wires
Male/Female Jumper Wires
×1
USB-A to Mini-USB Cable
USB-A to Mini-USB Cable
×1
Resistor 10k ohm
Resistor 10k ohm
×1

Software apps and online services

Snappy Ubuntu Core
Snappy Ubuntu Core
SMS Messaging API
Twilio SMS Messaging API
Bolt IoT Android App
Bolt IoT Android App
Bolt Cloud
Bolt IoT Bolt Cloud

Story

Read more

Schematics

Hardware connection

connection of buzzer with I/P( pin'0') and GND, LDR with pin 'A0' and 3.3v and resistor(10kohm) with pin 'A0' and GND.

circuit connection

Code

Complete Code for anomaly detection

Python
The full code which is used to detect anomaly and we'll get Alert via SMS, Email and Buzzer.
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)
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 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)
            print("Making request to Mailgun to send an email")
            response = mailer.send_email("Alert", "Someone turned on the lights " +str(sensor_value))
            response_text = json.loads(response.text)
            print("Response received from Mailgun is: " + str(response_text['message']))
            response = mybolt.digitalWrite('0','High')
            print 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)
            print("Making request to Mailgun to send an email")
            response = mailer.send_email("Alert", "Someone turned off the lights " +str(sensor_value))
            response_text = json.loads(response.text)
            print("Response received from Mailgun is: " + str(response_text['message']))
            response = mybolt.digitalWrite('0','High')
            print response
        history_data.append(sensor_value);
    except Exception as e:
        print ("Error",e)
    time.sleep(10)

Credits

Upagyya Dixit

Upagyya Dixit

1 project • 0 followers
Bachelor of Technology Electronics Engineering

Comments