This project is build for the factories where light plays an important role in the production system and for the smart rooms where a person can turn on or off the light of the room only by clicking on the ON or OFF button from the smart phone. Also there is a major feature of this project is that if the person do not want to set the status of the light then he/she can leave them as it is only by clicking the "Machine" button from the app. With the use of this feature, the intensity of the light can be maintained at the value which is required according the light intensity of the surrounding and sends an alert email to the user when a sudden change occur in the light intensity of the surrounding.
HTML CODE FOR UI :-<!DOCTYPE html>
<html>
<head>
<title>Automatically_Controlled_Lighting_System</title>
<script type="text/javascript" src="https://cloud.boltiot.com/static/js/boltCommands.js"></script>
<script>setKey('{{ApiKey}}','{{Name}}');</script>
</head>
<body>
<center>
<div class="ON">
<button class="button button1" onclick="digitalWrite(0, 'HIGH');digitalWrite(1,'HIGH');">ON</button>
</div>
<div class="MACHINE">
<button class="button button3" onclick="digitalWrite(1, 'LOW');">MACHINE</button>
</div>
<div class="OFF">
<button class="button button2" onclick= "digitalWrite(0, 'LOW');digitalWrite(1, 'HIGH');">OFF</button>
</div>
</center>
<style type="text/css">
body {
margin:0;
padding:0;
}
div {
height: 33.3vh;
width: 60.9vh;
align-content: center;
}
.ON{background-color: #FFFFFF;}
.MACHINE{background-color: #FFFFFF;}
.OFF{background-color: #FFFFFF;}
.button {
border: none;
color: black;
padding: 16px 30px;
text-align: center;
width:210px;
height:70px;
text-decoration: none;
display: block;
font-size: 35px;
font-family: Impact, bold, sans-serif;
box-shadow: 1px 1px 6px 0px purple;
}
.button1 {background-color: #099924;
position: absolute;
top: 8%;
left:22%;
}
.button2 {background-color: #099924;
position: absolute;
top: 27%;
left:22%;
}
.button3 {background-color: #099924;
position: absolute;
top: 71%;
left:22%;
}
</style>
</body>
</html>
This code simply makes three buttons which are ON, OFF and MACHiNE and all of them have their unique functionalities. ON button is used to turn on the led and OFF button is used for turning off the led. And MACHINE button is used for making the whole system self dependent.
PYTHON CODE FOR MACHINE BUTTON :-print("----------Starting the program------------");
import confidentials,time,json,math,statistics
from boltiot import Bolt,Email
mybolt = Bolt(confidentials.api_key, confidentials.device_id)
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]
mailer = Email(confidentials.MAILGUN_API_KEY, confidentials.SANDBOX_URL, confidentials.SENDER_EMAIL, confidentials.RECIPIENT_EMAIL)
history_data=[]
while True :
if json.loads(mybolt.digitalRead('1'))['value'] == '0' :
time.sleep(10);
sensor_value = int(json.loads(mybolt.analogRead("A0"))["value"]);
print("Current sensor value is = " + str(sensor_value));
time.sleep(10);
bound = compute_bounds(history_data,confidentials.FRAME_SIZE,confidentials.MUL_FACTOR)
if not bound:
required_data_count=confidentials.FRAME_SIZE-len(history_data)
print("Not enough data to compute Z-score. Need ",required_data_count," more data points")
history_data.append(sensor_value);
time.sleep(10)
continue
try:
if sensor_value > bound[0] :
print ("The light intensity increased suddenly. Sending an EMAIL.")
print("Making request to Mailgun to send an email")
response = mailer.send_email("Alert", "Light intensity increased suddenly in the room. The current light intensity value is " +str(sensor_value))
response_text = json.loads(response.text)
print("Response received from Mailgun is: " + str(response_text['message']))
elif sensor_value < bound[1]:
print ("The light intensity decreased suddenly. Sending an EMAIL.")
print("Making request to Mailgun to send an email")
response = mailer.send_email("Alert", "Light intensity decreased suddenly in the room. The current light intensity value is " +str(sensor_value))
response_text = json.loads(response.text)
print("Response received from Mailgun is: " + str(response_text['message']))
history_data.append(sensor_value);
except Exception as e:
print ("Error",e)
if sensor_value < 100 :
print("Since the value is now below the thresholdlower value therefore, Now turning ON the LED");
print(mybolt.digitalWrite('0',"HIGH"));
time.sleep(10);
elif sensor_value > 900 :
print("Since sensor value is now above the thresholdupper value therefore, Now turing OFF the LED");
print(mybolt.digitalWrite('0',"LOW"));
time.sleep(10);
else :
sv = (sensor_value - 100)/3.13725;
sv = 255 - sv
print("Adjusting intensity of led according to the light in the room");
time.sleep(1);
print("intensity of led should be = " + str(sv));
print(mybolt.analogWrite('0',int(sv)));
time.sleep(10);
print("loop end");
else :
print("waiting till user click on MACHINE button");
time.sleep(10);
This code is written only for performing the functionality of the MACHINE button, which is the main feature of the project. This code simply uses the LDR sensor for fetching the light intensity present in the surrounding i.e in the room and then calculates the intensity of light required in the room by using the formula made by me(project maker) and then turn on the led with that much amount of intensity that is required according to the present situation of light intensity in the room. Also if required then the led can be turn off by itself. Also if any anamoly detected then it sends an email alert for that.
Formula Used To Calculate Led Intensity :-led_intensity = ( sensor_value - 100 )/3.13725
The intensity of led ranges from 0 to 255. As all know high intensity of led is required if there is very dark in the room and low intensity led light is required if there is enough light present in the room. Similarly this formula works. According to this formula there is no light required if the sensor_value reaches 900 or above and full intensity light will be required if the sensor_value is 100 or below i.e. for 100 or below the intensity of led will be 255 which is maximum intensity of the led and for the sensor_value above or equals to 900 then the intensity of the led should be 0 which nothing but turning OFF the led. And for sensor_value between 100 and 900 the intensity of led get adjusted according to the surroundings light intensity with the use of this formula.
Confidential File :-# This is confidentials file which contains all the confidentials information #
# for Bolt device details :-
api_key = '#############################'
device_id = "##########################"
# for using Mailgun :-
MAILGUN_API_KEY = "#####################"
SANDBOX_URL= '#########################'
SENDER_EMAIL = "########################"
RECIPIENT_EMAIL = "#####################"
# for the limits of the light intensity :-
thresholdLower = 100
thresholdUpper = 900
# for z-score analysis :-
FRAME_SIZE = 10
MUL_FACTOR = 6
Z- score Analysis :-The z-score analysis is used here for the anamoly detection i.e. whenever there is sudden change in the light intensity of the surrounding then an email alert will get triggered automatically to the user to inform for that. Here, sudden change means the sensor_value of the LDR goes beyond the upper bound or goes below the lower bound. values which is set at run time i.e. while running the program.
The upper bound and lower bound values can be set when by taking atleast ten(10) LDR sensor_value. Therefore for the first time when program runs the it take some time to collect ten sensor_value, so that, this z-score analysis can be used. After that the z-score analysis takes the last ten values i.e. the recent ten values of the LDR sensor_value.
Graph for z-score is like :-Use API key and bolt device id to connect wifi module with python code :-
Make product and link it to the the bolt device :-
And write your html code here in the configuration part :-
in Bolt iot wifi module :-
Total connection between module and breadboard :-
It require minimum 10 sensor_values to set the bounds.
2 : When ON or OFF button is clicked :-It show a message that it is waiting till the user click on the Machine button.
3 : When the Machine button is clicked :-It fetches light intensity of surrounding and adjust intensity of led according to that.
4 : When an Anamoly is detected using z-score analysis :-An email alert will be send using mailgun when there is sudden change in the light intensity of the surrounding.
Comments