Linux Format - UK (2019-12)

(Antfer) #1
http://www.techradar.com/pro/linux December 2019 LXF257 51

Node-Red TUTORIALS


A Trigger node is
used to send a
signal and then
reset back to
normal. In our
code we used it to
turn the LEDs on
for a short period.

The Switch node is the equivalent of a conditional statement, in that
the input is tested and the output is routed based on the test.

WHAT IS NODE-RED?


is equal to zero. For this we need == and the same data
format ( number ), and in the textbox enter 0. Add
another line and in this set the test to be if the score is
greater than ( > ) the number 0. When you’ve finished,
click Done.
Up to now we have our tweet being read by the code,
this then scores the sentiment of our tweet and is
routed based on the score. Our switch node now has
three outputs (grey circles) which correspond to the
logic that we have just written. In the last step we will
create three flows, one for each of the outputs.
The first is our negative score. From ‘function’, drag
a trigger node and connect it to the top grey circle of
our switch node. A trigger node will be used to send a
value to a GPIO pin. The values are 1 and 0 (on and off)
and this controls turning the LEDs on and off. Edit the
trigger node as follows.
‘Send’ should be a number (via the drop-down) and
it should be 1. The ‘wait period’ should be changed to 5
seconds (or longer) and in the ‘then send’ section
should be a number , 0. Name the node ‘Negative
message - Flash Red’ and when finished click Done.
Repeat this process for the two remaining outputs. The
name of the middle trigger node should be ‘Neutral
message - Flash Amber’ and the last should be ‘Positive
message - Flash Green’.

Twitter rage
The final nodes are found in the ‘Raspberry Pi’ section:
we need rpi gpio nodes with grey circles on the left
side. Drag one of these nodes and connect it to the
Negative message - flash Red trigger node. We
configure the node as follows. The pin should be
GPIO17 and the ‘initialise pin state?’ should be set
to initial level of pin - low (0). Name the node Red
and click Done.
Drag another rpi gpio node and connect it to the
middle trigger. This node is connected to GPIO27 and
the pin state is again low (0). Name the node Amber.
Drag another rpi gpio node and connect it to the
remaining trigger node. This node is connected to
GPIO22 and is again low (0). Name the node Green.
That’s all of the main nodes configured and ready for

use. But we are going to add two final nodes. From the
‘output’ nodes, drag two debug nodes. These nodes will
send any data to the debug console (right side of the
screen.) Connect one of these nodes to the output of
the Twitter node. This will output any tweets directly to
the debug console. The other debug node is connected
to the output of the sentiment node, but we need to
edit this node so that the output is our sentiment
score, so change the output to msg.sentiment.score
and click Done.
That is all of the code, so click on Deploy in the top
right corner to run it. Now go to Twitter and send a nice
message to your followers. It will take a few moments
for your tweet to be read by the code, but when it does,
you will see the tweet and score in the debug, and the
LED will light up! We have built a sentiment-controlled
LED using Node-RED.

There are many thousands of programming languages in existence
and they are used across a diverse number of platforms and projects.
But what if you cannot code, or need a language that can be quickly
understood with the minimal of training? Well, Node-RED is an ideal
candidate. Developed originally by Nick O’Leary and Dave Conway-
Jones at IBM, this flowbased visual programming tool uses
connected nodes to perform tasks. It can read content from websites
and Twitter, and via libraries it can also be connected to hardware
such as the Raspberry Pi and Arduino (Arduino User anyone?–Ed).
Rather than focus on loops which constantly run code, Node-RED
reacts to inputs and events and the flow of the code is controlled by
the nodes. For example, in our tutorial we used a switch node to
change the direction of a signal based on the sentiment score.
With Node-RED we don’t need to fully explain how the code works,
and instead the intended user can be given enough information to
perform the task. An example of this are the dinosaurs at Blackgang
Chine Fantasy Park on the Isle of Wight, which are coded using Node-
RED via a hack day organised by Prof Lucy Rogers PhD, who also
trains the operators to test and run the dinosaurs.

GET YOUR Pi FILLING HERE Subscribe now at http://bit.ly/LinuxFormat


If you need to
debug your
code, add a
Debug node to
the output of the
node that you
wish to debug.
Remember that
nodes can have
multiple inputs
and outputs,
so we can
have debugs at
every stage in
the process.

