harshit kumar
Published © GPL3+

Lamp Automation

When I study under lamp I wish that the lamp could switch on itself as I switch off the room lights. same I have tried to do in my project.

IntermediateFull instructions provided4 hours528
Lamp Automation

Things used in this project

Hardware components

Bolt WiFi Module
Bolt IoT Bolt WiFi Module
×1
Resistor 10k ohm
Resistor 10k ohm
×1
Male/Male Jumper Wires
as connecting wires
×1
LDR, 5 Mohm
LDR, 5 Mohm
to sense light
×1
5 mm LED: Yellow
5 mm LED: Yellow
led has been used in place of lamp for testing.In place of led we can connect the led bulb of lamp
×1
USB-A to Mini-USB Cable
USB-A to Mini-USB Cable
to connect to bolt wifi module
×1

Software apps and online services

mailgun
vmware
ubuntu-linux
python

Story

Read more

Schematics

circuit connection

hand made simple circuit connection

Code

email_conf.py

Python
contains all the information about api keys, device id,multiplication factor,frame size
API_KEY="XXXXX"
DEVICE_ID="BOLTXXXX"
MAILGUN_API_KEY="XXXXX'
SANDBOX_URL="XXXX"
SENDER_EMAIL="XXXX"
RECIPIENT_EMAIL="XXXX"
MUL_FACTOR=2
FRAME_SIZE=10

led_control_by_sensor.py

Python
main programme
import email_conf,json,time,math,statistics
from boltiot import Bolt,Email

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(email_conf.API_KEY, email_conf.DEVICE_ID)
email=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 getting 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 Exception as e:
        print("There was an error while parsing the response: ",e)
        continue
    bound = compute_bounds(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 ("light level increased suddenly. sending email.")
            status=mybolt.analogWrite('0','0')
            print("status of led is :",status)
            response=email.send_email("alert","someone turned on the lights")
            response=json.loads(response.text)
            print("this is the response from mailgun"+str(response['message']))
        elif sensor_value < bound[1] :
            print ("light level decreased suddenly. sending email.")
            status=mybolt.analogWrite('0','20')
            print("status of led is :",status)
            response=email.send_email("alert","someone turned off the lights")
            response=json.loads(response.text)
            print("this is the response from mailgun"+str(response['message']))
        history_data.append(sensor_value);
    except Exception as e:
        print ("Error",e)
    time.sleep(10)

Credits

harshit kumar
1 project • 0 followers

Comments