Get Started With Watson Iot Platform - Hello World!!!

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Get Started with Watson IoT Platform Hello World!!!

Recently we had a lot of request in support on How to Get Started with Watson IoT
Platform or on how to send a simple Hello World!!! message via the Watson IoT
Platform. There are uncountable resources out there that can help you with this
topic, but sometimes not everything is clear enough or easy, therefore I hope that
this article will help get started as easy as possible.
There are many ways to accomplish this and Ill try to cover some of them.
Resources? This is what Ill be using for starters
1. One piece of Centos 7 VM
2. Latest ibmiotf libraries (as this moment they are on 0.2.4 level)
3. Python 2.7.5 (using this version as it is shipped with Centos 7)
Perquisites: In order to install the ibmiotf libraries you need to have install pip.
Install pip
1. Download it from here
https://pip.pypa.io/en/stable/installing/
Use: wget https://bootstrap.pypa.io/get-pip.py
2. Install pip
Use: python get-pip.py
Install ibmiotf libraries
1. pip install ibmiotf
If you see this at the end the it means that it was installed successfully:
Successfully installed ibmiotf-0.2.4 iso8601-0.1.11 paho-mqtt-1.2 pytz2016.6.1 requests-2.11.1
As you can see, it automatically installed other necessary packages.
Now, you should be all setup to connect to the Watson IoT Platform.
As guiding you can follow:
Python for application developers
https://newconsole.ng.bluemix.net/docs/services/IoT/applications/libraries/python.html
Deploy and IoT Starter application
Getting started with Watson IoT Platform Starter
https://new-console.ng.bluemix.net/docs/starters/IoT/iot500.html
This will help you further to get an application id and deploy a Node-Red instance
that we use in this scenario.

Of course, we need to register a device in order to be able to contact the IoT


Platform. Below is the guide to help you achieve that:
How to Register Devices in IBM Watson IoT Platform
https://developer.ibm.com/recipes/tutorials/how-to-register-devices-in-ibm-iotfoundation/
After you register the devices you will be provided with the below information:
Organization ID
xxxxxx
Device Type
xxxxxxxxxxxxxxx
Device ID
xxxxxxxxx
Authentication Method
token
Authentication Token
Xxxxxxxxxxxx
There are several authentication mechanisms but I would suggest to use for this an
API Key that you can generate from Watson IoT Platform dashboard UI, it should look
similar to:
Generate API Key
API Key
x-xxxxx-xxxxxxxxxxxx
Authentication Token xxxxxxxxxxxxxxxxxxxx
The Python Coding
import
import
import
import
import

ibmiotf
sys
ibmiotf.application
ibmiotf.device
time

options = {
"org":"xxxxx", //this is Organization ID
"id":"MyApp", //this Application ID
"auth-method":"apikey", //this is the authentication mechanism
"auth-key":" x-xxxxx-xxxxxxxxxxxx", //this is the API Key
"auth-token":" xxxxxxxxxxxxxxxxxxxx"//this is the Authentication Token
}
//connect to the Watson IoT Platform
try:
appClient = ibmiotf.application.Client(options)
appClient.connect()
except ibmiotf.ConnectionException as e:
print(str(e))
sys.exit()

//send message to the Watson IoT Platform using the device


myData={"message":"Hello World!"}
appClient.publishEvent("Device Type"," Device ID","status","json, myData)
The simplest way to hook-up the code with the Watson IoT Platform is to use NodeRed. The Node-Red can be accessed with https://<APP_NAME>>.mybluemix.net/red/
after the Watson IoT Platform was deployed.
This is the flow that we are using:

The configuration for IBM IoT Node

Deploy the flow in Node- Red and you can run the script to publish the message. You
should be able to see in the debug tab of the Node-Red the: Hello World!
message.

Hope this will help you to get started and stay tuned for many more articles of this
type.

You might also like