Shashank Y
Published

Automatic Light Intensity Changer

Changes the intensity of LED according to the intensity of light in the surroundings with message alerts.

BeginnerFull instructions provided2 hours344
Automatic Light Intensity Changer

Things used in this project

Hardware components

Bolt WiFi Module
Bolt IoT Bolt WiFi Module
×1
LDR, 5 Mohm
LDR, 5 Mohm
×1
5 mm LED: Yellow
5 mm LED: Yellow
×1
Resistor 10k ohm
Resistor 10k ohm
×1
USB-A to Micro-USB Cable
USB-A to Micro-USB Cable
×1
Solderless Breadboard Half Size
Solderless Breadboard Half Size
×1
Male/Male Jumper Wires
×4

Software apps and online services

Telegram
Ubuntu

Story

Read more

Schematics

Circuit Connections of LED and LDR

The below circuit connections of LED and LDR with Bolt Wifi Module is for the automatic light intensity detection from surrounding and controlling LED

Code

Python Program code for Automatic Light Intensity Chager

Python
import conf1 , json, time,math,statistics   #importing various python libraries
from boltiot import Bolt
import requests

def compute_bounds(history_data,frame_size,factor):#Function to determine Z-score
	if len(history_data)<frame_size:
		return None
	if len(history_data)>frame_size:
		del history_data[0:len(history_data)-frame_size]
	Mn=statistics.mean(history_data)
	Variance=0
	for data in history_data :
		Variance += math.pow((data-Mn),2)
	Zn = factor* math.sqrt(Variance / frame_size)
	High_bound=history_data[frame_size-1]+Zn
	Low_bound=history_data[frame_size-1]-Zn
	return [High_bound , Low_bound]
	
def send_telegram_message(message):#Function for Telegram message alert
	url="https://api.telegram.org/"+ conf1.TELEGRAM_BOT_ID +"/sendMessage"
	data={"chat_id":conf1.TELEGRAM_CHAT_ID,"text": message}

	try:
		response = requests.request(
		    "POST",
		    url,
		    params=data
   		)
		print("Telegram response:")
		print(response.text)
		telegram_data=json.loads(response.text)
		return telegram_data["ok"]
	except Exception as e:
		print("An error occured in sending message via Telegram")
		print(e)
		return(False)
mybolt=Bolt(conf1.API_KEY,conf1.DEVICE_ID)#To work on our Bolt Iot module
history_data=[]

while True:
	response=mybolt.analogRead('A0')#To Read input from LDR and at pin 'A0'
	data=json.loads(response)
	if data['success']!=1:
		print("There was an error while retriving the data.")
		print("This is the error:"+data['value'])
		time.sleep(5)
		continue

	print("This is the sensor value "+data['value'])
	sensor_value=0
	try:
		sensor_value=int(data['value'])
	except e:
		print("There was an error while parsing the response: ",e)
		continue

	led_value_calc=int(sensor_value/4)#command for automatic intensity changing of LED based on LDR value 
	if led_value_calc>255:
		led_value_calc=255
	led_value=255-led_value_calc
	response_led=mybolt.analogWrite('0',led_value)#To write the value of LED to output
	print("Current LED value is: "+str(led_value))

	bound = compute_bounds(history_data,conf1.FRAME_SIZE,conf1.MUL_FACTOR)
	if not bound:
		required_data_count=conf1.FRAME_SIZE-len(history_data)
		print("Not enough data to compute Z-score. Need ",required_data_count,"more data points")
		history_data.append(int(data['value']))
		time.sleep(5)
		continue

	try:
		if sensor_value>bound[0]:
			print("The light level increased suddenly. Sending an SMS.")
			response=send_telegram_message("The current LED intensity is "+str(led_value))
			print("This is the response ",response)
		elif sensor_value< bound[1]:
			print("The light level decreased suddenly. Sending an  SMS.")
			response=send_telegram_message("The current LED intensity is "+str(led_value))
			print("This is the response ",response)
		history_data.append(sensor_value);
	except Exception as e:
		print ("Error",e)
	time.sleep(5)

Credits

Shashank Y
1 project • 0 followers
Contact

Comments

Please log in or sign up to comment.