Linux Format - UK (2019-12)

(Antfer) #1
50 LXF257 December 2019 http://www.linuxformat.com

TUTORIALS Node-Red


Twitter API key details. When done, click Add and the
node will update and show a connection. Now we need
to edit the node properties so that it searches “the
tweets of specific users” – in this case,my Twitter
account. For the name we call the node ‘Happy or not?’
and click Done.

Naughty or nice node?
This node will read all of my tweets and the data from
the tweet is going to be passed on to the next node
which is called ‘sentiment’, and is found in the ‘analysis’
section of nodes. Join the ‘Twitter’ node to the
‘sentiment’ node. The sentiment node checks the
content of our tweet against a word list, and if the
contents are deemed to be negative, a negative number
is outputted. If the content is neutral then 0 is
outputted. Positive content creates a positive number.
The next node is called ‘switch’ and is found in the
‘function’ nodes. Drag this node and connect the output
from sentiment to the input of the switch node. A
switch node works in the same manner as a conditional
statement: it routes the flow of our code based on logic.
In this case it will route the flow based on the sentiment
score. Double-click the switch node and edit as follows.
The name of the node is ‘Check the sentiment’ and
the Property is msg.sentiment.score. Then we need to
add three conditions to test. The first is if the score is
less than the number 0. So change this to < and make
sure that in the drop-down, the data format is set to
number , then add the number 0 in the text box. Now
click +add and in the next line we will check if the value

he Internet of Things (IoT) is a weird and
wonderful place, and in this tutorial we will
make it a little weirder by creating a tool that
reads the sentiment of our tweets and rates them
according to a list of positive and negative words. If the
tweet is positive, a green LED will light up; if it’s negative
a red LED will light. Neutral will see a yellow LED light.
This is a fun introduction to learning how we can
measure the mood of a tweet and create novel outputs.
We’ve kept components for this project simple. We
have three LEDs (red, yellow and green) connected to
pins GPIO17, 27 and 22 via their long leg, and connected
to GND via their short leg and a 330 ohm resistor.

Software setup
On the Raspberry Pi, we need to start the Node-RED
application, either via SSH or by attaching a keyboard
and mouse and screen. In a terminal type the following:
$ node-red-start
In the output look for the following message – the IP
address will be different.
Once Node-RED has started, point a browser at
http://192.168.0.4:1880
From another computer on the same network as the
Pi, go to this IP address and you will be greeted with the
Node-RED interface. Before we start any code we first
need to sign up for a Twitter Developer account at
https://developer.twitter.com and then go to
https://developer.twitter.com/en/apps to create
an app. Answer all of the required questions and the
app will be created. Then click ‘Keys and tokens’ and
make a note of the Consumer API keys (API key and
API secret key), and then under ‘Access token & access
token secret’, click Create and make a note of the
Access token and Access token secret. Do not share
these keys with anyone!
Back in Node-RED, there are a selection of nodes
(blocks of code/functions) that we can drag into a Flow
(the sequence of code). We can join these nodes
together by drawing lines from the grey circle on a node


  • left-click the mouse button and drag to connect.
    The first node we shall use is from ‘social’ and it is
    the ‘Twitter’ input node (grey circle on right side.)
    Double-click the node and click the edit button next to
    ‘Add new twitter-credentials’, and then enter your


In Node-RED the code is made into a flow, where nodes are connected
to perform a task.

Monitor the mood


of Twitter tweets


Les Pounder
is a freelance
maker who works
with organisations
such as the
Raspberry Pi
Foundation to
promote maker
skills.

Any model of
Raspberry Pi
The latest
Raspbian
3x LEDs (red,
yellow, green)
3x 330 ohm
Resistors
Breadboard
3x female-
to-male
jumper wires
Computer
with net
connection
Code:
http://bit.ly/
lxf257twit

T


NODE-RED


OUR
EXPERT

YOU NEED


Les Pounder shows us how to use a Raspberry Pi and Node-RED to read


the sentiment of a tweet and control a colour-coded LED.


50 LXF257 December 2019 http://www.linuxformat.com


TUTORIALS Node-Red


