The following project mainly has two functionalities. They are
- to turn ON the LED light and
- to turn OFF the LED light
The above two functinalites are mainly dependent on the intensity of light in the surrounding environment. This can be done by using LDR(Light Dependent Resistor). The LDR detects the light from the surroundings.
When the intensity of light detected by the LDR is greater than the given maximum value, the LED light is turned OFF and an alert message is sent to telegram. Likewise, when the light detected by the LDR is less than the given minimum value, the LED light is turned ON and an alert message is sent to telegram.
This system can be used
- as street lights in the abandoned places such as outskirts of the city where the lights turn ON automatically in the darkeness.
- can be installed in houses.
- to monitor the intensity of light of a photosensitive environment such as in medicine storage, where a sudden increase or decrease of light intensity can be harmful.
Let’s take a look at the circuit connections, instructions and working of the project.
2.1 Circuit Connections
Given below are the step by step circuit connections of LDR and LED with Bolt WiFi module.
i) Connect the LDR, 10k ohm resistor, LED(+ve terminal to 330 ohm resistor) and 330 ohm resistor in seires on bread board.
ii)Connect the first point of LDR to 3.3V pin of WiFi module.
iii)Connect the other point of LDR to A0 pin of WiFi module.
iv)Connect the -ve terminal of the LED to the GND pin of WiFi module.
v)Connect the other terminal of the 330 ohm resistor to the 0 pin of the WiFi module.
- Connections of Bolt WiFi module
- Connections on Breadboard
2.2 Working of the Project
The LDR sensor keeps on reading the values of the intensity of the light in the surroundings. The maximum value of light intensity read by LDR is 1024. The LDR doen’t have terminals. The input to the LDR is taken from the A0 pin of the WiFi module and the output of the LED is given to the digital pin 0 of the WiFi module. The power to the circuit i.e LDR is given form the 3.3V pin of the WiFi module. The resistance of the LDR changes with the intensity of the light. The resistance of LDR decreases if the intensity of the light is more and the resistance of the LDR increases if the intensity of the light is less in the surroundings. As the Bolt WiFi module cannot read the resistance but it can read the voltage values, a voltage divider circuit is made. The digital pin 0 acts as an power supply to LED and hence it lits up. Due to the 10k ohm and 330 ohm resistor the voltage is divided.
When the intensity of the light is more the resistance value decreases and there it sends a message to cloud and then sends an alret message to telegram the the lights are OFF. Similarly, when the intensity of the light is less the resistance value of the LDR increases, sends a message to the cloud which lits up the LED light and sends a message to the cloud that the lights are ON.
This is how the project works.
2.3 Instructions
- Make sure that the connection are correct.
- Resistor should be connected to the LED so that it controls the flow of the current through the LED.
- Don't keep pressure on the Bolt wifi Module.
3.1 Creating telegram account
i) Download the telegram account fom playstore.
ii) Sign up for a new account and verify your account
iii)You will be logged into your account.
3.2 Creating a new channel in telegram
i)Go to the homescreen. Click on the left top corner to jump into menu. Click in “New Channel”
ii)Create a new channel by giving a Channel name and Description.
iii)Click on public channel and give a link name for the new channel.
Therefore, new channel is created.
Now alert messages are to be sent automatically to telegram. This can be done with the help of telegram Bot.
Its helps in sending the messages whenever the light is ON or OFF.
3.3 Creating a telegram bot
i) Search for BotFather on telegram homepage search bar.
ii) Then click on start .
iii)Text “/new” for creating a new bot
iv)Give a name and username for the bot.
v) A bot API key will be generated. Bot is successfully created.
2.3.4 Linking the bot with the channel
i) Go to new channel created and click on the channel info.
ii) Click on administrators.
iii)Click on "Add Admin".
iv)Search for the username of the bot which is created in the previous section.
v)Hence, bot is added to the channel.
- Create a folder names LDR_alert.
- Create a file conf.py and save it.
bolt_api_key="XXXX" #This is your Bolt API key
device_id="XXXX" #This is the device ID like BOLTXXXX
telegram_chat_id="@XXXX" #This is the channel ID of the telegram channel created
telegram_bot_id="botXXXX" #This is the bot ID i.e. HTTP API key of the bot created
maximum=1000 #Maximum limit below which the LED is lit up
- Create another file ldr_alert.py and enter the below code
This is only a part of code for reading the sensor values from the Bolt device
import requests # for making HTTP requests
import json # library for handling JSON data
import time # module for sleep operation
from boltiot import Bolt # importing Bolt from boltiot module
import conf # config file
mybolt = Bolt(conf.bolt_api_key, conf.device_id)
#Getting value from sensor
def get_sensor_value_from_pin(pin):
"""Returns the sensor value. Returns Failed if request fails"""
try:
response = mybolt.analogRead(pin)
data=json.loads(response)
if data["success"]!=1:
print("Request not successful")
print("This is the response->", data)
return 0
sensor_value = int(data["value"])
return sensor_value
except Exception as e:
print("Something went wrong when returning the sensor value")
print(e)
return 0
#sending message to telegram
- This part of code sends alert messages via telegram
def send_telegram_message(message):
"""Sends message via Telegram"""
url="https://api.telegram.org/"+conf.telegram_bot_id+"/sendMessage"
data={
"chat_id": conf.telegram_chat_id,
"text": message
}
try:
response=requests.request(
"POST",
url,
params=data
)
#print("This is the Telegram URL")
#print(url)
print("This is the Telegram response")
print(response.text)
telegram_data = json.loads(response.text)
return telegram_data["ok"]
except Exception as e:
print("An error occurred in sending the alert message via Telegram")
print(e)
return False
- This part of code checks whether to turn ON or OFF the LED
while True:
sensor_value = get_sensor_value_from_pin("A0")
print("The current sensor value is:", sensor_value)
try:
if sensor_value > conf.maximum:
print("The intensity of light has increased....")
print("Making request to TELEGRAM to send a text message...")
message="Alert! As the sensor value is enough light, the lights are OFF..."
message_text=mybolt.digitalWrite('0','LOW')
telegram_status = send_telegram_message(message)
print("This is the Telegram status:", telegram_status)
if sensor_value < conf.maximum:
print("The intensity of light has decreased....")
print("Making request to TELEGRAM to send a text message...")
message="Alert! As there is no enough light, the lights are ON..."
message_text=mybolt.digitalWrite('0','HIGH')
telegram_status = send_telegram_message(message)
print("This is the Telegram status:", telegram_status)
except Exception as e:
if sensor_value==0:
print("Error has occurred")
print(e)
time.sleep(10)
4. OUTPUTHere is the output in telegram
Comments
Please log in or sign up to comment.