GOKULNATH M PRABHU
Published © GPL3+

Automatic Light Controlling System with Anomaly Detection

What if your lights worked automatically without you even bothering about them? As an added bonus you can detect light anomalies.

IntermediateFull instructions provided7 hours819

Things used in this project

Hardware components

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

Software apps and online services

Bolt Cloud
Bolt IoT Bolt Cloud
Snappy Ubuntu Core
Snappy Ubuntu Core
SMS Messaging API
Twilio SMS Messaging API

Story

Read more

Schematics

Circuit Connections

Code

auto_led.py

Python
This is the main code for this project.
NB: This code consists of an infinite loop which can be terminated when required using (Ctrl+X) keys
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) 

conf.py

Python
This is the python file in which the various credentials are stored. Make sure to import this file in the main code(auto_led.py
SSID = 'XXXXXXXXXXXXXXXX'  #Replace the X with the SSID found on your Twilio dashboard
AUTH_TOKEN = 'XXXXXXXXXXXXXX' #Replace X with auth token from your Twilio dashboard
TO_NUMBER = 'XXXXXXXXXX' #Replace X with the registered phone number
FROM_NUMBER = 'XXXXXXXXX' #Replace X with your Twilio phone number
DEVICE_ID = 'BOLTXXXXXX' #Replace this with your Bolt Device ID
API_KEY = 'XXXXXXXXXXXXXX' #Replace X with the API key of your Bolt Device

Credits

GOKULNATH M PRABHU

GOKULNATH M PRABHU

1 project • 0 followers

Comments