This project automatically turn ON/OFF the LED based on the time i.e. from 6 pm to 6 am, it checks external light intensity and if it is less than the limit specified then the LED will glow, also we can even control this LED using our voice commands in Google Assistant, and even we will receive SMS alert like "Lights ON" or "Lights OFF" based on LED. So these four (4) features make up my project using BOLT IoT Platform. This project consist of 2 sensors namely LDR (for sensing light intensity of the environment) and LED (used as output indicator).
This system can be introduced in the street lights or else it can be used in our house for controlling LED by itself.
DEMONSTRATION:Receiving Alert Notification by SMS:This section will show the auto functionality of LED based on the external light intensity, system time (6pm to 6am), and commands given to Google Assistant.
Circuit connection:
The circuit connection is shown below and the required equipments are mentioned in Hardware components.
Here the connections are given from bread board to Bolt Wifi module. Since LED is an output device so it is connected to digital pin 0 of the bolt whereas LDR is an input device so it is connected to analog pin A0 of the bolt. Since both LED and LDR cannot resist much current through it so we need to connect a suitable resistor for them. LED's positive leg (longer leg) is connected with a 330 ohm resistor and given to digital pin 0 and negative leg (shorter leg) is give to ground where as LDR's one leg is given to A0 pin and another leg is connected with a 10k ohm resistor and given to 3V3 of bolt this resistor's other end is given to ground.
Note: LDR do not have positive or negative legs whereas LED is having positive and negative legs.
Overall connections of the circuit is mentioned below:
- LED positive - 330 ohm - pin 0
- LED negative - ground
- LDR 1st leg - 3V3
- LDR 2nd leg - 10k ohm 1st leg - A0
- 10k ohm 2nd leg - ground
Creating Twilio account:
Twilio is a third-party SMS functionality provider. It is a cloud communications platform as a service (PaaS) company. Twilio allows software developers to programmatically make and receive phone calls and also send and receive text messages using its web service APIs.
To create an Free account you need to follow few steps:
- Open https://www.twilio.com/ in browser.
- Click on "Products" and select "Programmable SMS".
- sign up and start building.
Fill up all the details and click on "start your free trail".
- Give the mobile number where you need to receive sms.
- Give your project name (you can give any name) and continue and click on get a number and then go to dashboard.
- Here you will be getting Account SID and AUTH TOKEN (click on show).
- In configuration file we have mentioned FROM_NUMBER, this is shown in Trail number (shown in Dashboard).
Voice commands through Google Assistant:
This can be done with the help of IFTTT Google Assistant and Webhooks:
- Go to IFTTT website
- Click on "continue with google"
- Click on '+This' to create the trigger.
- Search for "google assistant" and select "Say Simple Phrase"
- You can type out the commands that you want to give to turn ON LED and click on "Create trigger" and then click on "+That".
- Search and select Webhooks and then Make a web request.
- Here you have to paste the API key.
- Method will be GET
- Content type will be Application/json
- Click on "create action"
Repeat similar steps for making LED OFF
AutomatedLight system code:
Codes are written in python using Ubuntu Software. We will be going to write the code in two (2) files i.e. conf.py file and street_light.py file. Configurations required for sms alert from Twilio, and certain data required for connecting bolt to cloud like API KEY and DEVICE ID (both are unique for different users) are mentioned in this file and saved as conf.py (you can rename it but save the extension as.py because its a python code. The code for configuration file is shown in code section.
The complete code of main program is given below:
import time, json, conf
from boltiot import Bolt,Sms
mybolt = Bolt(conf.API_KEY, conf.DEVICE_ID )
sms = Sms(conf.SSID, CONF.AUTH_TOKEN, conf.TO_NUMBER, conf.FROM_NUMBER)
min_time_limit = 18 //if time is 6pm or more then led should glow
max_time_limit = 5 //if time is 6am or less then led should glow
light_limit = 800 //if sensor value is less than this value then led should glow
def on_call():
led = mybolt.digitalWrite("0", "HIGH") // making led ON (HIGH means 1)
print("Lights ON") // displays on terminal
sms.send_sms("Lights ON") // sends sms
time.sleep(60) // 60 seconds delay
return led
def off_call():
led = mybolt.digitalWrite("0", "LOW") // making led OFF (LOW means 0)
print("Lights OFF") // displays om terminal
sms.send_sms("Lights OFF") // sends sms
time.sleep(60) // 60 seconds delay
return led
while(True): // infinite loop
t = time.localtime() // fetch system time
current_time = int(time.strftime("%H", t)) // takes only hours (H means hour)
print("Current time is :", current_time) // prints on terminal
response = mybolt.analogRead("A0") // takes value of LDR sensor
data = json.loads(response) // converts sensor value to json
result = int(data["value"]) // takes integer value of data
print("Light value of sensor is :", result) // prints on terminal
if ((current_time>=min_time_limit) or (current_time<=max_time_limit)): //18<t<6
if result<=light_limit: // sensor value less than 800
on_call()
elif result>=light_limit: // sensor value greater than 800
off_call()
elif ((current_time<=min_time_limit) or(current_time>=max_time_limit))://6<t<18
if result<=light_limit: // sensor value less than 800
on_call()
elif result>=light_limit: // sensor value greater than 800
off_call()
A
brief explanation is given in the form of comments in the main code. Almost every line of the code is explained, here the system time is in 24 hour format so I am using min_time_limit as 18 which implies 6pm (evening). Since we usually turn ON the light at 6pm in the evening I have set the value as 18 you can even alter the time based on your requirement. While loop is used for infinite iterations, local time of the system is fetched by time.localtime() and we are considering only hours so we are taking "%H" thus we have taken max_time_limit as 5 because till 5:59am hour is 5 but actually time is almost 6am. In response we are going to take the LDR sensor value and covert to json format for computer readability and then integer value of this data is stored in result for human readability. Then the program enters the if conditions and goes for on_call() or off_call() which are already defined in the above section of the code to turn on/off and also to send SMS alert.
If you want the LED to be ON from 6 pm to 6 am and in the remaining time you need the LDR sensor value to turn ON or OFF the LED then you can use the below code and replace this code in the above code's "if condition statements".
if ((current_time>=min_time_limit) or (current_time<=max_time_limit)):
on_call
elif ((current_time<=min_time_limit) or (current_time>=max_time_limit)):
if result<=light_limit:
on_call()
elif result>=light_limit:
off_call()
Hence
by using this code the LED will be ON from 6 pm till 6 am without considering the external light intensity, remaining time i.e. during day time it takes the LDR sensor value to turn ON/OFF the LED.
That's it, we are done with the code now just run the program and check the output by turning ON/OFF the light in your room so to check the LED's functionality. Check out the youtube video shared above for more clarification.
Comments
Please log in or sign up to comment.