Now a days digital technologies plays an important role in each and every person's life. Among several digital technologies I have choosen Internet Of Things and Machine Learning for this project. This project is Temperature anomaly detection where IOT is used to take the temperature reading from the temperature sensor as input and Machine Learning is used to find the anomaly of the temperature using the z-score analysis. Anomaly means deviating from the standard state. In this project when a anomaly in the temperature is observed, then immediately an alert nortification will be sent to the mobile as sms, telegram message and as a mail.
APPLICATIONS:
This project is used in the places where temperature has to be maintained at certain boundaries.
1. In manufacturing of medicines at Pharmaceutical companies
2. Many food factories
3. In Server Rooms.
to find how abnormally the temperature in our atmosphere changing.
VIDEO LINK OF THIS PROJECT:
STEPS TO BUILD PROJECT:
STEP_1:Set up the Bolt IOT cloud account 1.Open www.cloud.boltiot.com on your web browser and "SignUp” with your credentials.
2. Install BoltIOT app on your mobile and login with your credentials.
3. since no devices linked to your cloud account click on add device to link the bolt iot wifi module to cloud account
4.First, power on your Bolt Module provided in the kit, using a micro USB type android charging cable. Next, once the Device is powered ON the Blue LED on the ESP8266 Chip will start blinking steadily. When this happens click "READY" button shown in the right most Image below. You can swipe left to get there.
5. Turn on your location and turn off your mobile data
7. Next you have to connect the Bolt device to a local Wi-Fi Network which will be displayed by the app for you to select from. Make sure that the Wi-Fi does not have further verification code requirements except the password
8. after adding network click on connect button to add your bolt iot wifi moodule to your bolt cloud account then your module is connected to the cloud.
9. you can see the active of your device in mobile view as well as desktop view in below figures.
STEP-2: MAKE CONNECTIONS
Hardware Components:
1. Bolt IOT wifi module
2. LM35 Temperature sensor
3. Buzzer
4. Male to Female jumper wires
5. USB cable
6. Power source
HARDWARE CONNECTIONS: 1. Connect Bolt iot wifi module to some power supply using the USB cable
2. connect LM35 temperture sensor to the Bolt wifi module
- 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.
3. connect Buzzer to the Bolt wifi module
- negative pin of Buzzer to the Gnd
- positive pin of Buzzer to the A0(Analog input pin)
TOTAL CONFIGURATION:
STEP-3: SOFTWARE SET UP
A) TWILIO
Twilio is a third-party SMS functionality provider. It is a cloud communications platform as a service (PaaS) company. Twilio allows software developers to programmatically make and receive phone calls and also send and receive text messages using its web service APIs
CREATING AN ACCOUNT ON TWILIO:
Step 1: Open https://www.twilio.com/ in browser. Step 2: Click on Get a Free API Key button to sign up. Step 3: Fill all the necessary credentials in the sign up form.
Step 4: To verify they will ask for your phone number. Choose India as an option in the dropdown and then enter your phone number.
Step 5: Click on "Products" as shown on the screen below,
Step 6: Now enable the SMS services by clicking on two checkboxes for Programmable SMS and Phone Numbers as shown below.
Once you have done this, scroll to the bottom of the screen and click on "Continue".
Step 7: Now, you will need to give a name for your project. I have given the name as My Project. Click on "Continue" once you have entered the project name.
Step 8: Click on "Skip this step" when it asks you to Invite a Teammate.
Step 9: Your project should be created at this point. Click on "Project Info" to view the account credentials which is required for your projects.
Step 10: You can view the Account SID and Auth token on this page. The Auth token is not visible by default, you can click on "view" button to make the Auth token visible as shown below. Copy both and save them somewhere securely.
Step 11: From the drop-down menu, choose "Programmable SMS". Now click on Get Started button to generate phone number.
Step 12: Click on Get a number button.
Step 13: Then a popup will appear. Click on Choose this number button.
Step 14: Then a popup will appear which will have the final number. Copy this number and save to notepad for future references.
That's it. You have successfully created the account on Twilio.
D)MAILGUN
Mailgun 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
CREATING AN ACCOUNT IN MAILGUN:
Step 1: Open https://www.mailgun.com/ in browser.
Step 2: Click on Sign Up button.
Step 3: Fill all the necessary credentials in SIGN UP form. Make sure you are unchecking the payment option.
Step 4: You will get a verification mail having a link. Click on that link to verify your mail.
Step 5: To verify they will ask for phone number. Choose India as an option in the dropdown and then enter your phone number.
Step 6: After verification, scroll down to Sandbox Domain section. Click on Add Recipient button.
Step 7: Click on Invite New Recipient button.
Step 8: Enter the Receipient Email ID. In this case enter your Email ID.
Step 9: After adding Email ID a new sandbox will be generated. Click on the ID of the newly generated sandbox.
Step 10: The new screen will have all the necessary credentials that you want for sending an email. Copy all this credentials and save in the notepad.
That's it. You have successfully created the account on Mailgun.
E)TELEGRAM
Telegram is a cloud-based instant messaging and voice over IP service. Telegram client apps are available for Android, iOS, Windows Phone, Windows NT, macOS and Linux.Users can send messages and exchange photos, videos, stickers, audio and files of any type.
Telegram Channel:
A channel is to Telegram what Groups are to Whatsapp. Channels are a tool for broadcasting your messages to large audiences. They can have an unlimited number of subscribers, they can be public with a permanent URL and each post in a channel has its own view counter.
Bot:
Bots are third-party applications that run inside Telegram. Users can interact with bots by sending them messages, commands and requests.Steps to sign-up for/sign-in to Telegram
- Go to the Playstore or App Store on your phone and search for Telegram.
- Download and Install the latest version of Telegram app.
- Signup for a new account or sign in to Telegram by providing your mobile number.
- Telegram will call you OR send you a SMS to verify your mobile number.
- Get your account verified and approve any permissions if required.
- You will be logged in to Telegram and will be shown a screen similar to the one below. (It's okay if it differs from the screenshot below)
You have successfully installed Telegram and setup your account.
Steps to create a channel- Go to the home screen of the Telegram app.
- Swipe from the left side to reveal the menu.
- Click on "New Channel".
- It will ask you for a Name and Description for your channel. Give a suitable name and description. Adding photo is optional.
- In the next screen set the channel as Public.
- On the same screen, it will ask you to enter a permanent link for your channel. You can use lowercase letters and numbers 0-9 to create the channel link.
- Please note that the channel link name is global and you will be able to create a channel link only if the link name is available. The channel link name is something similar to an email address, i.e. only one unique email ID can exist at one time.
- You can keep the channel link name as temperature_alert_ followed by your email ID. Example, if my email ID is developer@boltiot.com, I can keep the channel link name as temperature_alert_developer_boltiot_com(I have removed the @ and. symbol since it was not allowed and replaced it with an underscore _). This is just a suggestion. You can keep the channel link name as you like as long as it is valid.
- Keep a note of this Channel permanent link name. It will be required later on to send messages to this channel. For example, the channel link name in the screenshot below is "temperature_alert".
- Congratulations! You have created a new channel. You can also view this channel from your app home screen.
- You can click on the channel name at the top to view more details about it.
- Telegram Channel creation is successful..
Telegram allows us to create a new bot with the help of a bot called as "BotFather". It is a single bot to create and manage other bots.
- On the Home screen of the app, click on the search icon on the top right and type in "botfather".
- In the search results, click on the correct result for "BotFather" as shown below. The correct "BotFather" will have a blue tick mark next to its name. This will take you to a chat window with the "BotFather".
- The chat will have a few items already and will display you a list of commands to get started.
- Since we need to create a new Bot, go ahead and type in "/newbot" in the window.
- It will ask you to type in few more details like Bot name and Bot username.
- When your bot is created successfully, you will be shown a screen similar to the one below and will contain the Bot Token. This token is used to control your Bot as well as send messages on behalf of it. Please keep this token secure as it will allow anyone to access your Bot.
- In the screenshot above, the Bot token is "894346529:AAhuJ2XJQy5dlEtLYF0sc0Z_qu0fSqihSSc". Please save it as telegram_bot_id. The telegram_bot_id is saved as "bot" followed by the bot token. So, in this example, the telegram_bot_id will be "bot894346529:AAhuJ2XJQy5dlEtLYF0sc0Z_qu0fSqihSSc".
- The telegram_bot_id will be used in the python code to send messages. The python code will be taught in the later lessons.
- Also, please be careful when saving the Bot ID. You may get confused between 0 and O, I and 1 etc. as they are similar looking.
- Congratulations, you have created a new Bot. Now, we will need to add it to the channel we have created previously so that we can send alerts.
- From the App home screen, open the channel we have created earlier.
- In the screen, click on the channel name on the top to open the information for the Channel.
- Click on the Administrators button so that we can add the newly created bot to the channel.
- Search for the bot that we have created using the Bot's username. Once you have found the correct Bot click on it to add it to the channel. Please make sure you have clicked on the Administrators button in the previous step as this will allow our bot to post to the channel.
- You will be asked to confirm the rights for the bot. Press on the tick mark on the top right to continue adding the bot to the channel. Make sure that the bot has the rights to "Post Messages".
- You will now see the newly created Bot in the list of administrators for the channel.
- Bot is created successfully.
F) Generating API in Bolt cloud :We have to generate Bolt API key as the following in Bolt cloud:
1. create a new file named conf.py in python programming language
sudo nano conf.py
2. write the code inside the file as,
""" TWILIO CONFIGURATION """
SSID = 'You can find SSID in your Twilio Dashboard'
AUTH_TOKEN = 'You can find on your Twilio Dashboard'
FROM_NUMBER='This is the no generated by Twilio.You can find this Twilio Dashboard'
TO_NUMBER = 'This is your number. Make sure you are adding +91 in beginning'
""" MAILGUN CONFIGURATION """
MAILGUN_API_KEY = "You can find in your Mailgun dashboard"
SANDBOX_URL = "You can find in your Mailgun dashboard"
SENDER_MAIL = "Mail address of Mailgun. You can find in your dashboard"
RECIPIENT_MAIL = "Write Your mail"
""" TELEGRAM CONFIGURATION """
bolt_api_key = "XXXX" # This is your Bolt Cloud API key
device_id = "XXXX" # This is the device ID and will be similar to BOLTXXXX
# where XXXX is some numbers
telegram_chat_id = "@XXXX" # This is the channel ID of the created Telegram channel.
# Paste after @ symbol.
telegram_bot_id = "botXXXX" # This is the bot ID of the created Telegram Bot.
# Paste after bot text.
""" BOLT IOT CONFIGURATION """
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
Note: You have to replace all the above value with your credentials. You can find the first four values in Twilio dashboard and the last two in Bolt Cloud dashboard.You can change the FRAME_SIZE and MUL_FACTOR as per your convinienceB) anomaly file
ALGORITHM:
step-1: Fetch the latest sensor value from the Bolt device.
step-2: Store the sensor value in a list, that will be used for computing z-score.
step-3: Compute the z-score and upper and lower threshold bounds for normal and anomalous readings.
step-4: Check if the sensor reading is within the range for normal readings.
step-5:If it is not in range, send the SMS.
step-6: Wait for 8 seconds.
step-7: Repeat from step 1.
Z-SCORE ANALYSIS:
Z-score analysis is used for anomaly detection. Anomaly here means a variable's value (light intensity of the surroundings) going beyond a certain range of values. 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.
1. create a file named anomaly.py in python programming language
sudo nano anomaly.py
2. import the moudles json, time, math, statistics, conf, Bolt, Sms
import json,time,math,statistics,conf
from boltiot import Bolt,Sms,Email
- json module is used inthis project to convert string to a dictionary
- time module is used inthis project to sleep the process for 5 seconds and continue again
- math module is used in this project to calculate power and square root
- statistics module is used in this project to calculate mean of the data
- cong file is imported to access the bolt device and to send the alert nortifications to the social media
- sms module is used to a message to respective mobile number
- bolt module is used to access bolt iot wifi module
3. function for sending a message to the telegram app
def send_telegram_message(message): """Sends message via Telegram"""
url = "https://api.telegram.org/" + conf.telegram_bot_id + "/sendMessage"
data = { "chat_id": conf.telegram_chat_id,
"text": message
}
try:
response = requests.request( "GET",
url,
params=data
)
print("This is the Telegram response")
print(response.text)
telegram_data = json.loads(response.text)
return telegram_data["ok"]
except Exception as e:
print("An error occurred in sending the alert message via Telegram")
print(e)
return False
4.define a function which calculates the Z-score and the using the Z-score calculates the boundaries required for anomaly detection.
def compute_bounds(history_data,frame_size,factor):
above function 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.The next code is used to initialize the Bolt, SMS and mailer variables, which we will use to collect data and send SMS, mail 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.
mybolt = Bolt(conf.API_KEY, conf.DEVICE_ID)
sms = Sms(conf.SSID, conf.AUTH_TOKEN, conf.TO_NUMBER, conf.FROM_NUMBER)
mailer = Email(email_conf.MAILGUN_API_KEY, email_conf.SANDBOX_URL,email_conf.SENDER_EM AIL, email_conf.RECIPIENT_EMAIL)
client=Client(conf.SSID,conf.AUTH_TOKEN)
history_data=[]
The following while loop contains the code required to run the algorithm of anomaly detection.
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(8)
continue
sensor_valuec= int(data['value'])
sensor_valuec = sensor_valuec/10.24
print ("The current sensor value is : "+str(sensor_valuec)
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(8)
continue
try:
if sensor_value > bound[0] :
buzz=mybolt.digitalWrite('1',"HIGH")
print(buzz)
print("Anomaly of the temperature occured because of increase in temperature sending an alert messages")
""" SMS """
print ("The temperature level increased suddenly. Sending an SMS")
response = sms.send_sms("The temperature has raised to:"+str(sensor_valuec)+"degree celcius")
print("This is the response ",response)
""" TELEGRAM """
message = "Alert! Sensor value has increased The current value is " + str(sensor_valuec)
telegram_status = send_telegram_message(message)
print("This is the Telegram status:", telegram_status)
""" MAIL """
print("Making request to Mailgun to send an email")
response = mailer.send_email("Alert","The Current temperature sensor value is " +str(sensor_valuec))
response_text = json.loads(response.text)
print("Response received from Mailgun is:"+str(response_text['message']))
print(message.sid)
elif sensor_value < bound[1]:
buzz=mybolt.digitalWrite('1',"HIGH")
print(buzz)
print("Anomaly of the temperature occured because of increase in temperature sending an alert messages")
""" SMS """
print ("The temperature level decreased suddenly. Sending an SMS")
response = sms.send_sms("The temperature has decreased to : "+str(sensor_valuec)+"degree celcius")
print("This is the response ",response)
""" TELEGRAM """
message = "Alert! Sensor value has decreased The current value is " + str(sensor_valuec)
telegram_status = send_telegram_message(message)
print("This is the Telegram status:", telegram_status)
""" MAIL """
print("Making request to Mailgun to send an email")
response = mailer.send_email("Alert","The Current temperature sensor value is " +str(sensor_valuec))
response_text = json.loads(response.text)
print("Response received from Mailgun is:"+str(response_text['message']))
history_data.append(sensor_value);
except Exception as e:
print ("Error",e)
time.sleep(5)
whole code
import json,time,math,statistics,conf,requests
from boltiot import Bolt,Sms,Email
def send_telegram_message(message):
url = "https://api.telegram.org/" + conf.telegram_bot_id + "/sendMessage"
data = { "chat_id": conf.telegram_chat_id,
"text": message
}
try:
response = requests.request(
"GET",
url,
params=data
)
print("This is the Telegram response")
print(response.text)
telegram_data = json.loads(response.text)
return telegram_data["ok"]
except Exception as e:
print("An error occurred in sending the alert message via Telegram")
print(e)
return False
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=0
Zn = factor*math.sqrt(history_data[frame_size-1])+Zn
Low_bound = history_data[frame_size-1]-Zn
High_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)
mailer = Email(conf.MAILGUN_API_KEY, conf.SANDBOX_URL,conf.SENDER_MAIL,conf.RECIPIENT_MAIL)
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(5)
continue
sensor_value= int(data['value'])
sensor_valuec = sensor_value/10.24
print ("The current sensor value is : "+str(sensor_valuec))
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] :
buzz=mybolt.digitalWrite('1',"HIGH")
print(buzz)
print("Anomaly of the temperature occured because of increase in temperature sending an alert messages")
""" SMS """
print ("The temperature level increased suddenly. Sending an SMS")
response = sms.send_sms("The temperature has raised to:"+str(sensor_valuec)+"degree celcius")
print("This is the response ",response)
""" TELEGRAM """
message = "Alert! Sensor value has increased The current value is " + str(sensor_valuec)
telegram_status = send_telegram_message(message)
print("This is the Telegram status:", telegram_status)
""" MAIL """
print("Making request to Mailgun to send an email")
response = mailer.send_email("Alert","The Current temperature sensor value is " +str(sensor_valuec))
response_text = json.loads(response.text)
print("Response received from Mailgun is:"+str(response_text['message']))
print(message.sid)
elif sensor_value < bound[1]:
buzz=mybolt.digitalWrite('1',"HIGH")
print(buzz)
print("Anomaly of the temperature occured because of increase in temperature sending an alert messages")
""" SMS """
print ("The temperature level decreased suddenly. Sending an SMS")
response = sms.send_sms("The temperature has decreased to : "+str(sensor_valuec)+"degree celcius")
print("This is the response ",response)
""" TELEGRAM """
message = "Alert! Sensor value has decreased The current value is " + str(sensor_valuec)
telegram_status = send_telegram_message(message)
print("This is the Telegram status:", telegram_status)
""" MAIL """
print("Making request to Mailgun to send an email")
response = mailer.send_email("Alert","The Current temperature sensor value is " +str(sensor_valuec))
response_text = json.loads(response.text)
print("Response received from Mailgun is:"+str(response_text['message']))
history_data.append(sensor_value);
except Exception as e:
print ("Error",e)
time.sleep(5)
Comments