Overview

Skill Level: Beginner

Beginner

Connect your Raspberry Pi to IBM Watson IoT Platform in less than 5 minutes

Ingredients

  1. Raspberry Pi
  2. IBM Watson IoT Platform
  3. The details for a registered device in your IBM Watson IoT Platform organization

Step-by-step

  1. 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.

     

  2. 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
  3. 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.

    Quickstart Vizualization

  4. 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:

    1. WIOTP_IDENTITY_ORGID
    2. WIOTP_IDENTITY_TYPEID
    3. WIOTP_IDENTITY_DEVICEID
    4. 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)

     

  5. 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.

  6. 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.

46 comments on"Connect a Raspberry Pi to IBM Watson IoT Platform"

  1. KiranShashi August 05, 2015

    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.

    • Vikki Paterson August 05, 2015

      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(

  2. KiranShashi August 05, 2015

    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.

    • Vikki Paterson August 05, 2015

      The API key authentication tokens are non recoverable for security reasons, this is stated at the time of creation.

  3. KiranShashi August 07, 2015

    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 = ********

    ===

  4. KiranShashi August 11, 2015

    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

  5. […] 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 […]

  6. 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

  7. 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.

    • Jeffrey Dare January 28, 2016

      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!

        • Jeffrey Dare January 29, 2016

          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!!

  8. Hello, where exactly is the code for logging and command?

  9. 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.

  10. 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.

    • Recipes@IoTF October 21, 2016

      Looks like a write permission issue. Can you try creating a sample file and see whether its allowed? or can you use sudo access?

  11. 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()

  12. 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.

  13. How can I troubleshoot Raspberry Pi with IOT not connecting with registered device.cfg file?

  14. YMDH_sathish_Palaniappan March 22, 2017

    The source code is present here – https://github.com/ibm-watson-iot/iot-raspberrypi, Can you build the service with debug points and try?

  15. 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!

  16. blackzero July 15, 2017

    can i use another OS like Ubuntu , and raspberry pi 3 , please let me know

  17. 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 }

  18. preetyjess01 November 21, 2017

    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.

  19. waleed swati November 23, 2017

    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

  20. waleed swati November 29, 2017

    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

  21. Hi,
    I’ve same error as walled.
    Could you please help on how to install the dependencies.
    Thanks

  22. 2WNN_Des_Adkinson January 03, 2018

    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

  23. 2WNN_Des_Adkinson January 03, 2018

    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

  24. gabor.szepes March 26, 2018

    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.

  25. 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

Join The Discussion