TwitterAPIkeydetails.Whendone,clickAddandthe
nodewillupdateandshowa connection. Now we need
toeditthenodepropertiessothat it searches “the
tweetsofspecificusers”– inthiscase,my Twitter
account.Forthenamewecallthe node ‘Happy or not?’
and click Done.

Naughty or nice node?
This node will read all of my tweets and the data from
the tweet is going to be passed on to the next node
which is called ‘sentiment’, and is found in the ‘analysis’
section of nodes. Join the ‘Twitter’ node to the
‘sentiment’ node. The sentiment node checks the
content of our tweet against a word list, and if the
contents are deemed to be negative, a negative number
is outputted. If the content is neutral then 0 is
outputted. Positive content creates a positive number.
The next node is called ‘switch’ and is found in the
‘function’ nodes. Drag this node and connect the output
from sentiment to the input of the switch node. A
switch node works in the same manner as a conditional
statement: it routes the flow of our code based on logic.
In this case it will route the flow based on the sentiment
score. Double-click the switch node and edit as follows.
The name of the node is ‘Check the sentiment’ and
the Property is msg.sentiment.score. Then we need to
add three conditions to test. The first is if the score is
less than the number 0. So change this to < and make
sure that in the drop-down, the data format is set to
number , then add the number 0 in the text box. Now
click +add and in the next line we will check if the value

heInternetofThings(IoT)isa weirdand
wonderfulplace,andinthistutorialwewill
makeit a littleweirderbycreatinga toolthat
readsthesentimentofourtweetsandratesthem
accordingtoa listofpositiveandnegativewords.If the
tweetispositive,a greenLEDwilllightup;if it’snegative
a redLEDwilllight.Neutralwillseea yellowLEDlight.
Thisisa funintroductiontolearninghowwecan
measurethemoodofa tweetandcreatenoveloutputs.
We’vekeptcomponentsforthisprojectsimple.We
havethreeLEDs(red,yellowandgreen)connectedto
pinsGPIO17, 27 and 22 viatheirlongleg,andconnected
toGNDviatheirshortleganda 330ohmresistor.

Softwaresetup
OntheRaspberryPi,weneedtostarttheNode-RED
application,eitherviaSSHorbyattachinga keyboard
andmouseandscreen.Ina terminaltypethefollowing:
$ node-red-start
In the output look for the following message – the IP
address will be different.
Once Node-RED has started, point a browser at
http://192.168.0.4:1880
From another computer on the same network as the
Pi, go to this IP address and you will be greeted with the
Node-RED interface. Before we start any code we first
need to sign up for a Twitter Developer account at
https://developer.twitter.com and then go to
https://developer.twitter.com/en/apps to create
an app. Answer all of the required questions and the
app will be created. Then click ‘Keys and tokens’ and
make a note of the Consumer API keys (API key and
API secret key), and then under ‘Access token & access
token secret’, click Create and make a note of the
Access token and Access token secret. Do not share
these keys with anyone!
Back in Node-RED, there are a selection of nodes
(blocks of code/functions) that we can drag into a Flow
(the sequence of code). We can join these nodes
together by drawing lines from the grey circle on a node


  • left-click the mouse button and drag to connect.
    The first node we shall use is from ‘social’ and it is
    the ‘Twitter’ input node (grey circle on right side.)
    Double-click the node and click the edit button next to
    ‘Add new twitter-credentials’, and then enter your


InNode-REDthecodeismadeintoa flow, where nodes are connected
toperforma task.

Monitor the mood


of Twitter tweets


LesPounder
isa freelance
makerwhoworks
withorganisations
suchasthe
RaspberryPi
Foundationto
promotemaker
skills.


Anymodelof
RaspberryPi
Thelatest
Raspbian
3xLEDs(red,
yellow,green)
3x 330 ohm
Resistors
Breadboard
3x female-
to-male
jumperwires
Computer
withnet
connection
Code:
http://bit.ly/
lxf257twit

T


NODE-RED


OUR
EXPERT

YOU NEED


Les Pounder shows us how to use a Raspberry Pi and Node-RED to read


the sentiment of a tweet and control a colour-coded LED.

Free download pdf