IBM Watson IoT Platform with Raspberry Pi

by Jun 1, 2020IoT Cloud, Raspberry Pi projects

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.

IBM Launches Watson Internet of Things Consulting Practice
Image courtesy of IBM

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.

Analyze IoT ESP8266 sensor data – IBM Developer
Image by IBM

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

Creating a multiplication Skill in Alexa using python

Written By Monisha Macharla

Hi, I'm Monisha. I am a tech blogger and a hobbyist. I am eager to learn and explore tech related stuff! also, I wanted to deliver you the same as much as the simpler way with more informative content. I generally appreciate learning by doing, rather than only learning. Thank you for reading my blog! Happy learning!

RELATED POSTS

Creating REST API CRUD with Node.js, Express and MongoDB

Creating REST API CRUD with Node.js, Express and MongoDB

In our previous blog, we explored the fundamentals of creating a REST API with in-memory CRUD operations using Node.js and Express. Now, we're taking a significant step forward by integrating our product data with a database. This transition is pivotal for achieving...

How to create REST API using Node.js and Express?

How to create REST API using Node.js and Express?

In the vast landscape of web development, creating a robust API is a fundamental skill. APIs, or Application Programming Interfaces, serve as the communication bridge between different software applications. Today, we'll embark on a journey to build a simple blog API...

APIs Vs. Microservices: What is the difference?

APIs Vs. Microservices: What is the difference?

You've probably heard two extravagant terms thrown around when discussing software design and integrations: APIs and microservices. Both topics are important in web application development and design nowadays, and their applications overlap. In this article, we will...

Understanding Salesforce IoT and Its Importance

Understanding Salesforce IoT and Its Importance

In this post, we are going to discuss Salesforce IoT. All across the globe, people are connecting to the Internet to access information, communicate with other people, and do business. But it's not just people that are using the Internet: objects use it too....

Best IoT IDEs For Successful IoT Products

Best IoT IDEs For Successful IoT Products

IoT IDEs are the essential tools that we need in developing our IoT projects. They give us the pathway to connect our Hardware and Software and do magical things using IoT. Consequently, they are useful. IoT is the next big thing in the world. It has taken the whole...

Top 5 Databases to store data of IoT applications

Top 5 Databases to store data of IoT applications

Databases for IoT applications The Internet of Things (IoT) produces massive quantities of data, such as streaming data, time-series data, RFID data, and sensory data, among other things. The use of a database is needed for efficient data management. The existence of...

VIDEOS – FOLLOW US ON YOUTUBE

EXPLORE OUR IOT PROJECTS

IoT Smart Gardening System – ESP8266, MQTT, Adafruit IO

Gardening is always a very calming pastime. However, our gardens' plants may not always receive the care they require due to our active lifestyles. What if we could remotely keep an eye on their health and provide them with the attention they require? In this article,...

How to Simulate IoT projects using Cisco Packet Tracer

In this tutorial, let's learn how to simulate the IoT project using the Cisco packet tracer. As an example, we shall build a simple Home Automation project to control and monitor devices. Introduction Firstly, let's quickly look at the overview of the software. Packet...

All you need to know about integrating NodeMCU with Ubidots over MQTT

In this tutorial, let's discuss Integrating NodeMCU and Ubidots IoT platform. As an illustration, we shall interface the DHT11 sensor to monitor temperature and Humidity. Additionally, an led bulb is controlled using the dashboard. Besides, the implementation will be...

All you need to know about integrating NodeMCU with Ubidots over Https

In this tutorial, let's discuss Integrating NodeMCU and Ubidots IoT platform. As an illustration, we shall interface the DHT11 sensor to monitor temperature and Humidity. Additionally, an led bulb is controlled using the dashboard. Besides, the implementation will be...

How to design a Wireless Blind Stick using nRF24L01 Module?

Introduction Let's learn to design a low-cost wireless blind stick using the nRF24L01 transceiver module. So the complete project is divided into the transmitter part and receiver part. Thus, the Transmitter part consists of an Arduino Nano microcontroller, ultrasonic...

Sending Temperature data to ThingSpeak Cloud and Visualize

In this article, we are going to learn “How to send temperature data to ThingSpeak Cloud?”. We can then visualize the temperature data uploaded to ThingSpeak Cloud anywhere in the world. But "What is ThingSpeak?” ThingSpeak is an open-source IoT platform that allows...

Amaze your friend with latest tricks of Raspberry Pi and Firebase

Introduction to our Raspberry Pi and Firebase trick Let me introduce you to the latest trick of Raspberry Pi and Firebase we'll be using to fool them. It begins with a small circuit to connect a temperature sensor and an Infrared sensor with Raspberry Pi. The circuit...

How to implement Machine Learning on IoT based Data?

Introduction The industrial scope for the convergence of the Internet of Things(IoT) and Machine learning(ML) is wide and informative. IoT renders an enormous amount of data from various sensors. On the other hand, ML opens up insight hidden in the acquired data....

Smart Display Board based on IoT and Google Firebase

Introduction In this tutorial, we are going to build a Smart Display Board based on IoT and Google Firebase by using NodeMCU8266 (or you can even use NodeMCU32) and LCD. Generally, in shops, hotels, offices, railway stations, notice/ display boards are used. They are...

Smart Gardening System – GO GREEN Project

Automation of farm activities can transform agricultural domain from being manual into a dynamic field to yield higher production with less human intervention. The project Green is developed to manage farms using modern information and communication technologies....