Monday, August 14, 2017

IBM Watson IoT (1): connecting a Raspberry Pi as a Device with Node-RED Quickstart


- Layout for this exercise:

1 - Introduction

The IBM Watson IoT Platform is a fully managed cloud-hosted service that makes it simple to derive value from Internet of Things (IoT) devices.

- It provides solutions for device registration, connectivity, control, rapid visualization and storage of data derived from the IoT.

- When combined with the IBM Bluemix environment and access to secure Watson APIs the platform allows to integrate and analyze predictive, cognitive, and contextual analytics for stronger decision-making.

Node-RED is a programming tool for wiring together hardware devices, APIs and online services in new and interesting ways.

- It provides a browser-based editor that makes it easy to wire together flows using the wide range of nodes in the palette that can be deployed to its runtime in a single-click.

- The goal of this exercise is to connect a Raspberry Pi device to the IBM Watson IoT Platform using the Node-RED programming tool and the Quickstart service.

- The Raspberry Pi device will send messages about its CPU temperature every 5 seconds and the IBM Watson IoT platform receives and handles the information displaying the messages at a website.

- Quickstart is a sandbox service provided by Node-RED that runs 24/7 and it's freely accesible. However, it does not provide any security that data is coming from a trusted source, so anybody using the API can send data into the service. That is the reason because in the next exercise we will see how to use Node-RED with a Registered device.

2 - Installing Node-RED nodes at the Raspberry Pi

- Installing a pair of Node-RED nodes for connecting the Raspberry Pi as a Device (also as a Gateway) to the IBM Watson Internet of Things Platform:

- Installing a pair of Node-RED nodes for connecting to both the Quickstart and Registered flows in IBM Watson IoT Platform:

- Starting Node-RED:

- Browsing to localhost IP at port 1880 lets us access the Node-RED editor:

 - Checking that the installation of the two nodes has been successful:

3 - Creating a Node-RED flow

- This flow measures the CPU temperature from the Raspberry Pi and sends it to the IBM Watson IoT platform:

- Let's check the configuration of the nodes.

- The first node is the injection node, what injects messages every 5 seconds from the start:

- The exec node runs the function vcgencmed with the option measure_temp to measure the CPU temperature of the Raspberry Pi device:

- The function node formats the msg.payload:

- The debug node outputs the msg.payload to the Debug tab:

- Finally, the Watson IoT node allow to connect the Raspberry Pi device sending events to the IoT platform. In this case, the Quickstart mode is selected:

- Exporting the flow to the clipboard:

[{"id":"aa1ea7a8.5cac6","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"474020f5.41c758","type":"inject","z":"aa1ea7a8.5cac6","name":"","topic":"","payload":"","payloadType":"date","repeat":"5","crontab":"","once":true,"x":216,"y":212,"wires":[["3377c7a3.ca8418"]]},{"id":"3377c7a3.ca8418","type":"exec","z":"aa1ea7a8.5cac6","command":"vcgencmd","addpay":false,"append":"measure_temp","useSpawn":"","timer":"","oldrc":false,"name":"RaspberryPi_CPU_temperature","x":410,"y":305.5,"wires":[["69a8e49e.1adb7c","21d130ef.8669c8"],[],[]]},{"id":"69a8e49e.1adb7c","type":"debug","z":"aa1ea7a8.5cac6","name":"debug","active":true,"console":"false","complete":"payload","x":845,"y":117.5,"wires":[]},{"id":"21d130ef.8669c8","type":"function","z":"aa1ea7a8.5cac6","name":"msg.payload","func":"msg.payload = {\"d\":{\"temp\":msg.payload.replace(\"temp=\",\"\").replace(\"'C\\n\",\"\")}};\nreturn msg;","outputs":1,"noerr":0,"x":675,"y":313,"wires":[["69a8e49e.1adb7c","a67a1c60.187598"]]},{"id":"a67a1c60.187598","type":"wiotp out","z":"aa1ea7a8.5cac6","authType":"d","qs":"true","qsDeviceId":"a67a1c60.187598","deviceKey":"","deviceType":"","deviceId":"","event":"event","format":"json","qos":"","name":"Watson IoT","x":895,"y":409.5,"wires":[]}]

4 - Deploying the NODE-Red flow in the Quickstart mode

- Clicking the Deploy tab:

- The Debug tab displays the CPU temperature every 5 seconds:

- Going to the Watson IoT node the Quickstart mode triggers the IBM Watson IoT Plaftorm webpage where the series of events gathered every 5 seconds is displayed: