00:03
You've probably heard a lot about Node-rRED and its importance in the IOT
00:07
revolution. But what exactly is it? Simply put Node-RED is an open source
00:12
visual editor that allows programmers of any level to rapidly interconnect
00:16
physical i/o, cloud-based systems, databases, and most API's in any
00:21
combination you can imagine. Node-RED is getting more and more support in the
00:25
growing world of IOT and IIoT. If you're not using it you're missing out on an
00:29
incredibly flexible and powerful tool. In this video I'm going to go over the
00:34
fundamentals of Node-RED and show you how to build a simple
00:36
project or flow to help you get started.
00:44
Node-RED can be run on most modern computer systems including Windows, Mac,
00:48
and Linux pcs or even lightweight computers like the Raspberry Pi or
00:52
industrially hardened appliances like this groov AR1 which is what I'll be
00:56
using for this workshop. Node-RED's website has instructions on how to get
01:00
the application running on whatever system you're using but it's running on
01:04
the groov AR1 by default. So I can just go over to Node-RED by going to my
01:08
boxes host name:1880 and login with my admin user's credentials. This is
01:17
a pretty big deal. By default Node-RED installations are wide open
01:21
but on the groov AR1 we have added both HTTPS and authentication for you. Now
01:28
I can see the main landing page for Node-RED with its three main sections. The
01:33
Node palette on the left, the main workspace front and center, and the
01:39
output pane on the right. The nodes on the left are organized into collapsible
01:44
categories to sort your available nodes.
01:48
You can also search for them in the filter at the top. Most of the nodes you
01:55
see here are included by default called 'core nodes'. You can install external
02:00
nodes to get more functionality as I'll explain later on. These two Opto nodes
02:05
are included on the AR1 by default. Each node performs a specific function
02:09
and what makes Node-RED so flexible and powerful is that the modular nodes are
02:13
self-contained and reusable with most of the JavaScript program logic and API
02:18
overhead hidden in these colored rounded boxes. You get new nodes into your
02:23
program by clicking and grabbing them from the node palette and dragging them
02:26
into the workspace. Here I've brought in one of the key core nodes 'the Inject
02:32
node'. Node-RED uses flow-based programming and so to start the flow
02:37
something will need to inject a message into the flow where the inject node
02:41
pushes a timestamp in by default. As you can see by its label another key core
02:47
node is the output debug node. so I'll drag one of those in now to
02:52
if you check the info tab and then output pane it explains that the debug
02:56
node displays the msg.payload from the flow by default. To actually see
03:01
this message you'll need to come up here and switch to the debug tab. To edit a
03:06
node double click it and a new pane will pop out. Here with the inject node you
03:11
can see that the time stamp is going into msg.payload which is what
03:15
the debug node will output to this window by default. So we don't need to
03:18
change any settings but we do need to connect the pair. As you can see input
03:24
nodes have a small icon on the left side of the node and output nodes have a
03:27
small icon on the right. This represents what kind of action the node takes. In
03:32
the case of the inject you get a small arrow, the debug has a couple of lines,
03:36
and the catch has an exclamation point and so on. Also note that the timestamp
03:44
node has one connection port on the right side. This is for the flow output
03:47
of the node. And the output node has a connection port on the left side of the
03:51
node this is for flow input. To wire two nodes together grab one connection port
03:56
click and hold drag the wire over to another connection port and release. You
04:01
can do this either left to right or right to left it doesn't matter. Now that
04:05
they're connected we need to make the changes active by hitting the deploy
04:08
button in the top right. Before changed nodes are deployed there's a small blue
04:13
circle indicating that there are unsaved changes. After deploying you will see it
04:18
go away and you know that the nodes are live currently the program represented
04:24
by these nodes is running but not actively doing anything since no
04:27
messages have been injected into the flow yet. Force an inject using the blue
04:32
button tab coming out of the timestamp node. This injects unix time - a standard
04:37
that measures the number of milliseconds elapsed since the first of January 1970.
04:41
This number flows along the wire and gets displayed in the debug window by
04:46
the output node. Node-RED recognizes this number as a time stamp. So I can click it
04:52
to see the ISO representation, click it again to see the hexadecimal value, and
04:58
click it once more to return to decimal. The debug shows the content of msg.payload which is what the inject node feeds.
05:06
Let's say I want to see the time every second I can go back to the inject node
05:10
select 'repeat' and change it to the interval of one second. Now I click done,
05:17
deploy the changes, and I'll see the time appear once every second. To stop this
05:24
I'll need to go back, turn the interval off, and save it once again. Now I can
05:33
change what the inject node is inserting into the flow and I can change it from
05:37
time stamp to a string message. To do that
05:40
I double-click the node, click the drop down for time stamp, and make it the
05:45
constant string 'hello world'. Click done, deploy,
05:56
and inject to see the new payload. Now you've seen some basic inputs and outputs
06:04
using the core nodes inject and debug. Let's see how we can use processing
06:07
nodes that have both an input and an output to modify our data. Let's start
06:12
with a simple change node. From the function section scroll down there and
06:18
drag one onto the wire in between the two nodes I already have. You'll see it
06:22
go from solid to dashed to indicate that it's going to automatically wire itself in.
06:28
Double click the node to edit it and here I'll show you how to change the
06:32
msg.payload and search for the word 'world' and replace it with another
06:40
string 'from node red'. Click done, deploy, and inject again to see the modified
06:50
flow. Here in the debug window I can see that it has changed the word 'world'
06:56
to 'from Node-RED' so now I have the full string 'hello fromNode-RED' however this
07:02
change node is relatively limited in what it can do and is not designed to
07:06
loop or make complex logical decisions. Only follow the specific rules entered
07:10
in this window. Thankfully there's an alternative - the function node. Which has
07:16
very few limitations. So let's get rid of this one. You can delete wires by
07:20
clicking on them and pressing delete. And you can get rid of nodes the same way. If
07:24
you make any changes you're unhappy with you can use the shortcut control Z to
07:28
undo them. Note that when I deleted the wires nothing happened to the nodes but
07:33
when I delete the node the wires connected to it are deleted as well. This
07:38
new function node is one of the most powerful in Node-RED.
07:44
Once I connect it and edit it I can see that it exposes a block of JavaScript to
07:49
the user and I can do whatever I wish within this language. In this example
07:54
I'll show how it can replicate the change node functionality by creating a
07:57
new string and using the replace function to look for the world 'world' and
08:01
replace that with 'from Node-RED'. Then I can return a new object with this new
08:09
string as the payload. I'll click done, deploy, and then inject the payload so
08:20
that I can see the new flow in action. Here in the debug window I see that the
08:24
exact same string is outputted as I got from the change node now to get a quick
08:29
example of how new node packages are installed go over to the node-red menu
08:33
click on manage palette and then navigate up to the install
08:39
nodes tab. You can search for any package you'd like. Here but I'm going to search
08:43
for the MS SQL package. This will allow me to connect to a Microsoft
08:48
SQL database. I can click install and since this node has no extra
08:55
dependencies I'm free to bring it in. Once it's done installing I can click
09:01
close and scroll down to the storage section where I see the MS SQL node
09:05
installed right below the core file nodes. So there you have it I have given
09:11
you a brief overview of what the Node-RED interface contains how inject
09:15
nodes can input information have it processed by different function nodes
09:19
and output messages with the debug node. Using user-defined function nodes or
09:23
community built packages.This simple flow based programming tool can do
09:27
incredible things. For more information check out workshops opto22.com and
09:32
node-red org thanks for watching