Skip to main content
All CollectionsIoT Projects Tutorials
Control a LED Remotely & Monitor Temperature values with a Raspberry Pi using Node-RED
Control a LED Remotely & Monitor Temperature values with a Raspberry Pi using Node-RED

Learn all you need to know about Node-RED to start with your IoT solution using Ubidots Cloud

Sergio M avatar
Written by Sergio M
Updated over 2 months ago

One tool that continues to make IoT and industrial solutions easier is Node-RED. Designed and built by IBM, Node-RED is an free, open source logic engine that allows programmers of any level to interconnect physical I/O, cloud-based systems, databases, and API’s. User interact with Node-RED via a browser-based flow editor that makes multi-device and multi-API integrations as easy as linking together nodes into easily understood flows.

To learn how you can better leverage the flexibility and visualization of projects with Node-RED continue reading.

In this article we will install and run Node-RED in a Raspberry Pi to quickly and easily develop and deploy your next IoT solution.

IMPORTANT NOTE: This guide assumes your Raspberry Pi has been configured and is already connected to the Internet and have Node-RED installed. If not configured yet, please reference to the following guides:

Requirements

  • Raspberry Pi already configured and connected to internet

  • 1-Wire Temperature Sensor - DS18B20

  • Led + 330 Ω Resistor

  • Male - Female jumper wires

  • Protoboard

Hardware Setup

Wiring up: 1-Wire Temperature Sensor

The 1-Wire temperature sensor has different versions for sale; one with a resistor integrated into the sensor and the other without. For this tutorial, we will work with both versions– with and without resistors. No matter which 1-Wire you choose for your system, make sure to double check any connections are properly based on the below diagrams and photos.

With resistor - with grove connector

Please follow the table below to make the right connections for your 1-Wire temperature sensor with resistor.

Without resistor integrated- without grove connector

The resistor in this setup is used as a pull-up for the data-line, and should be connected between the data wire and the power wire. This ensures that the data line is at a defined logic level, and limits interference from electrical noise should the pin be left floating. 

Use a 4.7kΩ (or 10kΩ) resistor and follow the diagram below to make the correct connections. Note that the pins connected in the Raspberry Pi are the same used in the table above: 

Wiring up: LED

Please follow the diagram below to make the right connections for the LED and resistor:

NOTE: The positive leg of the LED is usually longer, and it is this leg which should be inserted into the left side of the breadboard (e1 on the diagram).

Setting up Node-Red with Ubidots

Ubidots already built a Node-RED library including two new “nodes” in the pallete to handle data from/to Ubidots MQTT Broker. In order to install the Ubidots nodes, simply follow the steps below.

Starting Node-RED

1. Start Node-RED by running the command below in your Node-RED SSH Terminal: 

node-red-start

1.1. Once the command is inserted wait a couple seconds until the Node-RED server starts running. As you can see on the image below the terminal logs lets us know the device IP and port where that the Node-RED server will be pointing. 

The service is pointing to port 1880:  http://{your_pi_ip-address}:1880 

1.2. Once the Node-RED program has started, open a web-browser of your preference and in the URL bar paste the address shown in the first main line of your Node-RED terminal that reads “point a browser at.”

1.3. In my case, I access to the Node-RED web interface using http://10.0.0.29:1880. When you run the URL address in your browser you will be able to visualize the Noded-RED interface as shown below:

Installing the Ubidots Nodes

1. Select the Node-RED menu located in the upper-right side of the header. Then,to add the Ubidots nodes in the palette go to “Manage palette

2. In the “Manage palette” section, you will be able to manage and install the nodes needed. As shown below:

2.1. To install the Ubidots Nodes, go to the “Install” tab.

2.2. Then, search ubidots using the search bar. Select the install button of the one named “ubidots-nodered”:

2.3. Then, press the “install” button provided in the warning window, as shown below:

3. Once the installation starts you will begin receiving the following behavior in the Raspberry Pi’s terminal: 

Once the installation finishes properly, the following logs will be updated:

4. Now, you will be able to see the input and output for Ubidots nodes:

Setting up extra nodes

