The Internet of Things is a lot like it sounds. The IoT is a growing system of billions of devices — or things — worldwide that connect to the internet and to each other through wireless networks.
IoT devices contain sensors and mini-computer processors that act on the data collected by the sensors via machine learning.
Machine learning is when computers learn in a similar way to humans — by collecting data from their surroundings — and it is what makes IoT devices smart. This data can help the machine learn your preferences and adjust itself accordingly. Machine learning is a type of artificial intelligence that helps computers learn without having to be programmed by someone.
This project is a real time application of IoT which involves building of an industry grade temperature monitoring system. The device sends an e-mail, SMS as well as a WhatsApp message to the user whenever the temperature crosses a particular threshold.
Also machine learning algorithms like Z-score analysis have been implemented which predicts the temperature and checks for anomaly, such that a required action may be taken before anything actually happens.
VideoHere is the video of the entire project.
Hardware- Bolt WiFi module is connected to LM35 temperature sensor through jumper wires.
- Using male to female wire we will connect the 3 pins of the LM35 to the Bolt WiFi Module as follows:
- VCC pin of the LM35 is connected to 5v of the Bolt WiFi module.
- Output pin of the LM35 is connected to A0 (Analog input pin) of the Bolt WiFi module.
- GND pin of the LM35 is connected to the GND.
Code for prediction of temperature for next 20 minutes. Here we use Google prediction graph. Coding is done in JavaScript.
setChartLibrary('google-chart');
setChartTitle('Temperature Prediction For Pharmaceuticals');
setChartType('predictionGraph');
setAxisName('Time','Temperature');
setCrosshair('True');
mul(0.097);
plotChart('time_stamp','temp_monitor');
- Make the same circuit connections as described before.
- We can create a new folder and enter it using the following command.
mkdir Anomaly_Detection
cd Anomaly_Detection
- Create a configurations file for this project, using the following command
sudo nano conf.py
- After the editor is open, type in the following configuration parameters
SSID = 'AC2010b912941XXXXXXXXXXXXXXXX1ac73ae'
AUTH_TOKEN = '82757XXXXXXXXXXXXXXXXXXXXXXX9a6ca56'
FROM_NUMBER = '+1205XXXXXXXX2'
TO_NUMBER = '+919048326345'
API_KEY = '471a1bXXXXXXXXXXXXXXXXXX07995a2'
DEVICE_ID = 'BOLT1XXXXXX'
FRAME_SIZE = 10
MUL_FACTOR = 6
FROM_WHATSAPP = 'whatsapp:+141XXXXX6'
TO_WHATSAPP = 'whatsapp:+919XXXXX345'
MAILGUN_API_KEY = '5c0e9bac55533XXXXXXXXXXXXXXXX3a-fdb14dd6'
SANDBOX_URL= 'sandbox339XXXXXXXXXXXXXX.mailgun.org'
SENDER_EMAIL = 'test@sandbox33966eeXXXXXXXXXX80.mailgun.org'
RECIPIENT_EMAIL = 'virinchi1XXXXX.com'
Note: We have to replace all the above value with our credentials.
- Now create one more file named anomaly_detection1.py, using the following command
sudo nano anomaly_detection1.py
This file will contain the main code.
- We shall start the code with the imports.
import conf, json, time, math, statistics
from boltiot import Sms, Bolt, Email
- The math and statistics libraries will be required for calculating the Z-score and the threshold boundaries.
def compute_bounds(history_data,frame_size,factor):
- The above line helps define a function, which takes 3 input variables: hisotry_data, frame_size and factor.
if len(history_data)<frame_size :
return None
if len(history_data)>frame_size :
del history_data[0:len(history_data)-frame_size]
- The above code checks whether enough data has been accumulated to calculate the Z-score, and if there is too much data, then the code deletes the older data.
Mn=statistics.mean(history_data)
- The above code calculates the mean (Mn) value of the collected data points.
Variance=0
for data in history_data :
Variance += math.pow((data-Mn),2)
- This code helps to calculate the Variance of the data points.
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]
- Here we calculate the Z score (Zn) for the data and use it to calculate the upper and lower threshold bounds required to check if a new data point is normal or anomalous.
mybolt = Bolt(conf.API_KEY, conf.DEVICE_ID)
sms = Sms(conf.SSID, conf.AUTH_TOKEN, conf.TO_NUMBER, conf.FROM_NUMBER)
sms_whatsapp = Sms(conf.SSID, conf.AUTH_TOKEN, conf.TO_WHATSAPP, conf.FROM_WHATSAPP)
mailer = Email(conf.MAILGUN_API_KEY, conf.SANDBOX_URL, conf.SENDER_EMAIL, conf.RECIPIENT_EMAIL)history_data=[]
- The above code is used to initialize the Bolt and SMS variables, which we will use to collect data and send SMS alerts. Here we also initialize an empty list with the name 'history_data' which we will use to store older data, so that we can calculate the Z-score.
while True:
response = mybolt.analogRead('A0')
response1 = mybolt.analogRead('A0')
response2 = 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(5)
continue
sensor_value1 = int(data['value'])
sensor_value1 = sensor_value1/10.24
print ("The current temperature of your refrigerator is "+ str(sensor_value1)+" degree celsius. And the sensor Value is "+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(5)
continue
try:
if sensor_value > bound[0] :
sensor_value1 = sensor_value/10.24
print ("The temperature level has INCREASED suddenly.Sending SMS")
response = sms.send_sms("Someone Opened the fridge door. The current temperature is " + str(sensor_value1)+ " degree celsius")
response1 = mailer.send_email("ALERT", "The temperature of your refrigarator has been INCREASED suddenly because someone opened the fridge door. The current temperature is " + str(sensor_value1)+" degree celsius")
response2 = sms_whatsapp.send_sms("The temperature of your Refrigerator has been INCREASED suddenly. The current temperature sensor value is " + str(sensor_value1)+ " degree celsius")
print("This is the response for SMS ",response)
print("This is the response for EMAIL ",response1)
print("This is the response for WHATSAPP ",response2)
history_data.append(sensor_value);
except Exception as e:
print ("Error",e)
time.sleep(5)
- The above following while loop contains the code required to run the algorithm of anomaly detection.
- The entire code at a stretch.
import conf, json, time, math, statistics
from boltiot import Sms, 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(conf.API_KEY, conf.DEVICE_ID)
sms = Sms(conf.SSID, conf.AUTH_TOKEN, conf.TO_NUMBER, conf.FROM_NUMBER)
sms_whatsapp = Sms(conf.SSID, conf.AUTH_TOKEN, conf.TO_WHATSAPP, conf.FROM_WHATSAPP)
mailer = Email(conf.MAILGUN_API_KEY, conf.SANDBOX_URL, conf.SENDER_EMAIL, conf.RECIPIENT_EMAIL)
history_data=[]
while True:
response = mybolt.analogRead('A0')
response1 = mybolt.analogRead('A0')
response2 = 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(5)
continue
sensor_value1 = int(data['value'])
sensor_value1 = sensor_value1/10.24
print ("The current temperature of your refrigerator is "+ str(sensor_value1)+" degree celsius. And the sensor Value is "+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(5)
continue
try:
if sensor_value > bound[0] :
sensor_value1 = sensor_value/10.24
print ("The temperature level has INCREASED suddenly.Sending SMS")
response = sms.send_sms("Someone Opened the fridge door. The current temperature is " + str(sensor_value1)+ " degree celsius")
response1 = mailer.send_email("ALERT", "The temperature of your refrigarator has been INCREASED suddenly because someone opened the fridge door. The current temperature is " + str(sensor_value1)+" degree celsius")
response2 = sms_whatsapp.send_sms("The temperature of your Refrigerator has been INCREASED suddenly. The current temperature sensor value is " + str(sensor_value1)+ " degree celsius")
print("This is the response for SMS ",response)
print("This is the response for EMAIL ",response1)
print("This is the response for WHATSAPP ",response2)
history_data.append(sensor_value);
except Exception as e:
print ("Error",e)
time.sleep(5)
Step 3: Project OutputNow that we have written the code for anomaly detection lets run the code.Once that is done, run the anomaly detection code using the following command:
sudo python3 anomaly_detection1.py
An email, SMS and a WhatsApp message will also be send simultaneously.
That's it! We are done with our Industry grade temperature monitoring system.
Thank you.
Comments