With the advancement in technology, IoT is the "next big thing!" With increased demand of automation and smart appliances at industry level as well as at personal home level, IoT is serving the need to make the objects smart.
This project is an evident example of IoT in action. The project deals with the sensing of temperature with the help of LM35 in any industry level, say any pharmaceuticals company, which manufactures medicines. It is mandatory for the pharmaceuticals companies to keep track of the temperature they are using for the manufacturing.
This project deals with that problem. Whenever the temperature crosses a certain threshold, our Bolt Wifi Module will start its magic. The modules' job is to inform the owner about the anomaly.
In our case, the module is responsible for the generating an alarm through buzzer, and sending alert messages on message, email, and twitter.
Also, the machine learning algorithm, polynomial regression, is so used to predict the future temperature changes and values. This would help to manage any anomaly before it's happened.
I had a great learning time with this project, hope you'll have too!
Steps for Building This ProjectHardware SectionStep 1. Hardware Components
We need the following components to build the whole project:
- Bolt WiFi module
- LM35 Temperature sensor
- 3Jumper wires ( Male to Female ) and 2simple connecting wires(for buzzer)
- USB cable ( mobile charger can also be used )
- Power Source ( power bank can also be used )
- First, we will connect the Bolt Wifi module to LM35 temperature sensor through jumper wires.
- LM35 has 3 pins, namely VCC, output, GND. So, we'll connect it with Bolt Wifi Module as follows:
VCC pin of the LM35 is connected to 5v of the Bolt Wifi module.(The
brownwire)
Output pin (middleone)is connected to A0 (Analog input pin) of the Bolt Wifi module.(The
red wire)
GND pinLM35 is connected to the GND.(The
orange wire)
- To connect the Buzzer, we'll use simple connecting wires, and connect as follows:
The negative pin(small one) is connected with the GND along with the LM35.
The Positive pin(large pin) is connected to the pin '1' (GPIO).
Remember,buzzer is optional. No need to worry much about it!
Step 3: Software Configuration.We'll be using VMware Ubuntu device on our PC(Windows). You can Download the Virtual Box and Ubuntu Server.
To set up the server, follow the steps as shown in below video:
Once you are done with the ubuntu installation, let's hop into the next section.
Step 4: Bolt Python Library and TweepyThe Bolt Python library makes it easy to interact with the Bolt cloud API from our Python application.
We can control or fetch the data from Bolt cloud to our server(Here Ubuntu from VMware ), Bolt Python library will help us in building the IoT product.
Before we proceed to this step, first ssh(login) to Ubuntu server and follow the below steps to use Bolt Python library in code.
----> i. Update the packages on UbuntuExecute the command below so that the packages on Ubuntu are updated to the latest version.
sudo apt-get -y update
----> ii. Install python3 pip3pip3 is a package manager for python3 used to install and manage packages and python libraries. It is system independent.
Install pip3 using the following command,
sudo apt install python3-pip
----> iii. Installing boltiot Library Using pipNow we will install the boltiot python library on your Ubuntu server.
Type the below command in terminal to install boltiot python library.
sudo pip3 install boltiot
Cool, now we are all set with the boltiot python library.
Similarly, now we can install tweepy:
sudo pip3 install tweepy
The next sections deals with the third party applications set-up as needed to generate sms, email, and twitter.
Step 5: Creating an account on Twilio#1: Open https://www.twilio.com/ in browser.
#2: Click on Get a Free API Key
button to sign up.
#3: Fill all the necessary details in SIGN UP form. Below is the screenshot of filled sign up form.
#4: To verify they will ask for your phone number. Choose India as an option in the dropdown and then enter your phone number.
#5: Click on "Products" as shown on the screen below,
#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".
#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.
#8: Click on "Skip this step" when it asks you to Invite a Teammate.
#9: Your project should be created at this point. Click on "Project Info" to view the account credentials which is required for your projects.
#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.
#11: From the drop-down menu, choose "Programmable SMS". Now click on Get Started
button to generate phone number.
#12: Click on Get a number
button.
#13: Then a popup will appear. Click on Choose this number
button.
#14: Then a popup will appear which will have the final number. Copy this number and save to notepad for future references.
Phew!. We have successfully created the account on Twilio. Next, we'll set up an account on mailgun for email services.
Step 6: Mailgun Configuration for EmailMailgun 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 on Mailgun
a. Open https://www.mailgun.com/ in browser.
b. Click on Sign Up button.
c. Fill all the necessary details in SIGN UP form. Below is the screenshot of filled sign up form.
d. We will get a verification mail having a link. Click on that link to verify our mail.
e. To verify they will ask for phone number. Choose India as an option in the dropdown and then enter our phone number.
f. After verification, we will go to domain and click on sandbox url
g. Now we need to verify our mail address to receive mail from mailgun. for this we need to enter our mail address in authorised recipient section. we will receive the mail then we have to verify our email address.
Mailgun Mail Address Verify
h. We need to click on API section for API key and API url.
Once we are done with the sms, and email. Let's set up our twitter configuration to give us latest "tweets" about the temperature.
Step 7: Setting up Twitter configurationStep 1: Create an account on twitter.com
Head over to Twitter's Signup Page and create an account. If you already have a Twitter account, you can skip this step. Creating a Twitter account is easy and only takes a few minutes. You do not have to wait for a long time for your account to be created.
Sign-up for the Twitter account using your mobile phone number. If you do not have a verified mobile number linked to the Twitter account, Twitter won't allow you to create a developer account.
Once your account has been created, you will also need to enter your email ID for the account. Without the email ID, we won't be able to invite you to the Developer account.
Go to your profile on Twitter using this link. You will see a field for entering your email ID. Please enter your email ID if it is not there and click on Save Changes button at the bottom of the page.
IMPORTANT - Please make sure that you supply both the email ID and the mobile number while creating a Twitter account. If you do not supply both of them, you won't be able to apply for the Developer Account.
Also, please do NOT disable email notifications from Twitter. Doing so, will not allow us to share the Developer Account access with your Twitter account.
Step 2: Share Twitter handle details with Bolt IoT
Once you have created a Twitter account, you will need to fill the form by clicking here. If you already have an existing Twitter Developer account, you do not have to fill up the form. If your twitter developer account has been approved, you will receive an email from Twitter informing you of the same. If you have not received any such mail from Twitter, you can fill up the form.
Once you have filled up the form and submitted it, Bolt IoT team will send you an invite link within next 24 hours.
Click on "Accept invitation".
Step 3: Sign in to Twitter
Enter your details to sign in to Twitter if you have not signed in yet. If you are already signed into Twitter, this screen will not show up.
After logging in, if you see a screen similar to the one in Step 7, you can directly skip steps 4 to 6.
Step 4: Choose a Twitter account to link
In this step, you will be asked to link the Twitter account to your developer account. Note down the Twitter username as shown in this step.
Click on "Continue" once you have selected the account.
Step 5: Add Account details
In this step, you will need to tell Twitter some details about your Twitter account. For the first question, since you are using it for your personal use, click on the second option. For the second question, write down the Twitter username that you had noted down in the previous step. Remove the "@" symbol before writing down the username. Finally, select your country from the dropdown menu and click on "Continue".
Step 6: Mention Use case for the project
In this step, you will need to tell the Twitter reviewing authority why you are applying for a developer account. Since you are a student, you can click on "Student project/Learning to code" and/or "Academic Research".
For the next question, you will need to describe the project that you are going to build in a minimum of 300 words. You can use the below description
I want to learn about IoT using the Bolt IoT platform. I am planning to integrate Bolt IoT with Twitter to build a project, which tweets from my account whenever the proximity sensor detects that the obstacle has reached too close. This will help me understand how IoT works as well as how I can use it in real-world scenarios and build useful projects.
Finally, click on "No" in the final question and then click on "Continue" as you will not be sending data to any Government or any such agency.
Step 7: Accept Developer agreement
The next step is to simply accept the Developer agreement. Read the developer agreement and click on the first checkbox to accept the terms and conditions and then click on "Submit".
Once you have submitted, you will be asked to verify the same via your email ID. A mail will be sent to your email ID which you had used to sign up for Twitter. Follow the instructions given in the email.
That's it. Once you have verified your developer account via email, your application for Twitter developer account will be approved and you will be shown a screen similar to the one below.
That's it. Now we can generate alerts on our Twitter Handle! Before moving on to the codes, let's first set up the Bolt cloud API and device ID.
Step 8: Generating API in Bolt Cloud8a. Device IDWe can find the device id under device section.
8b:Key
We have to generate Bolt API key as the following in Bolt cloud:
1. Make the same circuit connections as described before.
2. Once you are done with that, log in to digitalOcean droplet and create a new folder to hold the files for this topic.
3. We can create a new file it using the following command.
sudo nano conf_anomaly.py
4. After the editor is open, type in the following configuration parameters:
import boltiot import Bolt
import time,json,
import tweepy
#You can find SSID in your Twilio Dashboard
SSID = 'AC7ae69b9370XXXXXXXXXX6978d3ce4ce8'
#You can find AUTH TOKEN on your Twilio Dashboard
AUTH_TOKEN = 'eb4563dfdXXXXXXXXXXadde03ae817bb'
#FROM_NUMBER,This is the no. generated by Twilio.
FROM_NUMBER = '+13XXXXXXX80'
#TO_NUMBER,This is your number
TO_NUMBER = '+91XXXXXXXX6
3'#This is the private API key which you can find on your Mailgun Dashboard
MAILGUN_API_KEY = '4beed33869c0a7de8b97XXXXXXXXXXXX-060550c6-e591c150'
#SANDBOX_URL,You can find this on your Mailgun Dashboard
SANDBOX_URL= 'sandbox12b9e8227bf34c118XXXXXXXXXXXXXXX.mailgun.org'
#SENDER_EMAIL,This would be test@your, email from where we'll receive mail
SANDBOX_URLSENDER_EMAIL = 'test@sandboxXXXXXXXXXXXXXXX18ccec4718337b3f6.mailgun.org'
#RECIPIENT_EMAIL,Enter your Email ID Here
RECIPIENT_EMAIL = 'cbXXXXXX@gmail.com'
#tweeter consumer key
consumer_key="yTXXXXXXXXXXXXXXXXXXX9vI"
#consumer secret twitter
consumer_secret="aksXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXhng"
access_token="10786XXXXXXXXXXXXXX-BNXXXXXXXXXXXXXXXXXXXXXX"
access_token_secret="DqkXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
#API_KEY,This is your Bolt Cloud account API key
API_KEY = '499d00d0-81f6-XXXX-XXXX-7caaecca6e93'
#DEVICE_ID,This is the ID of your Bolt device
DEVICE_ID = 'BOLTXXXXXXX6'
FRAME_SIZE = 10
MUL_FACTOR = 3
mybolt=Bolt(API_KEY, DEVICE_ID)
#function to trigger the buzzer
def call_me():
response=mybolt.analogWrite('1','1023')
time.sleep(5)
response=mybolt.analogWrite('1','0')
# Dictionary to store credentials as key-value pairs.
config = {
"consumer_key" : consumer_key,
"consumer_secret" : consumer_secret,
"access_token" : access_token,
"access_token_secret" : access_token_secret
}
# Method to authenticate user via Tweepy and return API object
def get_api_object(cfg):
auth =tweepy.OAuthHandler(cfg['consumer_key'],
cfg['consumer_secret'])
auth.set_access_token(cfg['access_token'],
cfg['access_token_secret'])
return tweepy.API(auth)
mybolt = Bolt(conf.bolt_cloud_api_key, conf.device_id)
#function to call the tweet on our dashboard
def call_tweet():
temperature_threshold = 59
counter=random.random()
tweet=1
while tweet:
response = mybolt.analogRead('A0')
data = json.loads(response)
print ("The twitter value is "+data['value'])
try:
sensor_value = int(data['value'])
if sensor_value > temperature_threshold:
print ("Temperature has crossed the threshold.")
# Call get_api_object to authenticate user and get the API object
api_object = get_api_object(config)
# Store the tweet message in the variable
tweet = ("Temperature has crossed the threshold "+str(counter))
# Post the tweet on your Twitter account using the update_status method.
status = api_object.update_status(status=tweet)
except Exception as e:
print ("An error occurred ", e)
tweet=0
Note: We have to replace all the above value with our credentials. We can find the first four values in Twilio dashboard and the last two in Bolt Cloud dashboard.
You 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'
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:
5.1) Fetch the latest sensor value from the Bolt device.
5.2) Store the sensor value in a list, that will be used for computing z-score.
5.3) Compute the z-score and upper and lower threshold bounds for normal and anomalous readings.
5.4) Check if the sensor reading is within the range for normal readings.
5.5) If it is not in range, send the SMS.
5.6) Wait for 10 seconds.
5.7) Repeat from step 5.1.
6. Here we will start writing the code. Let us start with the imports.
import conf_anomaly, 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.
The following lies code helps 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):
The above line helps define a function, which takes 3 input variables: history_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. You can read more about variance here.
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 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.
mybolt = Bolt(conf.API_KEY, conf.DEVICE_ID)
sms = Sms(conf_anomaly.SSID, conf_anomaly.AUTH_TOKEN, conf_anomaly.TO_NUMBER, conf_anomaly.FROM_NUMBER)
mailer = Email(conf_anomaly.MAILGUN_API_KEY, conf-anomaly.SANDBOX_URL, conf_anomaly.SENDER_EMAIL, conf_anomaly.RECIPIENT_EMAIL)
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 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 Exception as 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 fridge. Sending an SMS and email.")
response = sms.send_sms("Someone has opened the Fridge")
response = mail.send_email("Someone has opened Fridge")
conf_anomaly.call_me()
conf_anomaly.call_tweet()
print("This is the response ",response)
history_data.append(sensor_value)
except Exception as e:
print ("Error",e)
time.sleep(10)
Step 10: 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_detection.py
After about 100 seconds (10 seconds delay with a frame size of 10), the system will start printing the values, as per the following image:
Anomaly occurred at the last line!
Note: If you are not getting these results, try changing the FRAME_SIZE and MUL_FACTOR values in the conf_anomaly.py file.
The outputs can be seen as:
That's all. The project is all complete. Thank You!
Comments