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":"c53b887d.a27888","type":"inject","z":"98e9cf79.a372f8","name":"","topic":"","payload":"","payloadType":"none","repeat":"5","crontab":"","once":true,"x":110,"y":100,"wires":[["90633f9e.bc1a58"]]},{"id":"90633f9e.bc1a58","type":"function","z":"98e9cf79.a372f8","name":"Setup Request","func":"var msg;\ndevice_label = \"Node-RED\";\nubidots_token = \"BBFF-xxxxxxxxxxxxxx\";\nmsg.method = \"POST\";\nmsg.url = \"http://industrial.api.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,"noerr":0,"x":275,"y":100,"wires":[["4b95b329.d784c4"]]},{"id":"8cf48dc0.84df7","type":"debug","z":"98e9cf79.a372f8","name":"","active":true,"console":"false","complete":"payload","x":629,"y":100,"wires":[]},{"id":"4b95b329.d784c4","type":"http request","z":"98e9cf79.a372f8","name":"","method":"use","ret":"txt","url":"","x":453,"y":100,"wires":[["8cf48dc0.84df7"]]}]

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 Broker:


 port: 1883 

Username: Your Ubidots token 

Password: Leave blank

Publish topic:


Subscribe topic:

  • 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":"8765d540.6b6bc8","type":"inject","z":"eb630143.270018","name":"","topic":"","payload":"{\"temperature\":123}","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":"","x":190,"y":200,"wires":[["f3c857d3.8d10c"]]},{"id":"f3c857d3.8d10c","type":"mqtt out","z":"eb630143.270018","name":"","topic":"/v1.6/devices/node-red","qos":"","retain":"","broker":"694beaca.29a254","x":577,"y":201,"wires":[]},{"id":"cc230b36.52f4e8","type":"mqtt in","z":"eb630143.270018","name":"","topic":"/v1.6/devices/node-red/temperature/lv","qos":"2","datatype":"auto","broker":"694beaca.29a254","x":220,"y":331,"wires":[["3e83a146.589e16"]]},{"id":"3e83a146.589e16","type":"debug","z":"eb630143.270018","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":594,"y":330,"wires":[]},{"id":"694beaca.29a254","type":"mqtt-broker","z":"","name":"","broker":"industrial.api.ubidots.com","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

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?