Linux Format

NODE-RED: Monitor Twitter

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.

- Les Pounder is a freelance maker who works with organisati­ons such as the Raspberry Pi Foundation to promote maker skills.

Les Pounder shows us how to use a Pi and NODE-RED to read the sentiment of a tweet and control a colour-coded LED.

The 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 introducti­on 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 applicatio­n, 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-credential­s’, and then enter your

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 conditiona­l 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

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.

 ??  ?? In NODE-RED the code is made into a flow, where nodes are connected to perform a task.
In NODE-RED the code is made into a flow, where nodes are connected to perform a task.
 ??  ??
 ??  ?? 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.
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 conditiona­l statement, in that the input is tested and the output is routed based on the test.
The Switch node is the equivalent of a conditiona­l statement, in that the input is tested and the output is routed based on the test.

Newspapers in English

Newspapers from Australia