ADITYA KUMAR SINGH
Published

Capstone Project

Email alert system for some thresholds temperature and Z-score analysis to detect the anomaly.

IntermediateFull instructions provided369
Capstone Project

Things used in this project

Hardware components

Bolt WiFi Module
Bolt IoT Bolt WiFi Module
×1
LM35 Temperature Sensor
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Micro-USB to USB Cable (Generic)
Micro-USB to USB Cable (Generic)
×1

Software apps and online services

Bolt Cloud
Bolt IoT Bolt Cloud
Bolt IoT Android App
Bolt IoT Android App
Digital Ocean
Mailgun
Snappy Ubuntu Core
Snappy Ubuntu Core

Story

Read more

Schematics

Objective A: Build the circuit for temperature monitoring system, using the Bolt and LM35 sensor

Hardware required :-
a. The Bolt WiFi module
b. 3 female to male wire
c. Temperature Sensor : LM35 sensor
Connecting the LM35 sensor to the Bolt :-
Step 1 : Hold the sensor in a manner such that we can read LM35 written on
it.
Step 2 : In this position, identify the pins of the sensor as VCC, output and
Gnd from left to right.
a. VCC is connected to the red wire.
b. Output is connected to the orange wire.
c. Gnd is connected to the brown wire.
Step 3 : Using male to female wire connect the 3 pins of the LM35 to the
Bolt Wifi Module as follows :
a. VCC pin of the LM35 connect to 5v of the Bolt WiFi module.
b. Output pin of the LM35 connects to A0 (Analog input pin) of the Bolt
WiFi module.
c. Gnd pin of the LM35 connects to the Gnd.

Objective B: Create a product on the Bolt Cloud, to monitor the data from the LM35

Create a product on the Bolt Cloud, to monitor the data from the LM35, and link it to your Bolt.

Understanding Polynomial Regreession

Parameters used in Polynomial Regression :-
A. Prediction points: This number tells the Visualizer how many future data points need to be predicted. By default, the Visualizer spaces the points with the data collection time in the hardware configuration of the product. So if you set the product to collect data every 5 minutes, and select 6 prediction points, the Visualizer will predict the trend and show 6 points up to 30 minutes into the future.

B. No. Polynomial coefficients: Polynomial Visualizer processes the given input time-dependent data, and outputs the coefficients of the function of the form image below which most closely resembles the trend in the input data. This number tells the Visualizer how many elements should be present in the function i.e. the value of n.

C. Frame Size: These are the number of previous data points the Visualizer will use to predict the trend of the data. For example, if we set this value to 5, the Visualizer will use the previous 5 points to predict the trend.

When we click on the predict button 2 graphs other than the data graph shown. These graphs are the prediction history, and the predicted data.
The prediction history graph helps us tune the machine learning model. We have to change the parameters below, to make it so that this graph most closely resembles the actual data. When this happens the predicted data, or the predicted future temperature will be most accurate.

Objective C: Write the product code, required to run the polynomial regression algorithm

Product Code, required to run the polynomial regression algorithm on the data sent by Bolt :-

setChartLibrary('google-chart');
setChartTitle('Polynomial Regression');
setChartType('predictionGraph');
setAxisName('time_stamp','temp');
mul(0.0977);
plotChart('time_stamp','temp');

The image of collected data is below.

After applying Polynomial Regression

Objective D: Record the temperature readings for about 2 hours.

Keep the temperature monitoring circuit inside your fridge with the door of the fridge closed, and let the system record the temperature readings for about 2 hours.

Understanding Email Alert System

We have written a program which will fetch the temperature data collected by Bolt and send an Email alert if the temperature value exceeds the defined temperature range.

Step 1: Connect the temperature monitoring circuit as we have done in the previous lesson -Hardware connections for temperature monitor.

Step 2: Login into the server by entering the IP address of your digital ocean droplet.

Step 3: After successful login, create a file named email_conf.py which will store all the credentials related to Mailgun. To create a new file type sudo nano email_conf.py in the terminal. After that write below code to save all the credentials in a single file.

MAILGUN_API_KEY = '***********'
SANDBOX_URL= '************'
SENDER_EMAIL = '************'
RECIPIENT_EMAIL = '***********'
API_KEY = '*************'
DEVICE_ID = '************'

Note: We have to replace all the above value with your credentials. We can find the first four value in Mailgun dashboard and the last two in Bolt Cloud dashboard.

