I am Pritam Naskar , a 3rd year B.Tech student . In this semester break I wanted to do something unique and surprisingly I get that chance when I came to know about the BOLT IoT Training program .Before moving to my project I want to thank BOLT IoT from the bottom of my heart to give me such a wonderful thoughts , ideas and influence to make my first IoT Project .
This project is about making Temperature Monitoring or Sensing device using BOLT IoT-WiFi module . It will sense the temperature of your surroundings and give you a real time temperature alert on your cell phone device . So hang in there to see how to build your very own Temperature alerting IoT device . Nowadays, this device can be very useful in the means of sensing temperature in any Pharmaceutical lab where temperature is the prime factor. It'll give you SMS update when the temperature crosses it limits and you can change those threshold on your own. You can make a huge profit out of it because your manufacturing defect will become lesser using this device.
In this project I using my BOLT IoT kit and BOLT Cloud website . So let's see how to build the project :STEP 1 : First and foremost task is to setup your BOLT IoT device with internet connection. Once you successfully connect your BOLT device with your home Wi-Fi it'll be look like below -
Hold the sensor in a manner such that you can read LM35 written on it. In this position, identify the pins of the sensor as VCC, Output and Ground.
VCC pin (Red) of the LM35 connects to 5v of the Bolt Wi-Fi module. Output pin (Orange) of the LM35 connects to A0 (Analog input pin) of the Bolt Wi-Fi module. Ground pin (Yellow) of the LM35 connects to the Ground.
The final setup will look like following -
STEP 3 : After setting up the final hardware connection we are ready to configure the hardware device by logging into the BOLT Cloud ( https://cloud.boltiot.com/ ) After logging into the BOLT Cloud website click on Products and then click on add product. After this following window will pop-up -
give the suitable name to your product. After that choose OUTPUT DEVICE which collect the data as GPIO.
After adding the product click on "CONFIGURE" . Now configure your product as the following step sequentially -
In hardware setup we connected the output pin of LM35 sensor with A0 pin of BOLT WIFI module that's why here we are choosing A0.
After writing the code we have to save the code and hardware using "Save" option on the right hand side of the screen and exit from that page.
After saving we have to link our product with our specific device . To do that we select the link option right at the corner of the screen. Choose our device and click on done.
Now we have to deploy the configuration that we have done up till now by clicking on the cloud shaped " deploy" button. After deploying the configuration we need to check the graph of the temperature.
How is it going guys up till now ! pretty interesting right . Now we will link our device with TWILIO which will alert us when the temperature increases or decreases it's assign thresholds. So, are you guys ready...I hope you are then let's check it out-
First we will go to the API section of our BOLT Cloud website and generate our API key and copy it somewhere from which we can get it easily.
STEP 4 : In this step we are going to create our free account in TWILIO(https://www.twilio.com/ ) by signing up in it (This will be totally free of cost) with your all necessary details. And after verifying your email you are able to log in to the TWILIO free account. After this you get a specific number like this which you have to use later in this project.
STEP 5 :
Before start coding in python you have to update the ubuntu and have to add the BOLT Python library in ubuntu to do that follow below mentioned steps:
1: Update the packages on Ubuntu
Execute the command below so that the packages on Ubuntu are updated to the latest version. If you skip this step, you may encounter an error while installing the Boltiot package.
sudo apt-get -y update
2: Install python3 pip3
pip3 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
3: Installing boltiot library using pip
Now 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
Now we are done with boltiot python library installation. In the next section, we will learn how to use the Bolt python library to check the device status and switch off the device
Now we will write a Python program which will fetch the temperature data collected by Bolt and send SMS if the temperature value goes outside our specified temperature range. You can write the python code anywhere as your wish (I write the code in Ubuntu ).
Before start to writing the code you have to save some details . Save them there from where you can easily access it. The details are following-
SID = 'You can find SID in your Twilio Dashboard' API_KEY = 'This is your Bolt Cloud account API key' DEVICE_ID = 'This is the ID of your Bolt device' AUTH_TOKEN = 'You can find on your Twilio Dashboard' FROM_NUMBER = 'This is the no. generated by Twilio. You can find this on your Twilio Dashboard' TO_NUMBER = 'This is your number. Make sure you are adding +91 in beginning
Keep in your mind that you have to replace all the above value with your credentials. You can find the values in Twilio dashboard and Bolt Cloud dashboard.
1. Next create a file named conf.py which will store all the credentials related to Twilio. To create a new file type sudo nano conf.py . After that write all the above details to save all the credentials in a single file.
2. Now create one more file named temp_sms.py . To do so you have to type sudo nano temp_sms.py in the terminal. Now we will write main code to collect the data from the Bolt and send SMS if it crosses the threshold.
3. Code Explanation :
- In the code, we first have to import our conf file which has all the credentials. The python json and time libraries are also imported in the same line. Since we have saved our conf file with the .py extension, we can directly import it.
import conf, json, time
- json is a python library used for handling all operations on JSON objects. JSON is nothing but a data communication format widely used on the Internet for sending/receiving data between a client and server. Remember, 'json' is the python library used for handling JSON objects and JSON is a data communication format.
- Now we will import Bolt python library which will let us fetch the data stored in Bolt Cloud. To send the SMS, the Sms library is also imported. The below line of code imports the required libraries.
from boltiot import Sms, Bolt
- In the above line, we are importing two objects. First one is SMS which will be used to send SMS alerts and the other one is Bolt which is used for accessing data from your Bolt device like the temperature reading.
- Now we will initialize two variables which will store minimum and maximum threshold value. You can initialize any minimum and maximum integer limits to them.
- This would send an alert if the temperature reading goes below the minimum limit or goes above the maximum limit similar to the alerts on a Pharmaceutical company's manufacturing line.
minimum_limit = 400 maximum_limit = 600
- Now to fetch the data from Bolt Cloud, we will create an object called 'mybolt' using which you can access the data on your Bolt.
For the Bolt Cloud to identify your device, you will need to provide the API key and the Device ID when creating the mybolt object. Since the conf file holds the API key and Device ID variables, you can use them as follows,
mybolt = Bolt(conf.API_KEY, conf.DEVICE_ID)
The above code will automatically fetch your API key and Device ID that you have initialized in
- Now to send an SMS, we will create an object of the same.
sms = Sms(conf.SID, conf.AUTH_TOKEN, conf.TO_NUMBER, conf.FROM_NUMBER)
The above code will automatically fetch your SID, AUTH_TOKEN, TO_NUMBER and FROM_NUMBER that you have initialized in
conf.py file. Make sure you have given correct value in
- Since we want to continuously monitor the temperature reading, we will enclose our logic to fetch, compare and send the SMS inside an infinite loop using the `while True:` statement. An infinite loop is a special loop which executes its code continuously since its exit condition is never going to be valid. To exit the loop, we will need to forcibly exit the code by holding CTRL + C.
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 Twilio to send a SMS") response = sms.send_sms("The Current temperature sensor value is " +str(sensor_value)) print("Response received from Twilio is: " + str(response)) print("Status of SMS at Twilio is :" + str(response.status)) except Exception as e: print ("Error occured: Below are the details") print (e) time.sleep(10)
- The code continuously fetches the temperature value using `analogRead` function. Since the sensor is connected to A0 pin of the Bolt, we will execute the analogRead() function on the pin A0.
- The response from the Bolt Cloud using the analogRead() function is in a JSON format, so we will need to load the JSON data sent by the cloud using Python's json library.
- The temperature value is inside a field labelled as "value" in the response. We can access the JSON values using the statement `sensor_value = int(data['value'])`. This line also converts the sensor reading to integer data type for comparing the temperature range.
- The next line of code checks if the temperature reading is above the maximum limit or below the minimum limit. If it exceeds, then the SMS will be sent.
- The SMS to be sent will contain the text "The Current temperature sensor value is" followed by the temperature value.
- The response from Twilio will be stored inside the `response` variable.
- Once the temperature reading has been sent, we will need to wait for 10 seconds to get the next reading. For this, we will put the program to sleep once every loop iteration.
- The statement `time.sleep(10)` puts the program execution on hold for 10 seconds. This means that the program would not execute for a period of 10 seconds.
In the above code, we are fetching the data every 10sec. You can change the value but ideally, it should be good if the time interval between 2 data points is more than 10sec.
[Note: The above "sensor_value" is the raw temperature reading, obtained from the LM35 sensor.]
Below is the complete code:
import conf from boltiot import Sms, Bolt import json, time minimum_limit = 400 maximum_limit = 600 mybolt = Bolt(conf.API_KEY, conf.DEVICE_ID) sms = Sms(conf.SID, conf.AUTH_TOKEN, conf.TO_NUMBER, conf.FROM_NUMBER) 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 Twilio to send a SMS") response = sms.send_sms("The Current temperature sensor value is " +str(sensor_value)) print("Response received from Twilio is: " + str(response)) print("Status of SMS at Twilio is :" + str(response.status)) except Exception as e: print ("Error occured: Below are the details") print (e) time.sleep(10)
[Note: The above "sensor_value" is the raw temperature reading, obtained from the LM35 sensor.]
Now it's time to run the code. Run the code using command
sudo python3 temp_sms.py
We are all ready to get the SMS of the temperature in our phone from TWILIO.....
And Here is our SMS Output
Now Enjoy with your own Temperature Monitoring Device. Test it in different temperature and take the reading and all. So I hope you guys will definitely give it a try. If you face any problem while doing this project feel free to ask . Till then Good Bye & Thank You...