What is Node-RED?

Node-RED is a powerful tool for building IoT applications and services. Its genesis was triggered exactly by this need to rapidly prototype IoT applications. An open source project led by the IBM Emerging Technologies group, in particular the two researchers, Nick O’Leary and Dave Conway-Jones, Node-RED was initially a tool for themselves as they were working on IoT projects and were “looking for a way to simplify the process of hooking together systems and sensors when building proof-of-concept technologies for customers.”

In this article we'll use both Ubidots MQTT nodes and Node-RED's native HTTP and MQTT libraries ("nodes") to read/send data to/from Ubidots.

Table of Contents

  1. Installing Ubidots' MQTT nodes in Node-RED
  2. Using Ubidots' MQTT nodes
  3. Using Node-RED's native HTTP and MQTT nodes

1. Installing Ubidots' MQTT nodes

Once Node-RED is running on your computer, head to the top-right menu and then click on the 'Manage pallets' option. A window, as depicted below, will appear. Go to the 'Install' tab, run a search for 'ubidots' an install the nodes called 'ubidots-nodered

Finally, close and re-open Node-RED, you should now see Ubidots' MQTT nodes in the input and output nodes waiting for you in the left-hand Node pane.

2. Using Ubidots' MQTT nodes

Here's a simple node arrangement that publishes and subscribes to a random "demo" variable in an Ubidots device called "node-red":

The example uses an inject node to publish the payload {"demo": 10} to Ubidots' MQTT node configured as shown below:
NOTE: If you do not know where to find your Ubidots TOKEN - please click here.

To setup the Ubidots' subscribe node:

To test this function, press the "Inject" button, and you will see the number "10" in the debug console. This number just went all the way to the Ubidots cloud, updated your account, and returned back to Node-RED:

3. Using Node-RED's native HTTP and MQTT nodes

Why use native libraries instead of Ubidots' library?

Through our experience and interaction with thousands of developers, Node-Red and Ubidots compliment each other's services well when: 

  • Grasp the concepts behind IoT protocols: Working with native HTTP and MQTT libraries will help you understand the concepts behind these protocols, enriching your IoT expertise. Using a library that wraps these protocols might save you lines of code, but will hide important concepts that might be useful for troubleshooting and scaling your project.
  • Connecting from any Node-RED device/server: Some Node-RED environments make it hard to install 3rd party libraries. For example, if you're running Node-RED from a server and you're not the admin and cannot install libraries. Using already built-in libraries can save you time, headache, and development costs. 

In the examples below we'll use an "Inject node" to simulate data, but of course in real-world applications, you need only replace this node with its actual source of data.

Node-RED + Ubidots using HTTP

Here's a simple node arrangement that sends an random "current" value, between 0 and 5, and a fixed "voltage" value to a Ubidots device called "node-red":

All the magic happens in the "function" node called "Setup Request", which prepares the request according to Ubidots API expected HTTP details:

Here's the clipboard version, so you can import it into your project - just copy and paste, if needed.

[{"id":"9a51d38b.c2c56","type":"inject","name":"","topic":"","payload":"","payloadType":"none","repeat":"5","crontab":"","once":true,"x":129,"y":120,"z":"b40cc76f.057c28","wires":[["ce12cba7.7e25e8"]]},{"id":"ce12cba7.7e25e8","type":"function","name":"Setup Request","func":"var msg;\ndevice_label = \"Node-RED\";\nubidots_token = \"A1E-xxxxxxxxxxxxxx\";\nmsg.method = \"POST\";\nmsg.url = \"http://things.ubidots.com/api/v1.6/devices/\" + device_label + \"/?token=\" + ubidots_token;\nmsg.payload = {\"current\":5*Math.round(Math.random() * 100) / 100,\"voltage\":\"110\"};\nreturn msg;","outputs":1,"valid":true,"x":294,"y":120,"z":"b40cc76f.057c28","wires":[["1f05de9a.b74411"]]},{"id":"cbae05ca.da6038","type":"debug","name":"","active":true,"console":"false","complete":"payload","x":648,"y":120,"z":"b40cc76f.057c28","wires":[]},{"id":"1f05de9a.b74411","type":"http request","name":"","method":"use","ret":"txt","url":"","x":472,"y":120,"z":"b40cc76f.057c28","wires":[["cbae05ca.da6038"]]}]

Node-RED + Ubidots using MQTT

Here's an example that publishes temperature data to Ubidots' MQTT broker and subscribes to the updated variable:

This example uses an inject node to publish the payload {"temperature":123} to an MQTT publish node configured as specified in Ubidots' MQTT documentation, that is:

Ubidots users:

industrial.api.ubidots.com

 port: 1883

For Educational users:

things.ubidots.com

 port: 1883 

Username: Your Ubidots token 

Password: Leave blank

Publish topic:

/v1.6/devices/{LABEL_DEVICE}

Subscribe topic:

/v1.6/devices/{LABEL_DEVICE}/{VARIABLE_LABEL}/lv
  • Note that "lv" stands for last value. This means, this topic only returns the last value of the variable, without the JSON formatting, making it easier to parse if needed.

Entering all of this in Node-RED would look like this:

In the MQTT publish node:

When clicking in the edit button of the Broker:

In the MQTT subscribe node:

Finally, here's the clipboard version so you can easily import this sample into your project:

[{"id":"ba386057.845d3","type":"mqtt-broker","broker":"things.ubidots.com","port":"1883","clientid":""},{"id":"b8b29c42.d9ecf","type":"inject","name":"","topic":"","payload":"{\"temperature\":123}","payloadType":"string","repeat":"","crontab":"","once":false,"x":285,"y":88,"z":"21dd2ded.2c3352","wires":[["b3747b5f.0fce28"]]},{"id":"b3747b5f.0fce28","type":"mqtt out","name":"","topic":"/v1.6/devices/node-red","qos":"","retain":"","broker":"ba386057.845d3","x":672,"y":89,"z":"21dd2ded.2c3352","wires":[]},{"id":"b8271085.e034c","type":"mqtt in","name":"","topic":"/v1.6/devices/node-red/temperature/lv","broker":"ba386057.845d3","x":315,"y":219,"z":"21dd2ded.2c3352","wires":[["e46bc941.41f718"]]},{"id":"e46bc941.41f718","type":"debug","name":"","active":true,"console":"false","complete":"false","x":689,"y":218,"z":"21dd2ded.2c3352","wires":[]}]

NOTE: you will need to configure the MQTT broker even after pasting the clipboard version above.

Still have questions? Want to suggest a new Node-RED capable device to our growing compatible device family? Feel free to drop us a line at support at ubidots.com.

Other users also found the following helpful...

Did this answer your question?