Sunteți pe pagina 1din 6

Lab 1: IoT System Design using Node-RED

At the end of the lab you should be able to:

Install Node-RED on a Windows platform


Use Node-RED to construct a IoT system flow

What is Node-RED?
Node-RED is a software developed by IBM that is based on Node.js.
Node-RED is useful for real-time prototyping of devices (IoT Devices,
Drones, Motors, etc.
In Node-Red, We will be using Nodes. Each node has its own unique
functionality. Nodes are written in JavaScript (for GUI) and Python (for
functions) (or sometimes with the users choice of language such as
Perl.) Unfortunately, at the moment the Node-Red community is tiny
compare to other software and the Nodes are limited but you can write
your own Nodes. Node-Red requires Node.js and npm (Node Package
Manager) on every OS.

Installation of Node.js
Node-RED requires version 0.10.x of Node.js. The most recent stable
release of Node 0.12.x is not currently supported. You can get the
latest
supported
version
of
Node
0.10.x
from
http://nodered.org/docs/getting-started/
Download the Windows Installer: 64-bit; node-v0.10.36-x64.msi
Installation of Node-RED
For Node-RED 0.10.4 or later, the easiest way to install Node-RED is to
use node package manager, npm. Installing it as a global module adds
the command node-red to your system path. Open a command prompt
session and enter commands as follows:
npm install -g node-red
Note: During the install some errors may be reported by the nodegyp command. These are typically non-fatal errors and are related to

Lab 1

Page 1

optional dependencies that require a compiler in order to build


them. Node-RED will work without these optional dependencies.

Enter the command as follows:


node-red
You'll see the outputs as follows:

Use a browser and enter command below to see Node-RED!


localhost:1880

Lab 1

Page 2

Note* upon running Node-Red, do not close the command prompt


session unless you want to quit the Node-RED.

Creating a flow using Node-RED


In order to integrate the JavaScript on to the Node-RED System, we will
need to use the following node:

Inject Node
Debug Node
Function Node
Xively Node

Inject Node
The inject node of the Node-RED system is used to start the system
activity or function. Inject node can set a timer to start the functions
which are created by the System. Pressing the button on the left side
of the node allows a message on a topic to be injected into the flow.
This is mainly for test purposes.

Lab 1

Page 3

Debug Node
The debug node is used to check whether result is received when
executing the function. The Debug node can be connected to the
output of any node. It can be used to display the output of any
message property in the debug tab of the sidebar. The default is to
display msg.payload.

Function Node
The function node of the Node-RED system is used to validate the JSON
data pass over the flow of the system. Function allows you to write
code to do more interesting things. The message is passed in as a
JavaScript object called msg .

Create your First Node-RED flow


http://nodered.org/docs/getting-started/first-flow.html
1.

Add an Inject node

The Inject node allows you to inject messages into a flow, either
by clicking the button on the node, or setting a time interval
between injects. Drag one onto the workspace from the palette.

Open the sidebar (Ctrl-Space, or via the dropdown menu) and


select the Info tab.
Select the newly added Inject node to see information about its
properties and a description of what it does.

Lab 1

Page 4

2.

Add a Debug node


The Debug node causes any message to be displayed in the
Debug sidebar. By default, it just displays the payload of the
message, but it is possible to display the entire message object.

3.

Wire the two together


Connect the Inject and Debug nodes together by dragging
between the output ports of one to the input port of the other.

4.

Deploy
At this point, the nodes only exist in the editor and must be
deployed to the server. Click the Deploy button. With the Debug
sidebar tab selected, click the Inject button. You should see
numbers appear in the sidebar. By default, the Inject node uses
the number of milliseconds since January 1st, 1970 as its
payload. Lets do something more useful with that.

5.

Add a Function node


The Function node allows you to pass each message though a
JavaScript function.
Wire the Function node in between the Inject and Debug nodes.
Youll need to delete the existing wire (select it and hit delete on
the keyboard).

Double-click on the Function node to bring up the edit dialog.


Copy the follow code into the function field:

//

Create a Date object from the payload


var date = new Date(msg.payload);

Lab 1

Page 5

//

Change the payload to be a formatted Date string


msg.payload = date.toString();

//

Return the message so it can be sent on


return msg;

Click Ok to close the edit dialog and then click the deploy button.
Source Code for the Node-RED flow is represented by the following json.
It can be imported straight into the editor by pasting the json into the
Import dialog (Ctrl-I or via the dropdown menu).
[{"id":"58ffae9d.a7005","type":"debug","name":"","active":true,"compl
ete":false,"x":640,"y":200,"wires":[]},
{"id":"17626462.e89d9c","type":"inject","name":"","topic":"","payload"
:"","repeat":"","once":false,"x":240,"y":200,"wires":
[["2921667d.d6de9a"]]},
{"id":"2921667d.d6de9a","type":"function","name":"Format
timestamp","func":"// Create a Date object from the payload\nvar date
= new Date(msg.payload);\n// Change the payload to be a formatted
Date string\nmsg.payload = date.toString();\n// Return the message so
it can be sent on\nreturn msg;","outputs":1,"x":440,"y":200,"wires":
[["58ffae9d.a7005"]]}]

Create your Second Node-RED flow


http://nodered.org/docs/getting-started/second-flow.html

Lab 1

Page 6

S-ar putea să vă placă și