In this project, I have tried to switch on the led as the room lights goes down.I have use python to control the led and to retrieve data from bolt cloud.Machine learning technique, z-score analysis, has been used to compute the higher bounds and lower bounds of the data.If the sensor value goes out of the bounds then an email is sent to my mail and LED is switched on or off depending on the situation.
Z-SCOREANALYSIS:
In z score analysis the higher and low bound keeps changing with the data so that anomaly can be detected even if the anomalous value is below the previous noted values.It has three factors:
Mn=mean of r values
Zn=z score
Tn=thresholds
HARDWARECONNECTIONS:
Breadboard is divided into two halves all the points in line in one half are connected to each other and two points on two different lines or different halves are not connected to each other. On the bread board, LDR is connected in series with 10k resistor.The leg in common between LDR and the resistor is connected to the 'A0' pin of the bolt through the yellow wire.The other leg of the LDR is connected to the 3v3 pin through green wire.The resistor is connected to the LED through blue wire.This common line point between resistor and the LED is connected to the ground pin of the bolt.The other leg(longer leg) is connected to the '0' GPIO pin of bolt.
SOFTWARE:The software is divided into two python programme files.
1)email_conf.-It contains the details of my mailgun account, my bolt device name, api keys and multiplication factor and frame size for z score analysis.
API_KEY="XXXXX"
DEVICE_ID="BOLTXXXX"
MAILGUN_API_KEY="XXXXX'
SANDBOX_URL="XXXX"
SENDER_EMAIL="XXXX"
RECIPIENT_EMAIL="XXXX"
MUL_FACTOR=2
FRAME_SIZE=10
2)led_control_by_sensor:this is main code to implement the project i.e. to control LED, get sensor data and send email if sensor value crossed the bounds.
import email_conf,json,time,math,statistics
from boltiot import Bolt,Email
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]
mybolt = Bolt(email_conf.API_KEY, email_conf.DEVICE_ID)
email=Email(email_conf.MAILGUN_API_KEY,email_conf.SANDBOX_URL,email_conf.SENDER_EMAIL, email_conf.RECIPIENT_EMAIL)
history_data=[]
while True:
response = mybolt.analogRead('A0')
data = json.loads(response)
if data['success'] != 1:
print("There was an error while getting the data.")
print("This is the error:"+data['value'])
time.sleep(10)
continue
print ("This is the value "+data['value'])
sensor_value=0
try:
sensor_value = int(data['value'])
except Exception as e:
print("There was an error while parsing the response: ",e)
continue
bound = compute_bounds(history_data,email_conf.FRAME_SIZE,email_conf.MUL_FACTOR)
if not bound:
required_data_count=email_conf.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(10)
continue
try:
if sensor_value > bound[0] :
print ("light level increased suddenly. sending email.")
status=mybolt.analogWrite('0','0')
print("status of led is :",status)
response=email.send_email("alert","someone turned on the lights")
response=json.loads(response.text)
print("this is the response from mailgun"+str(response['message']))
elif sensor_value < bound[1] :
print ("light level decreased suddenly. sending email.")
status=mybolt.analogWrite('0','20')
print("status of led is :",status)
response=email.send_email("alert","someone turned off the lights")
response=json.loads(response.text)
print("this is the response from mailgun"+str(response['message']))
history_data.append(sensor_value);
except Exception as e:
print ("Error",e)
time.sleep(10)
In the above programme we have included the following:
time-time.sleep() function to pause programme for 10 seconds
json-json.loads() function to translate data from boltiot and mailgun
statistics-statistics.mean() function to calculate mean
math-math.pow() function to find square
Email-to create an object of it to send email
Bolt-to connect to the device
In the programme, the function compute_bounds calculates the thresholds and returns the. It takes the following arguements : list of sensor data, frame size and the multiplication factor.If the length of list is less than the frame size the it returns none as the required number of points are not available.It maintains the size of list to 10 by deleting the extra starting elements.
Then I created objects of Bolt and Email.An infinite loop is run and we get the data and if an error occurres it is shown.then it gets bounds by calling the compute_bounds functions.If the sensor value crosses the bounds the the led switched on or off depending on the situation and an email is sent and the response from the mailgun is shown.If value is within the bounds the it is simply added to the list.Finally the programme is paused for 10 seconds after each loop.
WORKINGMODEL:
Below images
shows the working project.
Comments