This Project monitors the temperature in the Cooling Chamber of the Tablet Storage. It checks if the temperature is in the specified limits and notifies the user if there is any deviation in it.
It is basically implemented in 3 stages-
- Stage 1- Record the values of temperature in the chamber for a few hours. Implement Machine learning Algorithm of Polynomial regression, for predicting Future values and decide the boundaries for the temperature within the chamber.
- Stage 2- Fetch the Temperature value for every 10 sec and send a notification(via e-mail) to the user, if the temperature goes beyond the temperature thresholds(that we had set in Stage 1).
- Stage 3- Make the system "Self Reliant" - Use z-score analysis, to detect any anomaly and notify the user.
Protecting the medicinal drugs and tablets from equipment failure or malfunction is crucial in maintaining compliance and quality standards.
STAGE - 1In this stage, we will record the values of temperature sensor for about 2 hr. We will also implement Polynomial Regression Model, to predict the temperature readings of some future points. Based on the results, we will find threshold boundary values for temperature.
1.1 Build the circuit
LM35 sensor is a commonly used temperature reading sensor. As the temperature increases, voltage across the sensor increases and vice versa.
T = r / 10.24, r-> Reading Of LM35 sensor, T-> Temperature(in degree centigrade)
Given below are the circuit connections of LM35-Temperature Sensor with the bolt module.
Connecting LM35 to Bolt Module-
1)Hold LM35 such that you can read LM35 written on it.
2)Leftmost pin from this position, is connected to Vcc(+5v on Bolt Module).
3)Middle pin is connected to Output(i.e A0 on Bolt Module)
4)Rightmost pin is connected to Ground(GND on Bolt Module)
Connect the Bolt Wifi Module to power supply via Micro USB Cable.
1.2 Create a product and link it to our device
Go to https://cloud.boltiot.com/ and then follow the steps below:
1.3 Implement Polynomial regression algorithm
- Polynomial regression is a machine learning algorithm, in which we find a polynomial equation, which best mimics a data set.
The polynomial equation is of the form-
y = f(x), where f(x) is a function of sum of different powers of x.
Here, we use "time" as x coordinate. Then we can use the acquired polynomial equation to predict future temperature values.
- Steps- 1. Click on configure this product.
2. Click on A0 pin and give it a name-"temp". (As this pin will take Temperature as input)
Select the Data Collection Rate as per your requirement.(I have selected 5 min)
3. Click on Code. Choose.js as the extension file and write the following code.
setChartLibrary('google-chart')
setChartTitle('Polynomial Regression')
setChartType('predictionGraph')
setAxisName('time_stamp','temp')
mul(0.0977)
plotChart('time_stamp','temp')
Note- a) I have used google charts library for implementing polynomial regression.
b) setChartLibrary() - chooses the google chart library, setChartTitle() - sets the title to the chart setChartType() - sets the chart type to prediction graph(i.e it chooses polynomial regression) setAxisName()- sets names to x and y axis(which is time and temperature values respectively) mul(0.0977) - multiplies the temperature values by 0.0977, to convert the temperature sensor readings to temperature in degree centigrade. plotChart() - Finally plots the chart.
4. Save the configuration.
5. Deploy the Hardware Configuration and click on "View this Device".
6.Now Keep the Sensor in the Cooling Chamber for about 2 hours (with lid closed) and record the values. (P.S - I have used a closed tray with some ice cubes in it as chamber, but you may keep the setup inside refrigerator).
- Understanding the Prediction Model-
Prediction points: This number tells the Visualizer how many future data points need to be predicted. So if you set the product to collect data every 5 minutes, and select 4 prediction points, the Visualizer will predict the trend and show 4 points up to 30 minutes into the future.
No. Polynomial coefficients: Polynomial Visualizer processes the given input time-dependent data, and outputs the coefficients of the function of the form:
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.
Frame Size: These are the number of previous data points the Visualizer will use to predict the trend of the data. For example, if you set this value to 3, the Visualizer will use the previous 3 points to predict the trend.
The prediction history graph helps you tune the machine learning model. You 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.
This is useful when there are certain requirements like - the temperature of the tablets should never remain between -33 and -30 degrees for longer than 20 minutes at a time. Using the prediction data, we can take early action, whenever the graph predicted that the temperature would be maintained within the -33 and -30 degrees Celsius range for longer than 20 minutes.
1.4Set Boundaries for Temperature inside chamber
By looking at the data, we can set Threshold values at 26*C(sensor value - 277) and 34*C (sensor value - 351)
STAGE-2We will write a python code to fetch the temperature data, every 10 seconds, and send out an email alert (by Mailgun), if the temperature goes beyond the temperature thresholds we decided in 1.4 section.
So, if there is any problem in the chamber (due to which the temperature has changed), it can be detected easily and repaired.
2.1 Setting Up Mailgun
I have used Mailgun- It is an Email automation service. It has a very powerful set of inbuilt functions for sending emails. Developers can process their email with the help of Mailgun API.
Step 1- Open https://www.mailgun.com/ and sign up for the free trial account.
Step 2- Follow the following Steps-
2.2 Serverand SSH terminal Software
I have used Digital Ocean Cloud Service in this project.
DigitalOcean is a cloud computing vendor that offers an Infrastructure as a Service (IaaS) platform for software developers.
- Click on this link- https://www.digitalocean.com/ and Make an account and get it verified. Create a droplet.(Make sure you choose the data centre nearest to you)
- You will get the login credentials and server IP over the mail.
I have used "putty" software to SSH into the server. SSH is a network protocol that will allow us to remotely access computer over the internet.
- Click on-https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html and choose the Windows installer from the Package files list according to your windows architecture.
- After installing putty, login through the credentials and ip address of the droplet, received over the mail. Change the new password and set the new one.
Just have a look, for login in putty.
2.3 Configuration file
We have to create a file to store all the credentials of Mailgun and Bolt IoT.
Step 1- Create a file "email_conf.py" in the terminal.
sudo nano email_conf.py
Step 2- The following is the configuration file-
MAILGUN_API_KEY = 'This is the private API key which you can find on your Mailgun Dashboard'
SANDBOX_URL= 'You can find this on your Mailgun Dashboard'
SENDER_EMAIL = 'This would be test@your SANDBOX_URL'
RECIPIENT_EMAIL = 'Enter your Email ID Here'
API_KEY = 'This is your Bolt Cloud account API key'
DEVICE_ID = 'This is the ID of your Bolt device'
Mailgun_API_KEY and SANDBOX_URL - Reference to determine this is explained in the video in the section 2.1.
The API key and Device ID of the Bolt module can be determined as follows:
- Open https://cloud.boltiot.com/ and connect your Bolt Device to the Bolt Cloud.
- The Bolt Device ID is boxed-
- Go to the API section to know the API Key.
2.4 Writing final python code
Algorithm for the code-
- Fetch the latest sensor value from the Bolt device.
- Check if the sensor value is in the range specified in our min and max values.
- If it is not in range, send the e-mail.
- Wait for 10 seconds.
- Repeat from step 1.
Final code is -
import email_conf #import the configuration file
from boltiot import Email, Bolt #import the boltiot module and email from the Bolt python library
import json, time #import various python libraries
minimum_limit = 277 #the minimum threshold of temperature
maximum_limit = 351 #the maximum threshold of temperature
mybolt = Bolt(email_conf.API_KEY, email_conf.DEVICE_ID) #To identify your bolt device
mailer = Email(email_conf.MAILGUN_API_KEY, email_conf.SANDBOX_URL, email_conf.SENDER_EMAIL, email_conf.RECIPIENT_EMAIL) # For sending mails through the mailgun account
while True:
print ("Reading sensor value")
response = mybolt.analogRead('A0') #fetch the sensor value
data = json.loads(response) #Retrieve the input data in json format
print ("Sensor value is: " + str(data['value']))
try:
sensor_value = int(data['value'])
#Check if it is in the specified range
if sensor_value > maximum_limit or sensor_value < minimum_limittry:
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)
Some Terms-
- json - json is a python library used for handling all operations on JSON objects (a data communication format widely used on the Internet for sending/receiving data between a client and server).
- The send_email function takes two parameters. First one is
Subject of the Mail
and the other one isMessage content
. - The response from Mailgun will be stored inside the `response` variable.The text parameter from the response extracted and converted to json and the message from it is printed.
- `time.sleep(10)` puts the program execution on hold for 10 seconds.
2.5 Demonstration
For testing the result, I have opened the chamber and brought a glass of hot water near the sensor (so that the temperature crosses the threshold). See the video -
The putty terminal screenshot-
Screenshot of mail received-
In stage -2, I had manually set the threshold bounds for the temperature sensor. But in this stage, I will be using the Z-score algorithm to dynamically change the bounds at which an alert is sent.
A gist of this stage is -
When the chamber is working fine, the temperature change will be slow, and the bounds for the system will change to match this change. But when there is any system failure or any problem, the temperature in the room will change suddenly. Because of this, the system will detect an anomaly and quickly alert user that there is some problem.
3.1 Z-score analysis
Z-score analysis is an algorithm used for anomaly detection. Here, the sudden change of temperature beyond the (threshold) range values, is an anomaly.The range of values is called bounds (upper bound and lower bound). These bounds are calculated using the input values, frame size and multiplication factor. The frame size is the minimum number of input values needed for Z-score analysis and the multiplication factor determines the closeness of the bounds to the input values curve.
Given above is the formula to calculate the bounds. Here the input is represented as 'Vi', 'r' denotes the frame size and 'C' is the multiplication factor. Firstly we calculate the mean (Mn) of the input values (for every new input, the mean is calculated again). The variation of each input value (from the mean) is given as (Vi - Mn)^2. The Z-score (Zn) is calculated as shown above ( square root of the mean of the variation of each input value multiplied by the multiplication factor). The bounds are represented as 'Tn' and the upper bound is calculated as (Vi + Zn) and the lower bound is calculated as (Vi - Zn).
The frame size and multiplication factor are determined using trial-and-error method.
3.2 Modified Configuration File
MAILGUN_API_KEY = 'This is the private API key which you can find on your Mailgun Dashboard'
SANDBOX_URL= 'You can find this on your Mailgun Dashboard'
SENDER_EMAIL = 'This would be test@your SANDBOX_URL'
RECIPIENT_EMAIL = 'Enter your Email ID Here'
API_KEY = 'This is your Bolt Cloud account API key'
DEVICE_ID = 'This is the ID of your Bolt device'
FRAME_SIZE = 10
MUL_FACTOR = 6
Frame size and multiplication factor are guessed as 10 and 6 and then tuned as per requirements.
3.3 Python Code for anomaly detection
import email_conf #import the configuration file
from boltiot import Email, Bolt #import the boltiot module from the Bolt python library
import json, time, math, statistics #import various python libraries
#//---------FUNCTION TO COMPUTE BOUNDS OR Z SCORE ANALYSIS------------//
def compute_bounds(history_data,frame_size,factor):
#//Function to compute bounds
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] # //Returns Low Bound and High Bound
mybolt = Bolt(email_conf.API_KEY, email_conf.DEVICE_ID) # //To identify your bolt device
mailer = Email(email_conf.MAILGUN_API_KEY, email_conf.SANDBOX_URL, email_conf.SENDER_EMAIL, email_conf.RECIPIENT_EMAIL) # //To identify Mailgun account
history_data=[] # //Array of input values from LM35
#//---------------------READ INPUT FROM LM35--------------------------//
while True:
response = mybolt.analogRead('A0') #//Read input from LM35 at A0 pin
data = json.loads(response) # //Retrieve the input data in json format
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']) # //store current input value in variable
except e:
print("There was an error while parsing the response: ",e)
continue
#//----------------PERFORMING Z SCORE ANALYSIS--------------------------//
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
#//-----------DETECTING ANOMALY AND SENDING ALERTS--------------//
try:
if sensor_value > bound[0] : # //If input crosses upper bound
print ("The temperature has increased suddenly. Sending an E-mail.")
response = mailer.send_email("Alert", "The temperature has suddenly increased! ")
print("This is the response ",response)
elif sensor_value < bound[1]: # //If input crosses lower bound
print ("The light level decreased suddenly. Sending an Email.")
response = mailer.send_email("Alert", "The temperature has suddenly decreased! ")
print("This is the response ",response)
history_data.append(sensor_value); # //Append each new input to array history_data[]
except Exception as e:
print ("Error",e)
time.sleep(10) # //Wait for 10 seconds
3.4 Demonstration
When a glass of hot water is brought near the sensor, there is a sudden change in temperature and anomaly in temperature value is detected. The frame size and multiplication factor has been tuned in that way.
3.5 Output Explanation
As I have set the frame size as 10, it will not calculate z-score until it fetches 10 sensor values.
After 100 sec (10 seconds delay with a frame size of 10), it will start calculating z-score and check if there is an anomaly.After bringing a glass of hot water, when there is a sudden change in temperature, an anomaly is detected.
Screenshot of mail received-
Thus, this is a kind of self-reliant system in anomaly detection.
Comments