We’re going to create a small Node-RED project that listens to Twitter for a specific term or set of keywords that you set and posts them to an OpenSensors Topic. Node-RED is an easy way of streamlining your projects. This brief ten minute project is the first of two showing just how simple it is to get info in and out of OpenSensors.
You will need to create or own a Twitter account and an OpenSensors account before continuing.
Installing Node-REDFirst things first, we’ll need to install Node-RED, if you haven’t already. You can install from here or run Node-RED in a Docker container (for advanced users).
Create Node-RED flowBoot up Node-RED, open up a browser and go to: http://localhost:1881/
Take a moment to familiarize yourself with the interface, on the left there is a list of ‘nodes’, elements that interact with each other, input, output, function, storage, analysis and so on. To deploy a node to the workspace simply drag and drop the node. Let’s do this now.
Add Twitter Input NodeIdentify the Twitter input node and drop it into the workspace. Double click on the Twitter input node and enter your details and authenticate with Twitter. Enter what you’d like to search for on Twitter to return to an MQTT feed.
That was easy. Once deployed, this node will listen to Twitter and consume any tweets that conform to your search criteria, for example, I want to consume any tweets containing #opensensorsnodered (above). We want to post the tweet to OpenSensors. So let’s now drag and drop an MQTT output node into the workspace alongside the Twitter input node.
Create OpenSensors Device and TopicBefore continuing, open up OpenSensors’ website and create a new Device and Topic, take note of your Device Client ID and Password and Topic name.
Edit MQTT NodeLet’s go back to Node-RED and edit the MQTT node to work with OpenSensors.io. Double click the node. You will see a number of fields, we want to edit the broker field. Click on the ‘edit pencil’ beside the broker field. Fill out as following:
- Broker:
mqtt.opensensors.io
- Port:
1883
- Client ID: this is the client ID of your OpenSensors device
- Username: this is your OpenSensors username
- Password: this is your OpenSensors device password
When finished click Update and complete the editing of this node by entering your Topic path in the Topic field, for example: /users/jamesmoulding/TwitterNodeRed
Leave the QoS and Retain fields blank for this project.
On the nodes panel on the left-hand side, find the Debug Node under Outputs. Drag and drop this node to the workspace above the MQTT node. This allows us to read the Twitter message payload in the debug panel on the right, below the ‘Deploy’ button.
Link the Nodes!We’re almost there. Let’s link up the nodes and make them functional by clicking and holding on the small circles at the end of the nodes. Create a link between the Twitter input and MQTT output node. Also create a link from the Twitter input to the Debug node. Your Node-RED flow should look like:
You can now deploy the flow! Click the big red deploy button in the top right.
Open up the OpenSensors Topic you created. Post a tweet to the hashtag or search time you created and watch as it is delivered to the Topic. You should see the same message on the Node-RED debugger.
This service will continue to run until you close the instance of Node-RED. The messages sent to OpenSensors will persist indefinitely and are easily accessible via our API. To save the flow, navigate to the top right menu and choose from the export options.
With this simple flow, saving you plenty of time coding, you can archive your favourite tweets and with a little extra functionality through Node-RED, create alarms and notifications based on events in the payload of your Twitter messages. The possibilities are endless.
In the next Node-RED tutorial, we’ll be showing how you can listen to an MQTT feed from OSIO, and if a message arrives, confirm receipt via Twitter.







Comments