Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
rathish TJ
Published

Automated light intensity

Electricity is the need for future. This project will help to minimize the use of electricity in the operation of a led light.

IntermediateFull instructions provided2 hours185
Automated light intensity

Things used in this project

Hardware components

Bolt WiFi Module
Bolt IoT Bolt WiFi Module
×1
LDR, 5 Mohm
LDR, 5 Mohm
×1
LED (generic)
LED (generic)
×1
Resistor 1k ohm
Resistor 1k ohm
×1

Software apps and online services

Bolt IoT Android App
Bolt IoT Android App
SMS Messaging API
Twilio SMS Messaging API

Story

Read more

Schematics

Automated light

Automated controlling of intensity of a LED

Code

Automated light

Python
Automated control of intensity of LED
mport requests, json, time, math, statistics //import various python libraries
from boltiot import Bolt
//import the boltiot module from the Bolt python library
import conf                  //import the configuration file
  
//---------FUNCTION TO COMPUTE BOUNDS OR Z SCORE ANALYSIS------------//
def compute_bounds(history_data, frame_size, factor): 
//Function to compute bounds
    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]    //Returns Low Bound and High Bound
  
//---------------FUNCTION FOR SMS ALERTS----------------------//
    sms = Sms(conf.SSID,conf.AUTH_TOKEN,conf.TO_NUMBER,conf.FROM_NUMBER)
//-----------------------------------------------------------------//
  
mybolt = Bolt(conf.API_KEY, conf.DEVICE_ID)    //To identify your bolt device
history_data = []                //Array of input values from LDR
  
while True:
  
//---------------------READ INPUT FROM LDR--------------------------//
    response_ldr = mybolt.analogRead('A0')     //Read input from LDR at A0 pin
    ldr_data = json.loads(response_ldr)//Retrieve the input data in json format
    if ldr_data['success']!='1':
        print("There was an error while retrieving the data")
        print(ldr_data['value'])
        time.sleep(10)
        continue
    try:
        sensor_value = int(data_ldr['value'])
        //store current input value in variable 
    except e:
        print("There was an error while parsing the response")
        print(e)
        continue
    print("LDR sensor value is: "+str(sensor_value)) //Print LDR input value
    print(response_ldr)
  
//----------------------MONITORING INTENSITY OF LED--------------------------//     
    led_value_1 = int(sensor_value/4)
    if led_value-1 > 255:
        led_value_1 = 255
    led_value = 255 - led_value_1//Output value to LED based on LDR input value
    response_led = mybolt.analogWrite('0', led_value) //Write output at pin 0
    print("Automated LED value is: "+str(led_value)) //Print LED output value
    print(response_led)
  
//----------------PERFORMING Z SCORE ANALYSIS--------------------------//
    bound = compute_bounds(history_data, conf.FRAME_SIZE, conf.MUL_FACTOR)
    //Call compute_bounds function
  
//-------------COLLECTING SUFFICIENT DATA FOR Z SCORE ANALYSIS---------//
    if not bound:
    //If number of inputs are not sufficient to do Z score analysis
        required_data_count = conf.FRAME_SIZE - len(history_data) -1
        if (required_data_count != 0 and required_data_count != 1):
            print("Not enough data to compute Z score. Need",required_data_count,"more data points.")
        elif (required_data_count == 1):
            print("Not enough data to compute Z score. Need 1 more data point.")
        else:
            print("Enough data to compute Z score.")
        history_data.append(int(ldr_data['value']))
        //Append each new input to array history_data[]
        time.sleep(10)    //Wait for 10 seconds
        continue
  
//-----------DETECTING ANOMALY AND SENDING ALERTS--------------//
    try:
        if sensor_value > bound[0]: //If input crosses upper bound
            print("The light level has increased suddenly.Sending a SMS")
            response = sms.send_sms("Someone turned on the light")
            print("This is the response",response)
        elif sensor_value < bound[1]: //If input crosses lower bound
            print("The light level has decreased suddenly.Sending a SMS")
            response = sms.send_sms("Someone turned off the light")
            print("This is the response",response)        
            history_data.append(sensor_value) 
        //Append each new input to array history_data[]
    except exception as e:
        print("Error")
        print(e)
    
    time.sleep(10)    //Wait for 10 seconds

Credits

rathish TJ
2 projects • 1 follower
I am studying in PSNA college.I was interested in the field of embedded system and EV and working on it for the past one year.
Contact

Comments

Please log in or sign up to comment.