Node-RED is a tool for wiring together hardware devices, APIs and online services in new and interesting ways.
Node-Red is a very light weighted run time built on top of Node.js, taking full advantage of its event-driven, non-blocking model of operation.
Watch the video here
Node-Red allows you to create flows using drag and drop functionality to connect together inputs, outputs and web services to create an application. We are using a Raspberry Pi to run Node Red. On instructions to install Node-Red on the Raspberry Pi, follow this link https://nodered.org/docs/hardware/raspberrypi
A Node-Red node is a function block which consists of at least one input, some internal logic, and zero or more output. Data is usually carried via the msg.payload though it can be carried by other properties under msg.
A node can also be configured to access to certain resources such as file from the file system, a database, MQTT messages, or the local hardware I/O, for example, I/O pins on a Raspberry Pi.
The MQTT nodes are for talking with the MQTT brokers. The broker we have used is iot.eclipse.org
Create the following flow in Node-Red.
1) Twitter input node.
Can be used to search either:
- the public or a user's stream for tweets containing the configured search term
- all tweets by specific users
- direct messages received by the authenticated user
Use space for and and comma , for or when searching for multiple terms.
to tweets/ and then appends the senders screen name.
to the tweeters location if known.
to the full tweet object.
2) Sentiment Node
and adds a
object that contains the resulting AFINN-111 sentiment score as
A score greater than zero is positive and less than zero is negative.
The score typically ranges from -5 to +5, but can go higher and lower.
3) Function Node
A function block where you can write code to do more interesting things.
By convention it will have a
property containing the body of the message.
Our function Node Looks something like this.
4) MQTT Node
Connects to a MQTT broker and publishes messages.
The topic used can be configured in the node or, if left blank, can be set by
Likewise the QoS and retain values can be configured in the node or, if left blank, set by
msg.retain respectively. By default, messages are published at QoS 0 with the retain flag set to false.
msg.payload contains an object it will be converted to JSON before being sent.
Here is how our MQTT Node Configuration Looks Like. The Topic Field is case sensitive and should be customised, because devices subscribed to same topic (/Idiotware/twitterpetcube) on same server (iot.eclipse.org) will recieve messages published from any device that sends it to the same server and topic.
To test the flow, hit the Deploy button on the upper Right Corner. The debug nodes help show the data sent by the sentiment and function nodes.
If all goes well, you should see the debug console ( right side, tab on top) display a number whenever your tweet to the handle configured in the Twitter Node.
The ESP-Link firmware is standard for the Idiotware Shield. This firmware has settings to configure MQTT. As show below the server is set to iot.eclipse.org and topic updates received from MQTT will be available on the Arduino with the help of the Idiotware Shield.
The sentiment value is sent to MQTT broker iot.eclipse.org by Node Red Flow. Once this is received by the broker, it sends it downstream to all subscriber nodes which are listening for data on a specific topic.
In our case, the topic /Idiotware/twitterpetcube is subscribed by the Idiotware Shield and is the recipient of the data published on that topic.
There is a section in the code that can be changed to a specific, custom topic to subscribe to. Once this topic is changed to what has been set in Node-Red, the sentiment value will be received on the Idiotware Shield, and the code will choose an appropriate graphic to show on the OLED.