We store all the credentials in a separate file since it is sensitive data which should not be shared with anyone. Hence it is a good practice to avoid using credentials in code directly. After replacing all the values, save the file using CTRL+X.

Step 4: Now create one more file named temp_email.py. To do so we have to type sudo nano temp_email.py in the terminal. Now we will write main code to collect the data from the Bolt and send Email if it crosses the threshold.

Algorithm :-
1. Fetch the latest sensor value from the Bolt device.
2. Check if the sensor value is in the range specified in our min and max values.
3. If it is not in range, send the Email.
4. Wait for 10 seconds.
5. Repeat from step 1.

Image of Email Alert send by Mailgun is below :

Objective H: Understanding Z- scrore analysis and detection of anomaly.

In Objective G we have written python code which can use z-score analysis to find out whether someone opened the door of fridge (anomaly) and print the line "Someone has opened the fridge door"

Step 1 : Make the circuit connection for Temperature Monitoring .

Step 2: Once you are done with that, log in to your digitalOcean droplet and create a new folder to hold the files for this topic.

Step 3 : Make Directory for the analysis.

mkdir Anomaly_Detection
cd Anomaly_Detection

Step 4 :
nano conf.py

API_KEY = ' ********** '
DEVICE_ID = ' ********* '
FRAME_SIZE = 10
MUL_FACTOR = 6

Note : We can find the DEVICE_ID and API_KEY in Bolt Cloud Dashboard.

We can set the FRAME_SIZE to 10, and the MUL_FACTOR to 3 for now. Once done, we can save the configurations file by pressing 'CTRL+x' .

Step 5: Now create one more file named anomaly_detection.py, using the following command

sudo nano anomaly_detection.py

This file will contain the main code. The algorithm for the code can be broken down into the following steps:

1. Fetch the latest sensor value from the Bolt device.
2. Store the sensor value in a list, that will be used for computing z-score.
3. Compute the z-score and upper and lower threshold bounds for normal and anomalous readings.
4. Check if the sensor reading is within the range for normal readings.
5. If it is not in range, print the line “Someone has opened the fridge door”.
6. Wait for 10 seconds.
7. Repeat from step 1.

The important parameters used in Z-score analysis such as Mn, Zn and Tn are given below :

Log of anomaly_detection.py and detection of anomaly.

Code

Objective E & F : Set boundaries for the temperature and send email alert .

Python
The image in Objective D is showing the record of data that is collected from LM35 sensor using Bolt WiFi module.

Note: For more precise boundary decision i have collected the data of 4
hours.

Boundaries :-
a. minimum_limit = 1
b. maximum_limit = 100

This would send an alert if the temperature reading goes below the minimum limit or goes above the maximum limit.

Python code will fetch the temperature every 10 seconds, and set out an email alert, if the temperature goes beyond the temperature thresholds we decided above
import email_conf
from boltiot import Email, Bolt
import json, time
minimum_limit = 1
maximum_limit = 100
mybolt = Bolt(email_conf.API_KEY, email_conf.DEVICE_ID)
mailer = Email(email_conf.MAILGUN_API_KEY, email_conf.SANDBOX_URL,email_conf.SENDER_EMAIL,          email_conf.RECIPIENT_EMAIL)
while True :
  print (Reading sensor value)
  response = mybolt.analogRead(A0)
  data = json.loads(response)
  print (Sensor value is:  +str(data[value]))
  try :
    sensor_value = int(data[value])
    if sensor_value > maximum_limit or sensor_value < minimum_limit :
      print(Making request to mailgun to send an email)
      response = mailer.send_email(Alert,  The Current temperature sensor value is 
      +str(sensor_value))
      response_text = json.loads(response.text)
      print(Response received from Mailgun is:  +str(response_text[message]))
  except Exception as e :
    print (Error occured: Below are the details)
    print (e)
  time.sleep(10)

Objective G: Z-score analysis and detection of anomaly

Python
Note: FRAME_SIZE = 10 , MUL_FACTOR = 6
import conf, json, math, statistics
from boltiot import Bolt
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(conf.API_KEY, 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 retriving 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, conf.FRAME_SIZE, conf.MUL_FACTOR)
  if not bound :
    required_data_count = 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(Someone has opened the fridge door)
    else :
      history_data.append(sensor_value)
  except Exception as e :
    print(Error, e)
  time.sleep(10)

Credits

ADITYA KUMAR SINGH
1 project • 0 followers
Contact

Comments

Please log in or sign up to comment.