Overview

Skill Level: Beginner

Beginner

IntroductionThe Watson IoT Node is a pair of Node-RED nodes for connecting your device to the IBM Watson Internet of Things Platform as a Device or as a Gateway.The previous recipe showcased how you can use the Watson IoT node to connect Raspberry Pi, as a device, to the IBM Watson IoT Platform. In this […]

Ingredients

Hardware

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

2. Minimim 8GB SD card with Rasbian operating system(Recommended Raspberry Jessie)

Step-by-step

  1. Introduction

    The Watson IoT Node is a pair of Node-RED nodes for connecting your device to the IBM Watson Internet of Things Platform as a Device or as a Gateway.

    The previous recipe showcased how you can use the Watson IoT node to connect Raspberry Pi, as a device, to the IBM Watson IoT Platform.

    In this recipe, you will learn

    1. How to install Watson IoT Node in Raspberry Pi (This is not needed, in case you have already carried out this step while following the previous recipe),
    2. Connect the Raspberry Pi, as a gateway, to the IBM Watson IoT Platform, and
    3. Learn how to send gateway events to the platform and receive gateway commands from it.

    For more information about the Watson IoT Node please click here

  2. Watson IoT Node on Raspbian Jessie

    The latest version of Raspbian Jessie has both Node-RED and Watson IoT node pre-installed. However, the Watson IoT node is not the latest. To get the latest version of the Watson IoT node, carry out the following steps.

    • Update Node-RED to the latest version

    sudo apt-get update

    sudo apt-get install nodered

    At this stage we have installed the latest version of Watson IoT Node in Raspberry Pi.

    If you intend to use the browser in the PI then the current recommended browser for use with Node-RED is the Iceweasel browser. This can be installed by the following:

      sudo apt-get install iceweasel  

  3. Starting Node-RED

    You can start the Node-RED by running the command node-red-start in the shell prompt. The following output would be displayed in the shell.

    node-red-start

    Start Node-RED

    Once Node-RED has started, point a browser at http://9.20.202.223:1880

    On Pi Node-RED works better with the Iceweasel browser



    Use node-red-stop to stop Node-RED

    Use node-red-start to start Node-RED again

    Use sudo systemctl enable nodered.service to autostart Node-RED at every boot

    Use sudo systemctl disable nodered.service to disable autostart on boot



    To find more nodes and example flows - go to http://flows.nodered.

    You may also need to install and upgrade npm

    sudo apt-get install npm

    sudo npm i -g npm@2.x

    Warning: Unit file of nodered.service changed on disk, 'systemctl daemon-reload' recommended



    Starting Node-RED graphical event wiring tool....

    Started Node-RED graphical event wiring tool...

    Welcome to Node-RED

    ===================

    21 Apr 12:24:57 - [info] Node-RED version: v0.13.4

    21 Apr 12:24:57 - [info] Node.js version: v0.10.29

    21 Apr 12:24:57 - [info] Linux 4.1.19-v7+ arm LE

    21 Apr 12:24:57 - [info] Loading palette nodes

    21 Apr 12:25:08 - [info] Settings file : /home/pi/.node-red/settings.js

    21 Apr 12:25:08 - [info] User directory : /home/pi/.node-red

    21 Apr 12:25:08 - [info] Flows file : /home/pi/.node-red/flows_my-pi.json

    You can then access the Node-RED editor by entering http://localhost:1880 in the browser address of Raspberry Pi.

    To connect to the Node-RED editor via network:

    Once Node-RED is running – open the browser program in the host machine and then browse the address http://{the-ip-address-returned}:1880/. One way to find the IP address of the Pi is to use the command

     hostname -I

    After this step you will be able to access Node-RED editor, with the Watson IoT Nodes, in it using which you can send events to the IBM Watson IoT Platform and receive commands from the same.

  4. Registering your Gateway In Watson IoT Platform

    To explore and make use of the full capabilities of Watson IoT Platform (including bidrectional flow), we need to setup and register your gateway in the platform. This section shows how you can setup the same.

    Carry out the steps present in this recipe to register your gateway in IBM Watson Internet of Things Platform.

    At this step, we have successfully created the Watson IoT service and registered your gateway in it.

  5. Sending Gateway Events to Watson IBM IoT Platform

    Gateway runs only in the registered flow, which is why there is no Quickstart section in this recipe, unlike the Device recipe. In this section you would deploy a Node-RED flow in your Raspberry Pi. The flow does the following

    1. Every 5 seconds it gets triggered using the inject node.
    2. It reads the CPU temperature in the exec node.
    3. Creates a JSON object for the temperature reading in the function node.
    4. It posts this CPU temperature, to IBM Watson IoT Platform using the Watson IoT node

    Carry out the steps given below

    • In the Node-RED editor, which you have opened in the browser (either on Raspberry Pi or on the host system), click on Menu > Import -> Clipboard
    • Copy the JSON from this link and paste it in the clipboard. This code is not yet complete as you need to provide the credentials. That is why you would see a small triangular in the right side of the node.

    • The four nodes in this flow are
    1. Inject node set to repeat every 5 seconds
    2. Exec node set to call out to an external program vcgencmd measure_temp – that reads the Pis internal CPU temperature.
    3. Function node that extracts the number from the temperature reading and formats it into a JSON packet for the Watson IOT server.
    4. Debug node so you can see this payload
    5. WIoTP node that sends the payload to the Watson IOT server in Bluemix.

    Once you have registered your gateway in the Watson IoT organization make the following modifications to add the credentials.

    • Double click on the Gateway node.

    • Select connection as Registered .

    • Select edit button in Credentials.

    • Fill the Organization, Device Type, Device ID, Auth Token and then click on the Add button.

    • Fill in the Device Type with the Gateway type Id and Device Id with the Gateway Id once again as filled previously. Provide the exact gateway event type publishing

    • Click on the Ok Button.
    • Click on deploy button to start.

    Now the CPU temperature from your Raspberry Pi is sent to the Watson IoT Platform once every 5 seconds, as a gateway event. You can also verify that in the debug tab.

    Now Node-RED flow will start sending events to your organization.

  6. Receiving Gateway Commands From IBM Watson IoT Platform

    In this section we would make the Raspberry Pi accept commands published over Watson IoT. The flow has just 2 nodes

    1. Watson IoT node which subscribes to commands (either all or specific command)
    2. Debug node which prints the commands it received.

    Please follow the step Registering your Gateway in Watson IoT Platform given above, in case you have not registered your device.

    • In the Node-RED editor, which you have opened in the browser (either on Raspberry Pi or on the host system), click on Menu-> Import – >Clipboard.
    • Copy the json from this link and paste it in that clip board.
    • The flow is not yet complete as you need to provide credentials. That is why you can see a small red coloured triangle at the right side on the top of the node.
    • Double click on cmd receiver.

    • In case you have already provided the credentials in the Node-RED flow (by following the above section), you need not carry out the below steps and can straight click Ok button and deploy, else please read on
    • Select edit button in Credentials.

    • Fill the Organization, Device Type, Device ID, Auth Token and then click on add button.

    • Click on the Ok Button.
    • Click on the deploy button to start the flow.

    Note : You will need an external application that is sending commands to this Raspberry Pi Device.

    Now you can receive the commands from Watson IoT Platform which will be displayed in the debug tab.

  7. Conclusion

    We have seen how to deploy Watson IoT Node in the Raspberry Pi as a gateway and send gateway events to the IBM Watson IoT Platform and receive gateway commands from the Platform using Node-RED without any programming.

  8. Where to go from here?

    The next part of the recipe will show how to use the Raspberry Pi, as a gateway, to send events on the behalf of a device and subscribe to commands, on the behalf of a device.

Join The Discussion