Twitter Sentiment Gadget
Using the sentiment node in Node-Red, we are analysing a twitter feed to find out if the sentiment if the feed is happy, sad or neutral.
Things used in this project
Hardware components
- Arduino UNO x 1
- Idiotware Shield x 1
- USB-A to B Cable x 1
- Generic Jumper (0.1″) x 2
Software apps and online services
- Arduino IDE
Story
Step 1: Introduction to Node-Red
Node-RED is a tool for wiring together hardware devices, APIs and online services in new and interesting ways. Node-Red is a browser based tool to let you quickly and easily construct your own logic of handling various IoT devices, including subscribing the MQTT messages, listening to HTTP requests, reading and writing to Databases, publishing to MQTT brokers and responding to HTTP requests. It also allows you to define specific functions written in JavaScript to provide complex logic operations, while using a visual, easy to use, drag-and-drop user interface to link different components and logic together. 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.
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
Step 2: Connecting Node-Red to Twitter
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 termall tweets by specific usersdirect messages received by the authenticated userUse space for and and comma , for or when searching for multiple terms. Sets the msg.topic to tweets/ and then appends the senders screen name. Sets msg.location to the tweeters location if known. Sets msg.tweet to the full tweet object.
2) Sentiment Node
Analyses the msg.payload and adds a msg.sentiment object that contains the resulting AFINN-111 sentiment score as msg.sentiment.score . 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. See the Sentiment docs here.
3) Function Node
A function block where you can write code to do more interesting things. The message is passed in as a JavaScript object called msg . By convention it will have a msg.payload 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 msg.topic . Likewise the QoS and retain values can be configured in the node or, if left blank, set by msg.qos and msg.retain respectively. By default, messages are published at QoS 0 with the retain flag set to false. If 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.
Step 3: Settings for ESP-Link
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.
Step 4: Representing the Sentiment Value on 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.