Shraddha Chitte
Published © GPL3+

Automatic LED Control

Not at home and the lights are still on? No worries.This project enables you to Auto-control lights based upon the brightness.

IntermediateFull instructions provided4 hours511
Automatic LED Control

Things used in this project

Hardware components

Bolt WiFi Module
Bolt IoT Bolt WiFi Module
×1
USB-A to Mini-USB Cable
USB-A to Mini-USB Cable
×1
5 mm LED: Red
5 mm LED: Red
×1
LDR, 5 Mohm
LDR, 5 Mohm
×1
Resistor 10k ohm
Resistor 10k ohm
×2
Jumper wires (generic)
Jumper wires (generic)
×1
Solderless Breadboard Half Size
Solderless Breadboard Half Size
×1

Software apps and online services

Bolt Cloud
Bolt IoT Bolt Cloud
VmWare Workstation

Story

Read more

Schematics

20190721_211643_vIPxSXDJXD.jpg

20190721_211623_h6UiG4faga.jpg

Connections and Set-Up

Connect the wires carefully. Do not switch on the bolt device before making the connections. ALL THE BEST!!

Prediction Graph

Demonstration

Code

anomaly_detection.py

Python
I have written this code using VmWare Workstation.Please take care of the spaces and tabs used.If care not taken, identation errors may occur.
import email_conf
from boltiot import Email,Bolt
import json, time, math, statistics
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)
  history_data=[]
  
  while True:
    response = mybolt.analogRead('A0')
    data= json.loads(response)
    if data['success'] != 1:
      print("There was an error while retrieving 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 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 calculate 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 ("The light intensity increased suddenly.Someone turned on the lights.")
        response = mybolt.digitalWrite('0','LOW')
      elif sensor_value < bound[1] :
        print ("The light intensity decreased suddenly.Someone turned off the lights.")
        response = mybolt.digitalWrite('0','HIGH')
      history_data.append(sensor_value);
    except Exception as e:
      print ("Error occured: ",e)
    time.sleep(10)
      

email_conf.py

Python
This is the code which contains the credentials.
API_KEY = "Your bolt API key"
DEVICE_ID = "Your bolt device id"
FRAME_SIZE = "10"
MUL_FACTOR = "6"

Graph

JavaScript
setChartLibrary('google-chart');
setChartType('predictionGraph');
setChartTitle('LED Auto-Control');
setAxisName('time_stamp','ana');
setCrosshair(true);
mul(1/10.24);
plotChart('time_stamp','ana');

Credits

Shraddha Chitte

Shraddha Chitte

4 projects • 1 follower

Comments