Home Automation with Node-RED and Raspberry Pi: Control Lights & Read DHT11 Data

Home Automation with Node-RED and Raspberry Pi

Node-RED is a visual tool for non-programmers to work with the IoT, it can be used to build applications faster and reduce the “go-to-market” time for IoT products. It can also be used to easily interface hardware devices, APIs, and other online services together in new and interesting ways.


Node-RED can directly access data from microcontroller boards like Arduino, Raspberry Pi using the predefined port number or pin number. In this tutorial, we are going to build a Home Automation System that can control lights and can monitor temperature and humidity graphically on gauge created on the Dashboard. Throughout this tutorial, we will cover how to install and set-up Node-RED on Windows. Then we will also design a dashboard on Node-RED and use different input, output, and functions nodes to create a flow.


Components Required for Home Automation with Node-Red

  • Raspberry Pi
  • Relay Module
  • DHT11 Sensor
  • AC Bulb
  • LED
  • Breadboard
  • Jumper Wires


Installing Node-RED on Raspberry Pi

Node-RED comes preinstalled on the Raspbian Stretch OS. In case, you don’t have Node-RED installed on your Pi, you can use the command given below to install Node.js, npm, and Node-RED onto a Raspberry Pi. This command can also be used to upgrade an existing installation.

bash <(curl -sL https://raw.githubusercontent.com/node-red/linux-installers/master/deb/update-nodejs-and-nodered)

Installing Node-RED

The command will do the following things:

  1. Remove the old version of Node-RED and Node.js if present.
  2. Install the latest Node.js LTS version. If it detects Node.js is already installed, it will ensure that it is at least Node version 8.
  3. Clean out npm cache
  4. Install the latest version of Node-RED using npm
  5. Ask if you want to install a collection of useful Raspberry Pi nodes
  6. Will set up Node-RED to run as a service and provide a set of commands to work with the service


When the installation completes, you should see the Node-RED icon under the programming apps list.


Starting Node-RED on the Raspberry Pi

Node-RED can be either launched via Pi’s Desktop Interface or via Raspberry Pi terminal window.

From the Pi’s Desktop Interface: Select Menu -> Programming -> Node-RED

Remotely from your Computer’s Terminal: Run node-red-start in a new Raspberry Pi terminal window. To launch from the Pi’s Desktop Interface, click on Menu > Programming > Node-RED.

Starting Node-Red on Raspberry Pi


To launch it from the Raspberry Pi Terminal window, navigate to Terminal and enter the command given below:


Starting Node-Red on Raspberry Pi


Opening the Node-RED Interface

With Node-RED now installed and started up on Raspberry Pi, we can proceed to access its web interface. For that, navigate to the browser and write http://your_pi_ip-address:1880 in the address bar. Raspberry Pi IP address is shown in the first mainline of the Node-RED terminal as shown below.

Raspberry Pi IP Address


After this, you will be greeted with the following screen.

Node-RED Interface


Before we start building the flows, we need to install the palette for the Dashboard and DHT11 sensor in node-red. For that, click the menu icon in the top right corner, and then click on “Manage Palette”

Palette for Dashboard


Go to the Install tab, and then search and install ‘node-red-dashboard’ and 'node-red-contrib-dht-sensor’ nodes.

Node-RED Dashboard


Creating a Flow in Node-RED

With all the required nodes installed, we can proceed with creating a flow to control an LED and read DHT11 sensor data. The first step is to create a dashboard layout. In this project, we’ll have two switches to control an LED and bulb; two gauges to display temperature and humidity values. In the top right corner, click on the dropdown menu icon, and select the dashboard tab.

Node-RED Dashboard Layout


Then create a tab called ‘Home Automation’ and inside this tab, create two groups: Office and DHT as shown below.

Creating Flow for LED Control


Now, go to the Dashboard tab and drag the Switch node into the flow section.

Switch Node


Now, double-click on the Switch node to edit it. Rename the Switch node as LED. Add Name of Group “Home Automation” for grouping the node.

Home Automation Switch Node


Then go to the Raspberry Pi tab and drag the ‘rpi gpio out’ node into the flow section.

Raspberry Pi tab


Then double-click on the node, select the GPIO18 pin, and select ‘Digital Output’ as the output type.

Creating a Flow in Node-RED


Follow the same procedure for adding another switch.

Node-RED Switch Node


Now. in the next step, add nodes to read DHT11 sensor data. For that, go to the Common node palette and drag the inject node to the flow. Double click on the inject node and then set the interval to every 1 second.

Node-RED Inject Node


Then go to the Raspberry Pi tab and drag the rpi-dht22 node to the flow.

rpi-DHT22 Node


Double click on the rpi-dht22 node and then select the DHT11 as sensor module and 4 in a pin number.

Edit rpi-dht22 Node


Now, in the next step, go to the Function tab and drag two functions nodes to the flow as shown below.

Node-RED Function Node


Double click on the function, rename it to Temperature, and then add a line to get only temperature data. Do the same for the second function node to get humidity data.

Node-RED Function Node


Now, in the next step, go to the Dashboard tab and drag two gauge nodes to the flow as shown below.

Node RED Gauge Node


Double click on nodes to configure them. You can specify the following, as seen in the picture here:

Node RED Dashboard Configuration


The final flow will look like this:

Node-RED Home Automation Dashboard


With this done, we are now ready to deploy the project. Click the Deploy button in the top right corner.

Node RED Dashboard


Now, to see how the Node RED dashboard looks, go to http://your-pi-ip-address/ui.

Home Automation


Circuit Diagram for Node-RED Home Automation

The circuit diagram for Node-RED Home Automation is given below:

Node-Red Home Automation Circuit Diagram


The LED is connected to the GPIO18 pin while the input pin of the relay is connected to the GPIO24 pin of Raspberry Pi. The data pin of DHT 11 is connected to GPIO4.

Node-Red Home Automation


With this done, go to Node-RED Dashboard to control the LED and Bulb.

Home Automation with Node-RED

This is how you can turn on lights and monitor sensors on Raspberry Pi using Node-RED. You can follow these basic steps to build more advanced projects. 


Add new comment

The content of this field is kept private and will not be shown publicly.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.