This is my first IoT Project ever :-) Please do provide your valuable feedback as necessary.
In this project, I tried to automate LIFX Smart Bulb based on light availability. Light intensity is read using LDR connected to my new friend BOLT WIFI Module. Based on sensor value, API request for lights turn on will get initiated to LIFX. This can be easily extended to Turn Off as well.
Step 1 : Hardware Setupa) Connect LIFX Smart Bulb to power lines.
b) Connect LDR to BOLT WIFI Module.
i) Connect one leg of LDR to A0 pin and another leg to 3.3V pin.
ii) Connect one leg of 10k Ohm resistor to GND pin and another leg to A0 pin.
c) Power on Bolt module and link it to Bolt Cloud.
Step 2 : Software Setupa) Setup OS: I have launched ubuntu on AWS.
b) Install required python libraries. sudo pip3 install boltiot
c) Write new python program like conf.py to store all access keys. Refer sample code below.
API_KEY = 'API key from Bolt IoT Cloud'
DEVICE_ID = 'Bolt IoT Device ID'
token = "LIFX Token from https://cloud.lifx.com/settings"
d) Write Python Code to read light intensity (from LDR) and initiate API request to Turn on LIFX Smart bulb. Refer sample code below
import conf, json, time, requests
from boltiot import Bolt
import json, time
minimum_limit = 20
mybolt = Bolt(conf.API_KEY, conf.DEVICE_ID)
while True:
print ("Reading sensor value")
response = mybolt.analogRead('A0')
data = json.loads(response)
print("Sensor value is: " + str(data['value']))
try:
sensor_value = int(data['value'])
if sensor_value < minimum_limit:
print("Making request to Lifx to Turn on Lights")
headers = {
"Authorization": "Bearer %s" % conf.token,
}
payload = {
"power": "on",
}
response = requests.put('https://api.lifx.com/v1/lights/all/state', data=payload, headers=headers)
print("Response received from Lifx is: " + str(response))
except Exception as e:
print ("Error occured: Below are the details")
print (e)
time.sleep(10)
## "conf" in above code snippet refers to python module coded in previous step to store access keys.
## minimum_limit can be adjusted accordingly
## payload - Off can be used instead of On to automate Turn OFF as well.
## Above sample code handles ALL LIFX Smart Bulbs under authorized account. We can handle selective bulbs as well using selectors. Refer Lifx website for more tricks :-)
Step 3: ExecutionNow, execute python program written in above steps which will Turn ON LIFX Smart Bulb if light intensity is less than minimum value (20 in above sample)
Comments
Please log in or sign up to comment.