Node-RED
is a visual tool for wiring together hardware devices, APIs and online services – for wiring the Internet of Things.
Node-RED
provides a browser-based flow editor that makes it easy to wire together flows using the wide range nodes in the palette.
Flows can be then deployed to the runtime in a single-click. JavaScript functions can be created within the editor using the a rich text editor.
A built-in library allows you to save useful functions, templates or flows for re-use.
The light-weight runtime is built on Node.js
, taking full advantage of its event-driven, non-blocking model.
This makes it ideal to run at the edge of the network on low-cost hardware such as the Raspberry Pi
as well as in the cloud.
1 Install node red on Raspberry Pi
As of the November 2015 version of Raspbian Jessie, Node-RED comes preinstalled on the SD card image that can be downloaded from RaspberryPi.org.
If you have an older version, you can install it doing:
sudo apt-get update sudo apt-get install nodered
2 Running node red
To start Node-RED, you can either:
- on the Desktop, select
Menu -> Programming -> Node-RED
. - or run
node-red-start
in a new terminal window.
To stop Node-RED, run the command node-red-stop
.
3 Connect to Watson IoT platform using Node-RED
In the following example we will publish Raspberry Pi
CPU temperature to the Watson IoT
cloud Platform using Node-RED
- Let's check the
Raspberry Pi
board temperatureCopy$ vcgencmd measure_temp temp=51.0'C
- Now start
nodered
on RaspberryCopy$ node-red-start
- Connect to nodered using a web browser. You can see nodered console.
Copy
http://raspberrypiaddress:1880/
-
From
input
, add inject block (timestamp) -
From
advanced
, addexec
block that will get CPU temp and fill in the exec node data as follows - Wire timestamp output to getCPUtemp input
-
From
output
, add a debug block and wire getCPUTemp output to debug input -
Deploy the changes, and select debug tab
-
Click on timestamp left checkbox to inject an event.
You should see the temperature on debug window foreach click like
9/4/2016, 1:23:40 AMc708accf.a751c msg.payload : string [12] temp=51.0'C
-
Add a function to trasform output from getCPUTemp to a format appropriate for Watson IoT.
- Transform the temperature to the format required for
Watson IoT
using a functionCopymsg.payload = {'d' : {'temp': msg.payload.replace("temp=","").replace("'C\n","")}} return msg;
- Wire the output of getCPUTemp to the input of the function.
- Wire the output of the function to the debug input.
- Deploy the changes, and send a new timestamp.
- You should see function output like
{ "d": { "temp": "50.5" } }
- Change timestamp to repeat automatically every 3 seconds.
- From output, select a Watson IoT block and wire the function output to Watson input.
- Deploy the changes, so time event forces data collection every 3 seconds.
- Now, open watson IoT block and click on
Quickstart Id
right link. - You can see real time CPU temperature is beeing send from your Raspberry Pi box to IBM Watson Cloud plaform.