Step-by-step
-
Preparation
Install Rasbian on your Raspberry Pi, and connect it to the internet.
Signup for an IBM Watson IoT Platform service instance from the IBM Cloud Catalog. For more information read the Getting started with Watson IoT Platform topic in Knowledge Center.
Register a device with IBM Watson IoT Platform. For information on how to register devices, see the Connecting Devices topic in the IBM Watson IoT Platform documentation.
At the end of the registration process, make a note of the following parameters:
- Organization ID
- Type ID
- Device ID
- Authentication Token
Optionally, to learn more about the device code you are going to install and run in this tutorial take a look at the README for the IBM Watson IoT Platform PSUtil Device Client in GitHub.
-
Installation on Raspbian
Installation across all OS’s is pretty much the same:
- Install any necessary system packages missing from the host (in order to install psutil on Windows you’ll need Visual Studio installed)
- Install the wiotp-sdk and psutil python modules using pip
- Download the sample code from GitHub
pi@raspberrypi ~ $ sudo apt-get update
pi@raspberrypi ~ $ sudo apt-get install python-dev python-pip
pi@raspberrypi ~ $ sudo pip install wiotp-sdk psutil
pi@raspberrypi ~ $ wget https://github.com/ibm-watson-iot/iot-python/archive/master.zip
pi@raspberrypi ~ $ unzip master.zip
pi@raspberrypi ~ $ cd iot-python-master/samples/psutil/src -
Connect to Quickstart
Connecting to Quickstart allows you to quickly verify your installation and connectivity to IBM Watson IoT Platform. The –quickstart command line argument will configure the device client to connect to quickstart using a generated deviceId based on your Pi’s MAC address:
pi@raspberrypi ~ $ python iotpsutil.py --quickstart
2019-05-20 12:39:28,621 wiotp.sdk.device.client.DeviceClient INFO Connected successfully: d:quickstart:sample-iotpsutil:3403868c1274
Welcome to IBM Watson IoT Platform Quickstart, view a vizualization of live data from this device at the URL below:
https://quickstart.internetofthings.ibmcloud.com/#/device/3403868c1274/sensor/
(Press Ctrl+C to disconnect)The following data points are collected:
- CPU utilization (%)
- Memory utilization (%)
- Outbound network utilization across all network interfaces (KB/s)
- Inbound network utilization across all network interfaces (KB/s)
Use the URL provided to launch the Quickstart web application and see the events coming from your Raspberry Pi visualized in real time.

-
Connect as a Registered Device
We will use environment variables to configure the device client to connect to your instance of Watson IoT Platform as your registered device.
These variables correspond to the device parameters for your registered device:
- WIOTP_IDENTITY_ORGID
- WIOTP_IDENTITY_TYPEID
- WIOTP_IDENTITY_DEVICEID
- WIOTP_AUTH_TOKEN
pi@raspberrypi ~ $ export WIOTP_IDENTITY_ORGID=myorgid
pi@raspberrypi ~ $ export WIOTP_IDENTITY_TYPEID=mytypeid
pi@raspberrypi ~ $ export WIOTP_IDENTITY_DEVICEID=mydeviceid
pi@raspberrypi ~ $ export WIOTP_AUTH_TOKEN=myauthtoken
pi@raspberrypi ~ $ python iotpsutil.py
(Press Ctrl+C to disconnect) -
Visualize Device Events
With the new boards and cards capability in the Watson Internet of Things platform, you can build your own Custom dashboard without writing any code. You can use the boards as the landing page of interest and then make use of the cards within them to:
- Create visualization charts for the real time data from your devices
- Create Gauges for visualizing physical quantities like Vehicle Speed, Temperature, pressure
- Create Donuts charts, bar charts to display the current value of the data points
- See the Data and storage consumption of your devices
- List of registered devices and etc..
Refer to Configuring Boards & Cards in the new Watson IoT Dashboard recipe to view the Raspberry Pi events in Watson IoT Platform using the boards and cards.
-
Conclusion & Next Steps
That was painless. Take a look at the IBM Watson IoT Platform Python SDK documentation and start modifying the device code in iotpsutil.py to fit your needs.

Where is the “Authentication Method” in the dashboard ? I am unable to figure it out ?
I see things like Device Id, Device Type, API Key, etc – but not the Authentication Method ?
Is it required ? If I open the NodeRed – It does not ask for it.
When you add a device to your IoT Foundation organisation you get your device credentials, which you need to put onto the device to connect it to your organisation. For example…
Organisation ID 123wer
Device Type Microwave
Device ID 345000987
Authentication Method token
Authentication Token ajJJU-kCZ@gbpKqw(
Vikki – Thanks for the responses above, I got past that.
Next, I’m in the Dashboard. and in “Download and extract your starter code to set up your development environment.
Download Starter Code”
If I click the above link to download – It goes to this page ” https://console.ng.bluemix.net/”
I was hoping it will download some code ?
I think you might be referring to a Bluemix related question? If you visit the IoT forum and post a question someone from the team will help you https://developer.ibm.com/answers/smartspace/internet-of-things/
How can I retrieve the “Authentication Token” once I have generated the API Key.
– I click on Generate API Key, then I get a API KEY and the token . I click on OK.
– Now I’m in Node Red – and if I go back to the dashboard – > API Keys -> I see only the ‘key’ , but not the authentication Token !
– which means I have to regenerate it again – and save it somewhere. I wished the Authentication Token was also visible when I clicked on the “API Keys”, in the dashboard.
The API key authentication tokens are non recoverable for security reasons, this is stated at the time of creation.
In this blog, the contents of the device.cfg are listed in a single line. Seems the “sudo service iot start” does not like it, and keeps failing when I restarted it.
Changing it to multiple lines got the iot process back running.
e.g:
#Device configuration file org = yourOrganizationCode type = iotsample-raspberrypi id = b827eba84426 auth-method = token auth-token = yourAuthToken #End of Configuration file
This did not work for me.
==
So, I broke it up into multiple lines and started the iot process, and now the iot process is running.
e.g:
org = 3****3
type = iotdevice
id = *******
auth-method = token
auth-token = ********
===
Thanks Kiran. Ran into the same issue myself. Your solution fixed it.
Can I get some help on the Command Support section – how do I do it ? is there some programming that I have to do.
Where or how do I send the commands ?
Thanks – Shashi
[…] ability to launch branded kits from enterprise partners (no Raspberry Pi kit yet) shows a commitment to establishing a strong connection to best position AWS IoT cloud as a […]
Nice recipe, but I got a question.
Is this iot service a essential component, or just an example program? I’m a bit confused because I cannot get the device ID without installing it in my Raspberry Pi. However, after the registration, it just keep sending test events and I have no way to change its behavior.
Thanks – Alex
Hi Alex
The iot service is not an essential component and is just an example program. The device Id could be any id as long as its unique in a given Bluemix organization. What the iot service does is – provides the MAC Address of the Raspberry Pi.
Thanks and Regards
Amit M Mangalvedkar
The installer is available at https://github.com/ibm-messaging/iot-raspberrypi/releases and the source code is available at https://github.com/ibm-messaging/iot-raspberrypi . In case you want to change the default behavior, you can modify the code and build it.
When I disconnect ether/wifi it takes up to +15min for this example to notice it’s not connected anymore. any ideas why?
[ res = publishMQTTMessage(&client, publishTopic, json); ] returns a “0” and eventually “-3” but not for a very long time.
It appears [ if (!MQTTAsync_isConnected(client)) ] does nothing once it has connected for the first time.
Hi,
We have tried it from our end, it usually takes a minute or 2 to notice the network is not connected. It depends on the network stack of your linux. When MQTTClient gets the message that the network is not connected, MQTTAsync_isConnected will return false and the reconnect logic will kick in. This code will take care of reconnect logic as well. So as soon as the client becomes disconnected, it will try to reconnect.
Please let us know if you have any more questions.
Thanks!
I’m using the Raspberry Pi 2, wheezy v 4.1.13. What version are you running and are you using ether or a Wifi Dongle? Also is there any documentation on what is actually performed when calling MQTTAsync_isConnected? And by “reconnect logic” does this mean you’re making calls to the network stack to try and restart a particular network interface or is it just the reconnect logic that I see in the sample C code.
Thanks for the quick reply!
Hi,
I have tried with in Raspberry Pi B. But I feel the functionality must be almost same in Pi2 as well. I have tried with ethernet and WiFi.
This sample uses the open source Paho client libraries to make the MQTT calls. So the documentation for the isConnected can be found in http://www.eclipse.org/paho/files/mqttdoc/Cclient/_m_q_t_t_client_8h.html#a6e8231e8c47f6f67f7ebbb5dcb4c69c0. The reconnect logic is in the sample code. The logic is present just in the code, and it does not make calls to the network interface.
But this issue occurs because the library still does not get the correct code from the interface that the connection has been lost.
Please let us know if you have any questions.
Thanks!!
Hello, where exactly is the code for logging and command?
Hi,
The logs in this sample are done using the syslog. You can find the logs in the /var/log/syslog.
And for the command, the code is present in https://github.com/ibm-messaging/iot-raspberrypi/blob/master/samples/c/mqttPublisher.c#L115 for rebooting the PI
If you want to understand to how to send a command to the raspberry pi, you can refer this recipe “https://developer.ibm.com/recipes/tutorials/arm-mbed-iot-starter-kit-part-2/” which sends a command using Node-RED.
Please let us know if you have any questions.
Thanks!
This is pretty awesome!
Just one more thing, I can see where you are sending data to Node-Red, the cpustat.c. However, where are you checking the command that is received?
Say if I want the raspberry pi to do something other than reboot.
Would this work with wiring pi?
Hey!!
The command implementation is on the same function. 🙂
https://github.com/ibm-messaging/iot-raspberrypi/blob/master/samples/c/mqttPublisher.c#L115
More precisely, the reboot is implemented in line 126 https://github.com/ibm-messaging/iot-raspberrypi/blob/master/samples/c/mqttPublisher.c#L126.
This function is called when a command is received in Raspberry Pi. So then it parses the json to get the time for reboot and then uses it to reboot the device.
Yes we can use this with wiring Pi. This is a sample and can be extended with wiring Pi.
Please let us know if you have any questions.
Thanks!
Hmmm, what changes need to be done to include the wiringPi library? Must be something with Makefile.
Hi @Static,
Yes. You would need to add “-lwiringPi” in the compile step of the Makefile.
Thanks!
I have got my Pi working with this recipe, but how do I stop it from sending the “sensor information” (d.myName, d.cputemp, etc.) every second? I had another Pi added awhile back and it is still in the device list but it isn’t sending this information every second, but I don’t remember what I did to it to get it to stop sending this information.
Try issuing a command – “sudo service iot stop” to stop the service sending the event to Watson IoT Platform.
Hi, I tried to download the installer from Github but my raspberry pi shows “Warning: Failed to create the file iot_1.0-2_armhf.deb: Permission denied
curl: (23) Failed writing body (0 != 16384)
Do you know why?
Thank you.
Looks like a write permission issue. Can you try creating a sample file and see whether its allowed? or can you use sudo access?
I think, the following needs to be done in Python to connect to a device registered on Watson IoT platform. However, the connection never goes through; keeps failing as ‘Connection reset by peer’. So, what do I need to do to make the connection happen from a Pi to the WIoTP?
import paho.mqtt as mqtt
# client Id derived using the tokens generated when adding a device on WIoTP
mqttc = mqtt.Client(client_id=’d:orgId:deviceType:deviceName’)
# password using the tokens generated when adding a device on WIoTP
mqttc.username_pw_set(‘use-auth-token’,password)
mqttc.tls_set(do_not_know_which_file__please_help)
mqttc.connect()
while True:
mqttc.loop()
I used the IoTFoundation.pem file for the tls_set() function. Still no luck.
Found it! We have to specify protocol as ssl.PROTOCOL_TLSv1_2 in tls_set() for the connection to go through.
Hello,
I have a raspberry Pi that is generating data in json format. I have created the iot device in Watson IoT Platform but still showing the device disconnected. No data from msg.payload in node-red application.
The iot service has been started on pi and device.cfg configured as well.
Any help is appreciated.
How can I troubleshoot Raspberry Pi with IOT not connecting with registered device.cfg file?
The source code is present here – https://github.com/ibm-watson-iot/iot-raspberrypi, Can you build the service with debug points and try?
I am following the steps for “Send Commands to your Raspberry Pi Device” and getting an error for the Trigger Reboot node “Syntax error: Unexpected token d” in the payload. Also, if I write the payload as {“delay” : 3}, there is no error but my Raspberry Pi doesn’t reboot. Can anyone suggest a solution?
Any help will be appreciated. Thanks!
can i use another OS like Ubuntu , and raspberry pi 3 , please let me know
I did every step like in the tutorial but when clicking the deploy button, I get :
The workspace contains some nodes that are not properly configured:
[Flow 2] IBM IoT (ibmiot out)
[Flow 2] reboot (inject)
When I click the trigger, I get the same Error as above: Syntax error: Unexpected token d”
And in the debug section I receive every second a message like this :
1.8.2017, 18:31:21node: da9634e9.dcc13iot-2/type/RbpIoT/id/ID/evt/status/fmt/json : msg.payload : Object
{ d: object }
Hi, I’ve the same issue.
Very informative post. I also using raspberry pi stater kit and I bought it some days ago from https://www.raspberrypistarterkits.com/ . But I don’t have idea to install this with my system. This post helps me a lot. Can you give some other reference regarding installations. It helps me a lot. Thanks.
pi@raspberrypi:~/Desktop/waleed $ sudo dpkg -i iot_1.0-2_armhf.deb
(Reading database … 112197 files and directories currently installed.)
Preparing to unpack iot_1.0-2_armhf.deb …
Unpacking iot (1.0-1) over (1.0-1) …
dpkg: dependency problems prevent configuration of iot:
iot depends on libssl1.0.0 (>= 1.0.0); however:
Package libssl1.0.0:armhf is not configured yet.
dpkg: error processing package iot (–install):
dependency problems – leaving unconfigured
Processing triggers for systemd (215-17+deb8u7) …
Errors were encountered while processing:
iot
Found this error once i going to run this command
sudo dpkg -i iot_1.0-2_armhf.deb
please help me
pi@raspberrypi:~ $ sudo dpkg -i iot_1.0-2_armhf.deb
(Reading database … 122700 files and directories currently installed.)
Preparing to unpack iot_1.0-2_armhf.deb …
Unpacking iot (1.0-1) over (1.0-1) …
dpkg: dependency problems prevent configuration of iot:
iot depends on libssl1.0.0 (>= 1.0.0); however:
Package libssl1.0.0 is not installed.
dpkg: error processing package iot (–install):
dependency problems – leaving unconfigured
Processing triggers for systemd (232-25+deb9u1) …
Errors were encountered while processing:
iot
Please help me in this
Hi,
I’ve same error as walled.
Could you please help on how to install the dependencies.
Thanks
The solution to the libssl dependency issue posted here helped me to resolve Waeleed’s issue:
https://github.com/ibm-watson-iot/device-raspberrypi/issues/17
The solution to the libssl dependency issue posted here helped me to resolve Waleed’s issue:
https://github.com/ibm-watson-iot/device-raspberrypi/issues/17
Hello,
It looks promising.
But I have a little bit of off-topic question:
if I would like to develop my own Watson application where to start?
Thank You in advance.
G.Sz.
Hello Gabor,
Considering that you’ve only mentioned ‘develop my own watson application’, and there isn’t any specification on the Industry, Technology and nature of deployment, I’ll take the luxury of suggesting the following link to get started, which shall help you further drill down your need and come up with your application:
https://developer.ibm.com/code/patterns/?cm_sp=Developer-_-Top-Nav-_-Journeys
Hey IBM:
Thank you for posting this project. My project does not work. (Of course.)
I’m using the Jeff Sloyer’s (IBM) example from YouTube. So, there are differences between this and Jeff’s example.
My Raspberry Pi 3 board is sending data to the Watson IoT Platform, I can see the data going through the IoT platform.
Question #1 – If I can see data from the Pi on Watson, by default does this mean that my device is running in “Registered Mode”?
The Python Cloud Foundry App (WebPage) comes up just fine.
I’m not having an issue with the CLI; but, it seems like it installs the same stuff over and over and takes 5 minutes. Seems long.
When I hit “light on” on the WebPage, I never see a response on the Python board. (I’m using a print statement trying to capture the event.)
Question #2 –
A. How do I debug this?
B. Firewall issue somewhere?
C. How can I know if a message is actually being sent from the Cloud? Bad message? Bad setup?
D. Can I simulate a message from the cloud (IE. curl) to see what response I’m getting back?
I can’t imagine all of the abstractions taking place.
Question #3 – Is there some linkage between Watson and Cloud Foundry that I missed?
Yes, I can go to NodeRed; but, I need to have a Webpage for a mobile device. I’m not sure how to do that in NodeRed.
Thanks!
Rick
On the server side, I’m using the
hello,
i followed this tutorial but i keep getting this error :
“crediantial not authorized ”
“error unexpected disconnect from IBM watson iot platform : 5”
Hello,
how can connect this service in to Pythonflask apliccation?
do you have an example?
Regards