bikram947
Created February 15, 2020 © GPL3+

Light Intensity Control System Using Google Assistant

Make the LED ON, OFF and control the intensity based on the brightness of the surroundings and alerts for a sudden change in the brightness.

IntermediateFull instructions provided4 hours7
Light Intensity Control System Using Google Assistant

Things used in this project

Hardware components

Bolt WiFi Module
Bolt IoT Bolt WiFi Module
×1
Breadboard (generic)
Breadboard (generic)
×1
Texas Instruments HD74LS00P
×1
LDR, 5 Mohm
LDR, 5 Mohm
×1
Resistor 10k ohm
Resistor 10k ohm
×1
5 mm LED: Yellow
5 mm LED: Yellow
×1
Resistor 330 ohm
Resistor 330 ohm
×1
USB-A to Mini-USB Cable
USB-A to Mini-USB Cable
×1
Connecting wires
×1
Wire Cutter
×1

Software apps and online services

Bolt Cloud
Bolt IoT Bolt Cloud
Ubuntu
IFTTT Google Assistant
IFTTT Webhooks
Mailgun
Gmail

Story

Read more

Schematics

Complete Circuit Diagram for the project

Code

Code for 'AUTO' functionality

Python
We should create a python file in the ubuntu server and run it to control the intensity of the LED depending on the brightness of the surrounding.
from boltiot import Bolt,Email
import json,time,math,statistics
import conf

mybolt=Bolt(conf.API_KEY,conf.DEVICE_ID)

def send_mail(message):
        mailer = Email(conf.MAILGUN_API_KEY, conf.SANDBOX_URL, conf.SENDER_EMAIL, conf.RECIPIENT_EMAIL)
        try:
                print("Making request to Mailgun to send an email")
                response = mailer.send_email("Alert",message)
                response_text = json.loads(response.text)
                print("Response received from Mailgun is: " + str(response_text['message']))
        except Exception as e:
                print("Error occoured while sending email.Below are details")
                print(e)

history_data=[]

def compute_bounds(history_data,frame_size,mul_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)
        var=0
        for i in history_data:
                var+=math.pow((mn-i),2)
        zn=mul_factor*math.sqrt(var/frame_size)
        up_bound=history_data[-1]+zn
        low_bound=history_data[-1]-zn
        return [up_bound,low_bound]


while(True):
        try:
                ldr_response=mybolt.analogRead('A0')
                ldr_data=json.loads(ldr_response)
                print(ldr_data)
        except e:
                print(e)
        if(ldr_data['success']!=1):
                print("Error in retriving data from ldr")
                time.sleep(10)
                continue
        ldr_val=int(ldr_data['value'])
        print('ldr response is '+str(ldr_val))
        ldr_val1=ldr_val//4
        if(ldr_val1>255):
                ldr_val1=255
        led_intensity=(255-ldr_val1)
        mybolt.analogWrite('2',led_intensity)
        print('led intensity is '+str(led_intensity))


        bound=compute_bounds(history_data,conf.FRAME_SIZE,conf.MUL_FACTOR)
        if not bound:
                required_data=conf.FRAME_SIZE-len(history_data)
                print('not enough data for z scre analysis. Required  '+str(required_data)+' more data')
                history_data.append(ldr_val)
                time.sleep(10)
                continue
        try:
                if(ldr_val<bound[1]):
                        message='ldr value decreased suddenly. Current ldr value is '+str(ldr_val)
                        print('ldr value decreased suddenly. Current ldr value is '+str(ldr_val))
                        send_mail(message)
                if(ldr_val>bound[0]):
                        message='ldr value increased suddenly. Current ldr value is '+str(ldr_val)
                        print('ldr value increased suddenly. Current ldr value is '+str(ldr_val))
                        send_mail(message)
                history_data.append(ldr_val)
        except Exception as e1:
                print("Error occoured. Below is the details")
                print(e1)
        time.sleep(10)

Credits

bikram947

bikram947

0 projects • 0 followers

Comments