K.S.G.Subhasree
Published

LDR Darkness Sensor

The project is about the automatic control of the LED lights which depends on the intensity of the light in the surrounding environment.

BeginnerFull instructions provided3 hours355
LDR Darkness Sensor

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
Breadboard (generic)
Breadboard (generic)
×1
LED (generic)
LED (generic)
×1
LDR, 5 Mohm
LDR, 5 Mohm
×1
Resistor 330 ohm
Resistor 330 ohm
×1
Resistor 10k ohm
Resistor 10k ohm
×1
Male/Male Jumper Wires
×1

Software apps and online services

Bolt Cloud
Bolt IoT Bolt Cloud
Bolt IoT Android App
Bolt IoT Android App

Story

Read more

Schematics

Circuit Connection

Connect the circuit as shown below

Code

Complete Code for LDR darkness sensor

Python
Here is the python code for LDR darkness sensor. Copy this code in a text file and save it. Run this code in the terminal
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)
#Reading sensor values from the Bolt device
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 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

#Checks whether the LED to turned ON or OFF
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)

Credits

K.S.G.Subhasree
1 project • 0 followers
Contact

Comments

Please log in or sign up to comment.