5550Decmbr rb0c219sb29howut December 2019 LXF257 51


Node-Red TUTORIALS


A Trigger node is
used to send a
signal and then
reset back to
normal. In our
code we used it to
turn the LEDs on
for a short period.

The Switch node is the equivalent of a conditional statement, in that
the input is tested and the output is routed based on the test.

WHATISNODE-RED?


is equal to zero. For this we need == and the same data
format ( number ), and in the textbox enter 0. Add
another line and in this set the test to be if the score is
greater than ( > ) the number 0. When you’ve finished,
click Done.
Up to now we have our tweet being read by the code,
this then scores the sentiment of our tweet and is
routed based on the score. Our switch node now has
three outputs (grey circles) which correspond to the
logic that we have just written. In the last step we will
create three flows, one for each of the outputs.
The first is our negative score. From ‘function’, drag
a trigger node and connect it to the top grey circle of
our switch node. A trigger node will be used to send a
value to a GPIO pin. The values are 1 and 0 (on and off)
and this controls turning the LEDs on and off. Edit the
trigger node as follows.
‘Send’ should be a number (via the drop-down) and
it should be 1. The ‘wait period’ should be changed to 5
seconds (or longer) and in the ‘then send’ section
should be a number , 0. Name the node ‘Negative
message - Flash Red’ and when finished click Done.
Repeat this process for the two remaining outputs. The
name of the middle trigger node should be ‘Neutral
message - Flash Amber’ and the last should be ‘Positive
message - Flash Green’.


Twitter rage
The final nodes are found in the ‘Raspberry Pi’ section:
we need rpi gpio nodes with grey circles on the left
side. Drag one of these nodes and connect it to the
Negative message - flash Red trigger node. We
configure the node as follows. The pin should be
GPIO17 and the ‘initialise pin state?’ should be set
to initial level of pin - low (0). Name the node Red
and click Done.
Drag another rpi gpio node and connect it to the
middle trigger. This node is connected to GPIO27 and
the pin state is again low (0). Name the node Amber.
Drag another rpi gpio node and connect it to the
remaining trigger node. This node is connected to
GPIO22 and is again low (0). Name the node Green.
That’s all of the main nodes configured and ready for


use. But we are going to add two final nodes. From the
‘output’ nodes, drag two debug nodes. These nodes will
send any data to the debug console (right side of the
screen.) Connect one of these nodes to the output of
the Twitter node. This will output any tweets directly to
the debug console. The other debug node is connected
to the output of the sentiment node, but we need to
edit this node so that the output is our sentiment
score, so change the output to msg.sentiment.score
and click Done.
That is all of the code, so click on Deploy in the top
right corner to run it. Now go to Twitter and send a nice
message to your followers. It will take a few moments
for your tweet to be read by the code, but when it does,
you will see the tweet and score in the debug, and the
LED will light up! We have built a sentiment-controlled
LED using Node-RED.

There are many thousands of programming languages in existence
and they are used across a diverse number of platforms and projects.
But what if you cannot code, or need a language that can be quickly
understood with the minimal of training? Well, Node-RED is an ideal
candidate. Developed originally by Nick O’Leary and Dave Conway-
Jones at IBM, this flowbased visual programming tool uses
connected nodes to perform tasks. It can read content from websites
and Twitter, and via libraries it can also be connected to hardware
such as the Raspberry Pi and Arduino (Arduino User anyone?–Ed).
Rather than focus on loops which constantly run code, Node-RED
reacts to inputs and events and the flow of the code is controlled by
the nodes. For example, in our tutorial we used a switch node to
change the direction of a signal based on the sentiment score.
With Node-RED we don’t need to fully explain how the code works,
and instead the intended user can be given enough information to
perform the task. An example of this are the dinosaurs at Blackgang
Chine Fantasy Park on the Isle of Wight, which are coded using Node-
RED via a hack day organised by Prof Lucy Rogers PhD, who also
trains the operators to test and run the dinosaurs.

GET YOUR Pi FILLING HERE Subscribe now at http://bit.ly/LinuxFormat


If youneedto
debugyour
code,adda
Debugnodeto
theoutputofthe
nodethatyou
wishtodebug.
Rememberthat
nodescanhave
multipleinputs
andoutputs,
sowecan
havedebugsat
everystagein
theprocess.
Free download pdf