Now with the Ubidots nodes already in the palette, is time to install the node “node-red-contrib-sensor-ds18b20” which is in charge of the sensor readings provided by the sensor connected. 

NOTE: This node is compatible only with the Raspberry Pi Hardware and it only reads the temperature values from the sensor. 

Setup sensor configurations

1. The interface for 1-Wire must first be enabled. Insert the command below in the Raspberry Pi terminal to begin the 1-Wire enablement.

sudo raspi-config

1.1. Then, in the following windows go to Interfaces and then to 1-Wire and enable. 

1.2. To finish, reboot the Raspberry Pi by running the command below.

sudo su reboot  

2. Then, load all the kernel modules needed for working with 1-Wire devices. In the case of this demo, we will use the DS1820 1-Wire sensor. For this, run the commands below:

sudo modprobe w1-gpio
sudo modprobe w1-therm

Installing sensor node

  1. To install the node for the DS18B20 sensor, follow the same steps followed to install the Ubidots nodes, but now searching for “DS18B20”. Select the install button for the node called node-red-contrib-sensor-ds18b20:

 2. Once the node finishes its installation, go to the palette and check the Raspberry Pi section. If done properly, the node for the sensor will be properly included in the palette: 

Publishing data to Ubidots

If you are visual learner check out the video below to get done with the Integration:

  1. To PUBLISH data to Ubidots, click on Node-RED menu in the upper-right corner, then “Import” -> “Clipboard” and paste the code below:

Clipboard:

[{"id":"d4878a4.d875278","type":"ubidots_out","z":"a048c6a8.07ebb8","name":"RaspberryPiUbidots","token":"","label_device":"raspberry-pi","tier":"business","x":920,"y":120,"wires":[]},{"id":"963e0bcf.00a718","type":"sensor-ds18b20","z":"a048c6a8.07ebb8","name":" DS18B20 Sensor","topic":"","sensorid":"28-00000831ba40","timer":"5","repeat":false,"x":390,"y":120,"wires":[["126f50ec.4e76ff","949894af.040ba8"]]},{"id":"5941863d.c365c","type":"inject","z":"a048c6a8.07ebb8","name":"Reading Activation","topic":"","payload":"","payloadType":"date","repeat":"5","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":120,"wires":[["963e0bcf.00a718"]]},{"id":"3e01e56e.b96b3a","type":"debug","z":"a048c6a8.07ebb8","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":930,"y":200,"wires":[]},{"id":"126f50ec.4e76ff","type":"function","z":"a048c6a8.07ebb8","name":"Parse Data to JSON Object","func":"var payload = msg.payload;\n\n/* Saves the sensor readings */\nvar temp_value_c = payload; // temperature in celsius\nvar temp_value_f = (temp_value_c*1.8) + 32; // temperature in fahrenheit\n\nvar data = {}\n/* Builds the payload to be published */\ndata.payload = {\"temperature-c\": temp_value_c, \"temperature-f\": temp_value_f.toFixed(2)};\nreturn data;\n\n","outputs":1,"noerr":0,"x":660,"y":120,"wires":[["3e01e56e.b96b3a","d4878a4.d875278"]]},{"id":"949894af.040ba8","type":"debug","z":"a048c6a8.07ebb8","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":650,"y":200,"wires":[]}]

2. With the code imported, the following flow should appear:

3. Select the Ubidots node and configure it with your Ubidots account: 

3.1. Select the type of your account:

  • Ubidots : industrial.api.ubidots.com

3.2. Assign the name desired for the MQTT client. In this case, RaspberryPiUbidots is the default. 

3.3. Assign your Ubidots TOKEN

3.4. Assign the device label desired for the device. In this case, raspberry-pi is the default. 

4. Once the parameters of your Ubidots node are properly configured, you will have something similar to the following. To save the changes, press the Done button. 

5. Now it is time to deploy the flow and publish data to Ubidots cloud. For this, just press the Deploy button located at the upper right-hand side.

6. The behavior presented in the Ubidots node after deploying the flow will look as follows:

  1. Connecting…

  1. Published

