Nikita Shehra
Published © GPL3+

Room Light Monitoring

This project helps to maintain the intensity in the defined range and alerts us if an anomaly is detected.

BeginnerShowcase (no instructions)5 hours450
Room Light Monitoring

Things used in this project

Hardware components

Bolt WiFi Module
Bolt IoT Bolt WiFi Module
×1
LDR, 5 Mohm
LDR, 5 Mohm
×1
Resistor 10k ohm
Resistor 10k ohm
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Solderless Breadboard Half Size
Solderless Breadboard Half Size
×1

Software apps and online services

Bolt Cloud
Bolt IoT Bolt Cloud
Bolt IoT Android App
Bolt IoT Android App
Ubuntu terminal
Mailgun

Story

Read more

Code

Room Light Monitor

Python
Alert code
import email_conf
import json, time, math, statistics
from boltiot import Email, Bolt

min_limit = -51

def compute(history_data,frame_size,factor):
    if len(history_data)<int(frame_size) :
        return None

    if len(history_data)>frame_size :
        del history_data[0:len(history_data)-frame_size]
    Mn=statistics.mean(history_data)
    Var=0
    for data in history_data :
        Var += math.pow((data-Mn),2)
    Zn = factor * math.sqrt(Var / 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(email_conf.API, email_conf.DEVICE_ID)
mailer = Email(email_conf.MAILGUN, email_conf.SANDBOX, 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'])
        if sensor_value<min_limit:
          response = mailer.send_email("Alert","Light intensity decreased below the minimum limit. The current value is " +str(sensor_value))   
    except e:
        print("There was an error while parsing the response: ", e)
        time.sleep(10)
        continue     

    bound = compute(history_data,email_conf.FRAME_SIZE,email_conf.MUL_FACTOR)
    if not bound:
        required_data_count=email_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 decreased suddenly. Sending an Email.")
             response = mailer.send_email("Alert","Someone turned off the lights. The current value is " +str(sensor_value))
             print("This is the response ",response)
        history_data.append(sensor_value);
    except Exception as e:
          print ("Error occured",e)
    time.sleep(10)

Room Light Monitor

Java
Prediction Graph
setChartLibrary('google_chart');
setChartTitle('Light Monitor');
setChartType('predictionGraph');
setAxisName('Time','Light');
mul(0.097);
plotChart('time_stamp','light');

Credits

Nikita Shehra
1 project • 0 followers

Comments