IBM Watson is one of the leading cloud computing platforms there is. It has almost all the features one could expect from a modern-day cloud platform, from machine learning support to IoT, even till edge computing. In this tutorial, we will use raspberry pi and an ultrasonic sensor to receive data, store it in the IBM cloud, and visualize the results.
Content
Hardware
Basic hardware requirements are necessary to visualize the data. In this tutorial, raspberry pi and sensor will be used. We will be using an ultrasonic sensor but feel free to experiment with any. Also, the complexity of the hardware does not affect the output if it can sense data and transfer it to the RPI.
1. Raspberry Pi
Raspberry pi is a small computer. It mostly uses Rasbian as its operating system but, as of today, it can support almost anything. From home automation to self-driving cars, it is used in almost every hardware project. We will use it to receive sensor data, process it, and upload it to the cloud for visualization.
If you are working on a certain project use the respective hardware, for the purpose of this tutorial the code for the ultrasonic sensor(HC-SR04) will be used and examples from temperature sensors as well since the process are not sensor specific. The only small difference will be seen in the code but operating them individually is quite easy.
IBM Watson
IBM Watson is one of the leading cloud computing platforms there is. Sometimes one may feel that Google Cloud gets confusing so knowing other platforms may come in handy. In IBM there are lite features that can be used without payment. Also, its community is very active, so if you come across any unexpected errors or doubts, they are addressed quickly.
Setup
Its quite simple to setup IBM Watson. Create an account in the IBM cloud and you will enter your dashboard.
IoT Platform
IBM’s IoT platform is well up to date on technology. It supports almost all features, from data visualization to edge computing. Go through the following link, it contains some of the latest updates on IoT:https://developer.ibm.com/technologies/iot/
After creating an account go to your dashboard and open catalog. Go to Catalog and click on IoT. There you will see an option for IoT platform. Create a new platform and launch it.
Add Device
The IoT platform is used to visualize and analyze data given to it. The device which acts as the source of that data must be linked to the platform to transfer data. Create a new device and add the details of the raspberry pi to it.
Open your raspberry pi and get its mac address. Type ifconfig in the command line if you have any issues getting it.
In the device ID add the mac adress of your RPI without any colons and in the device type add any name of choice.
Ignore the device information and security section, go the to the summary directly and publish it.
After finishing you will se the device credentials will be shown automatically. Save the details somewhere safe since they will be required during communication between RPI and cloud.
Add Graph
To analyze and visualize the sensor data we need to create graphs on the platform. It cumulates and visualizes the data simultaneously as it is received. There are a variety of graphs that can be chosen from, pick something that is appropriate to the application at hand. After creating the credentials add a new card to create a graph.
Click on the added device to add the sensor values provided by that device directly to the graph. Then click next.
Then choose the graph that you want for your application and give the specifications . The picture below is an example for a temperature sensor and for a ultrasonice sensor but feel free to try for others as well.
After filling in the details, choose the graph size and features then click next.
After completing that you will be able to see an empty graph. Change the security options to allow the connection with raspberry pi. Change them from default to TLS Optional. It means that the Transport Layer Security of this platform will be optional and authentication may be skipped.
Code
Now we will write a python code to get data from the sensor and upload it to the cloud. Start by installing the communication library used to send data to the cloud. Use the MQTT( MQ Telemetry Transport) protocol to transfer data. It is a lightweight messaging machine-to-machine protocol used mostly in low bandwidth devices. Also, there is a difference between MQTT and HTTP, for more information visit this site: https://iot4beginners.com/mqtt-vs-http/
pip3 install paho-mqtt
Now import the following libraries in a new python file:
import time
import paho.mqtt.client as mqtt
import RPi.GPIO as GPIO
- The RPI general purpose input/output library RPI itself.
Give the credentials of the device created :
# ORG stands for organization id
ORG = "******"
DEVICE_TYPE = "raspi"
TOKEN = "************"
DEVICE_ID = "*********"
Assign the URLs to access and upload the files:
server = ORG + ".messaging.internetofthings.ibmcloud.com";
pubTopic1 = "iot-2/evt/status1/fmt/json";
Create the client id:
authMethod = "use-token-auth";
token = TOKEN;
clientId = "d:" + ORG + ":" + DEVICE_TYPE + ":" + DEVICE_ID;
Start the coding for the sensor by assiging the trigger and echo pins of the ultrasonic sensor GPIO pin values.
TRIG = 16
ECHO = 18
GPIO.setmode(GPIO.BOARD)
GPIO.setup(TRIG,GPIO.OUT)
GPIO.setup(ECHO,GPIO.IN)
Calibrate the trigger pin before reading values:
GPIO.output(TRIG, False)
print("Calibrating.....")
time.sleep(2)
Establish a connection between the raspberry pi and cloud server. Any proper port number can be provided.
mqttc = mqtt.Client(client_id=clientId)
mqttc.username_pw_set(authMethod, token)
mqttc.connect(server, 1883, 60)
Finally the main loop that collects the data every 5 mins and sends it to the cloud:
while True:
GPIO.output(TRIG, True)
time.sleep(0.00001)
GPIO.output(TRIG, False)
while GPIO.input(ECHO)==0:
pulse_start = time.time()
while GPIO.input(ECHO)==1:
pulse_end = time.time()
pulse_duration = pulse_end - pulse_start
distance = pulse_duration * 17150
distance = round(distance+1.15, 2)
mqttc.publish(pubTopic1, distance)
print("published")
time.sleep(5*60)
- Working of sensor – The trigger pin is turned on to trigger a pulse. Then the time is calculated, starting from when the pulse left the sensor till it returned. Calculate the distance using the echo formula, taking into consideration the speed of sound.
- The distance uploaded is in JSON format
- After each such process, the system sleeps for 300 seconds and then continues.
Output
Every 5 mins the code sends data to the cloud. This data is accumulated and simultaneously plotted. The data can also be viewed in different forms. You can see each upload of data in the logs. Maintain the free limit of cloud service and explore more.
With this, we come to the end of this tutorial. I hope you learned something new about raspberry and IBM Cloud. It was hard in the beginning for me and I had to go through several material and blogs myself. So, if you come across any errors or doubts do comment below. The Working code and directory of this project can be found in this GitHub repository: https://github.com/Shaashwat05/IBM_IoT