Kunal Gupta
Published © GPL3+

Light Automation Using LDR & IFTTT

An automatic system built on BoltIoT platform which controls the Light Intensity automatically itself or By using Voice Commands.

IntermediateFull instructions provided1.5 hours1,145
Light Automation Using LDR & IFTTT

Things used in this project

Hardware components

Bolt WiFi Module
Bolt IoT Bolt WiFi Module
×1
USB-A to Micro-USB Cable
USB-A to Micro-USB Cable
×1
LDR, 5 Mohm
LDR, 5 Mohm
×1
LED (generic)
LED (generic)
×1
Resistor 10k ohm
Resistor 10k ohm
×1
Resistor 330 ohm
Resistor 330 ohm
×1
Male/Male Jumper Wires
×1
Breadboard (generic)
Breadboard (generic)
×1

Software apps and online services

Snappy Ubuntu Core
Snappy Ubuntu Core
Maker service
IFTTT Maker service
Assistant SDK
Google Assistant SDK

Story

Read more

Schematics

Schematic Diagram

Code

Light_automation_LDR.py

Python
This is the complete code for Main File. A second file is also made with name conf.py which includes Bolt_API_KEY AND BOLT_DEVICE_ID.

The Code is Self-Explanatory.
"""impoting libraries
    conf to import api_key and device_id written in conf.py
    Bolt for connecting to our bolt module using api_key and device_id
    json to convert data from sensor into json format [key:value]
    time to read value after equal interval of time
"""
import conf
from boltiot import Bolt
import json, time 


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

"""creating a function to set the value of LED automatically(0-255)
"""

def set_intensity(pin, value):
	mybolt.analogWrite(pin, value)

"""create a function to calculate Required LED Intensity
   corresponding to the LDR sensor value
   subtract from 255? - if LDR value=1024(sufficient light)-led will be off(0)
                        if LDR value=0(no light)-led will be on(255)
   intermediate value of LDR will corresponds to a specific LED intensity value(0-255)
   """

def converter(sensor_value):
	led_intensity= 255-(sensor_value*255/1024)
	return led_intensity


while True: 
    print ("Reading Sensor Value")
    response_ldr = mybolt.analogRead('A0') # Read the LDR Sensor Value from A0 Pin
    data = json.loads(response_ldr)   # Convert value in Json format 
    print("Sensor value is: " + str(data['value'])) # Print the value
    try: 
        sensor_value = int(data['value'])
        print("Calculating required Light Intensity for LED")
        led_value_float=converter(sensor_value)  # convert ldr sensor value in required
        led_value= int(led_value_float)          # led value in float and then typecast 
        print(led_value)                         # float into int type
        set_intensity('1',led_value)             # this will make LED to glow
                                                 # with calculated intensity value

# in case of any error encountered        
    except Exception as e: 
        print ("Error occured: Below are the details")
        print (e)
    time.sleep(15)

Credits

Kunal Gupta

Kunal Gupta

1 project • 3 followers

Comments