7. [OPTIONAL] If your desire to debug the messages incoming from the nodes, you can activate the debug node by pressing the “tabs” icon located on the right-side of the node. As you can see below, the node located on the left side is deactivated meanwhile the one of the right side is activated. 

To check the debugging messages, select the debug tab from the Node-RED console: 

8. Go you the Device section of your Ubidots account and check the new device created:

9. If you desire to change the device name to a more friendly one and you don’t know how, please reference the following guide. You can also change the device description icon with this simple guide

Once the names and icons are updated to your liking, launch your application to end-users using Ubidots User Management.

Subscribing to a variable from Ubidots

If you are visual learner check out the video below to get done with the Integration:

  1. From the Node-RED menu in the upper right corner, select “Import” -> “Clipboard.” Then copy and paste the code provided below. 

Clipboard:

[{"id":"9e19ab92.655258","type":"ubidots_in","z":"1bc2b3f9.e25d3c","tier":"business","name":"RaspberryPiUbidots","token":"","label_device":"raspberry-pi","label_variable":"control","x":287,"y":290,"wires":[["aa56f39f.127be","65877a10.6f4fc4"]]},{"id":"aa56f39f.127be","type":"function","z":"1bc2b3f9.e25d3c","name":"Parse Ubidots Value","func":"var lastValue = msg.payload.value;\nmsg.payload = lastValue;\nreturn msg;","outputs":1,"noerr":0,"x":547,"y":290,"wires":[["c2ce4045.d20e5","7f3ca3e8.c7a3a4"]]},{"id":"c2ce4045.d20e5","type":"rpi-gpio out","z":"1bc2b3f9.e25d3c","name":"LED","pin":"11","set":"","level":"0","freq":"","out":"out","x":767,"y":290,"wires":[]},{"id":"65877a10.6f4fc4","type":"debug","z":"1bc2b3f9.e25d3c","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":547,"y":370,"wires":[]},{"id":"7f3ca3e8.c7a3a4","type":"debug","z":"1bc2b3f9.e25d3c","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":787,"y":370,"wires":[]},{"id":"a78c085c.6e3988","type":"inject","z":"1bc2b3f9.e25d3c","name":"","topic":"","payload":"{\"control\": 0}","payloadType":"json","repeat":"","crontab":"","once":true,"onceDelay":0.1,"x":267.5,"y":175.5,"wires":[["44bd18f.f127168"]]},{"id":"44bd18f.f127168","type":"ubidots_out","z":"1bc2b3f9.e25d3c","name":"RaspberryPiUbidots","token":"","label_device":"raspberry-pi","tier":"business","x":513.5,"y":176,"wires":[]}]


2. Assign your Ubidots TOKEN where is indicated in both Ubidots nodes.

3. Now it is time to deploy the flow to start controlling the LED from Ubidots Cloud. Press the Deploy button locate at the upper right-hand side of your Node-RED interface.

4. The behavior presented in the first Ubidots node is “Connecting...”  then “published”. At this point, a variable called "control" is created in the previously device created. This variable will be used to control your devices - in this case an LED. 

5. Now to be able to control your device you need to create a control widget. For this, go to the Dashboard section of your Ubidots account and create a new control widget. Press the plus (+) icon located at the upper right side, then press Control > Switch > control (variable previously created) > Finish:

Then, you should be able to interact with the switch widget to start controlling the LED: 

6. You will be able to see the status of the LED; if the value is equal to 1, this means the LED is ON, if the value is equal to 0, this means the LED is OFF:

7. [OPTIONAL] If your desire to debug the messages incoming from the nodes, you can activate the debug node by pressing the tab located at the right side of the node. As you can see below, the node located on the left side is deactivated meanwhile the one of the right side is activated. 

To check the debugging messages, select the debug tab from the Node-RED console. 

8. Now is time to test everything! Change the status of the switch from the Ubidots Dashboard and see how you are able to control the LED remotely:

Customize Visualization with Ubidots

Now that you are able to publish and subscribe data to/from Ubidots, is time to create your customize Ubidots Dashboards to visualize, interpret your data, and control assets remotely.

Did this answer your question?