Overview

Skill Level: Beginner

Beginner

Connect your Raspberry Pi to IBM Watson IoT Platform in simple steps and visualize the data generated by the Pi in Watson IoT Platform dashboard. Also, you can use a Node-RED application to receive events and send commands back to the device.

Ingredients

Hardware

1. Raspberry Pi Model B/ Model B+/ 2

2. Minimim 8GB SD card with Rasbian operating system

Software

1. Bluemix Account

2. Watson IoT Platform Service

3. Node-RED Application

Step-by-step

  1. Get your Device and Bluemix setup ready

    Get your Device ready:

    Connect your Raspberry Pi to the internet. Ensure you are able to reach (Ping) and connect to it.

    Getting the Bluemix services ready:

    This IoT Recipe makes use of the Create Toolchain button to help deploy the necessary services on to Bluemix. Click on the Create Toolchain button provided below, provide a custom name to your application and choose to click on Create button, to quickly deploy the Watson IoT Platform – Node-RED Bluemix starter app. Post successful deployment, you shall have both the above mentioned services, up & running on Bluemix along with a Cloudant NoSQL DB at your disposal.

    Toolchain-8

    Note: If you are a User using the United Kingdom Region in your Bluemix environment, then please make use of the steps mentioned in the IoT Recipe Deploy Internet of Things Platform Starter service on Bluemix to deploy the setup. Alternatively, you can also try using the Deploy to Bluemix button, to deploy the setup under your United Kingdom Region, provided your Jazzhub account is validated. Users of US South Region can ignore this step.

    deploy

  2. Connect (Quickstart)

    Watson IoT Platform Quickstart connection

    1. Download the installer from GitHub:
      curl -LO https://github.com/ibm-messaging/iot-raspberrypi/releases/download/1.0.2.1/iot_1.0-2_armhf.deb 

      (Source code is also available)

    2. Install the package with:
      sudo dpkg -i iot_1.0-2_armhf.deb 
    3. The iot process starts and publishes events to theIBM Watson IoT Platform Quickstart. The process runs as a system service, and starts whenever Raspberry Pi starts. To verify the process is running, use the command:
      service iot status
    4. You can stop the iot process with the following command:
      sudo service iot stop
    5. To uninstall the package, run:
      sudo dpkg -P iot

      Note: For troubleshooting, go to /var/log/syslog to find more information about the iot process.

  3. Visualize

    Real-time visualization of device data

    1. Find the MAC address of your Raspberry Pi by running the following service command:

      service iot getdeviceid
      Enter your MAC address here to visualize your data.
  4. Register your Device In Watson IoT Platform

    To explore and make use of full capabilities of Watson IoT Platform, we need to setup and register your device(s) in it. This section shows how you can setup the same.

    Open the Bluemix Dashboard and click on the Application service that you have currently deployed (using the Create Toolchain button), if you have already moved away from it. You should see the Watson IoT Platform listed under the Connections. Click on the WIoTP service and choose the click on Launch button to launch the WIoTP Dashboard.

    Carry out the steps present in this recipe to register your device(s) in IBM Watson Internet of Things Platform.

    devicecredentials

    At this step, we have successfully created the Watson IoT service and registered your device(s) in it.

  5. Connect (Registered)

    Watson IoT Platform Registered connection

    1. During the device registration process you will get file configuration information containing the following details, copy these when you get them.
      Organization ID
      Device Type ID
      Device ID
      Authentication Method
      Authentication Token
    2. Stop the iot process
      sudo service iot stop
    3. Type in the following command
      sudo nano /etc/iotsample-raspberrypi/device.cfg
    4. Create a new device.cfg file at /etc/iotsample-raspberrypi fill in the configuration file with the details that you copied when registering the device.
      #Device configuration file
      org = yourOrganizationCode
      type = iotsample-raspberrypi
      id = b827eba84426
      auth-method = token
      auth-token = yourAuthToken
      #End of Configuration file

      Note: All the properties in the configuration file are mandatory. Currently, only token based authentication is supported.

    5. Save your file by pressing CTRL-X in the Nano editor and follow the steps on the screen.
    6. Start the iot process for the device to start in registered mode

      sudo service iot start
  6. Visualize the Raspberry Pi events in Watson IoT Platform dashboard

    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.

  7. Access the Node-RED application to receive events from the device

    With events now being sent to Watson IoT, the next step is to access the application in Bluemix to receive the events.

     Access the Node-RED application in Bluemix

    1. You have deployed the Node-RED service on to the Bluemix by clicking on the Create Toolchain button in Pre-requisites section. Access your Node-RED application by clicking on View App link and then choosing the option Go to you Node-RED flow editor, to access the editor.
    2. Parallelly, you can also access the Node-RED editor directly by accessing the following URL
        http://<your-application-name>.mybluemix.net/red

      Note: The Node-RED Editor opens up with a default flow. Click on the + sign to add a new tab and continue with the steps mentioned here after.

    Create a simple flow to start receiving the device events

    • Open the Node-RED flow editor,
    • Add an ibmiot input node and configure it to use Bluemix Service authentication. Set the Device Type field to match the type you used to register your device with. Leave the Device Id and Event fields set to all.receive-events-in-node-red_1
    • Attach the debug node to the IBM IoT node as shown below to output the datapoints in the debug panel.receive-events-in-node-red
    • Click Deploy to deploy the flow in Node-RED and observe that the readings are printed in the console.

    In this step, we have successfully configured the IBM IoT App In Node to receive the Raspberry Pi events from the Watson IoT Platform.

  8. Send commands to your Raspberry Pi device

    When the Raspberry Pi Sample is running in registered mode, it supports the receipt of Commands sent by an Application.

    This sample supports the Reboot Command. When the user sends this command, the Raspberry Pi will reboot based on the seconds provided in the payload.

    • Drag-n-Drop an inject node and configure the payload as follows,trigger-reboot
    • As shown the above payload instructs the sample to wait for 3 seconds and reboot the device.
    • Drag and drop ibmiot out node from the output pane and configure it to use Bluemix Service authentication. Set the Device Type, Device Id fields appropriately. Set the Output type as Device Command and connect it to the reboot trigger node as shown below,edit-command
    • The flow should look like the following,
      send-command
    • Click deploy to deploy the flow.
    • Click on the Trigger reboot node to send the reboot command to Raspbery Pi device.

    In this step, we saw how to send a command to the device to control it using a Node-RED application.

  9. Where to go from here?

    As a next step, go through the following recipes that will help you to create an end to end IoT solution,

35 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

Join The Discussion