Neeraj dhakad
Published

Are you looking for a SMART STREET LIGHT for your HAMLET??

What if your Street lights work automatically without you even bothering about them? and you can also detect light anomalies.

IntermediateFull instructions provided5 hours392
Are you looking for a SMART STREET LIGHT for your HAMLET??

Things used in this project

Hardware components

Bolt WiFi Module
Bolt IoT Bolt WiFi Module
×1
Breadboard (generic)
Breadboard (generic)
×1
LED (generic)
LED (generic)
×1
LDR, 5 Mohm
LDR, 5 Mohm
×1
Male/Male Jumper Wires
×1
Resistor 330 ohm
Resistor 330 ohm
×1
USB-A to Micro-USB Cable
USB-A to Micro-USB Cable
×1

Software apps and online services

SMS Messaging API
Twilio SMS Messaging API
Bolt Cloud
Bolt IoT Bolt Cloud
pycharm

Story

Read more

Code

CODE

Python
This is the main code of the program
import conf 
import json, time, math, statistics
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) 
history_data = [] 
frameSize = 10 
mul_factor = 5 
threshold = 500 
prev_value = -999 
sensor_value = -999

def get_sensor_value(pin): 
	try: 
		response = mybolt.analogRead(pin) 
		data = json.loads(response) 
		
		if data['success'] != 1: 
			print("There was an error in retriving the data") 
			print("This is the error : "+ data['value'])
			return -999 
	
		value = int(data['value'])
		return value
	except Exception as e: 
		print ("There was an error in parsing the data")
		print("This is the error : ", e) 
		return -999 

def turn_on_led():
	print("Turning on the LED")
	response = mybolt.digitalWrite('1', 'HIGH') 
	response2 = mybolt.digitalWrite('2', 'LOW') 
	print(response) 

def turn_off_led():
	print("Turning off the LED")
	response = mybolt.digitalWrite('l', 'LOW')
	response2 = mybolt.digitalWrite('2', 'LOW')
	print(response) 

def compute_bounds(history_data, frame_size, factor): 
	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]

while True: 
	sensor_value = get_sensor_value('A0') 
	print("The current light intensity value is "+ sensor_value) 

	if sensor_value == -999: 
		print("Request Unsuccessful. Skipping.") 
		time.sleep(10) 
		continue 
	
	bound = compute_bounds(history_data, frameSize, mul_factor) 
	if not bound: 
		required_values = frameSize-len(history_data) 
		print ("Not enough data to compute Z-score. Need ", required_values, "more data points") 
		history_data.append(sensor_value) 
		time.sleep(10) 
		continue 

	try: 
		history_data.append(sensor_value) 
		if sensor_value > bound[0]: 
			print("Anomaly detected! Light intensity increased suddenly")
			print("Sending SMS to registered phone number") 
			Twilio_response = sms.send_sms("Alert! An anomaly was detected. The light intensity increased suddenly") 
			print("Response received from Twilio is : ", str(Twilio_response)) 
			print("Status of response : ", str(Twilio_response.status))
			turn_on_led() 
			time.sleep(3) 
			turn_off_led() 
			time.sleep(10) 
			continue 
		
		elif sensor_value < bound[1]: 
			print("Anomaly detected! Light intensity decreased suddenly") 
			print("Sending SMS to registered phone number") 
			Twilio_response = sms.send_sms("Alert! An anomaly was detected. The light intensity decreased suddenly")
			print("Response from Twilio is : ", str(Twilio_response)) 
			print("Status of response : ", str(Twilio_response.status)) 
			turn_on_led() 
			time.sleep(3) 
			turn_off_led() 
			time.sleep(10) 
			continue 

		elif sensor_value < threshold: 
			if prev_value == -999 or prev_value > threshold: 
				print("The light intensity fell below the threshold") 
				turn_on_led() 
				print("Sending SMS to registered phone number") 
				Twilio_response = sms.send_sms("Alert! The light intensity fell below the threshold. The light was turned on")
				print("Response from Twilio is : ", str(Twilio_response)) 
				print("Status of response : ", str(Twilio_response.status)) 

			elif prev_value != -999 and prev_value < threshold: 
			print("The light intensity is still below the threshold") 
			turn_on_led() 

		elif sensor_value >= threshold:
			if prev_value != -999 and prev_value < threshold: 
				print("The light intensity rose above the threshold") 
				turn_off_led() 
				print("Sending SMS to registered phone number") 
				Twilio_response = sms.send_sms("The light intensity rose above the threshold. The light was turned off")
				print("Response from Twilio is : ", str(Twilio_response)) 
				print("Status of response : ", str(Twilio_response.status)) 

			elif prev_value > threshold: 
				print("The light intensity is still above the threshold") 
				turn_off_led() 
	
	except Exception as e: 
		print("Error : ", e) 
	prev_value = sensor_value 
	time.sleep(10) 

Credits

Neeraj dhakad
1 project • 0 followers

Comments