How to Use Node-RED with Raspberry Pi and NodeMCU

raspberry pi nodemcu node red

In this tutorial, we will learn how to use Node-RED with Raspberry Pi and NodeMCU by building a prototype home automation hub with an LED and a temperature and humidity sensor.

Node-RED

Node-RED is an open-source programming tool that allows visual coding using nodes. Nodes are code blocks that perform a specific function such as input, output, or processing. Furthermore, these nodes can be wired together to do more advanced stuff like visualizing data or receiving data with different protocols like MQTT. In Node-RED, these connected nodes are called flows.

Installing Node-RED

The official Node-RED website provides a short script to install Node-RED and all its dependencies namely Node.js and npm. You can copy that code below into your terminal.

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

After entering the command, you should see a screen like this:

Figure 1: Installing Node-RED

After the installation, a prompt will show:

Figure 2: Installation Done

Note that Node-RED is included in the Raspberry Pi repository so you can also install it using sudo apt-get install nodered. The difference is with the dependencies. Doing apt install won’t include npm to the installation so it’s better to use the script provided by the Node-RED website.

Running Node-RED

After installing, Node-RED will be available in your Raspberry Pi menu.

Figure 3: Node-RED inside the Pi Menu

Alternatively, you can start the service in the terminal with:

node-red

Press CTRL+ALT+C or close the terminal to stop Node-RED.

Due to the limited memory of the Raspberry Pi, you will need to start Node-RED with an additional argument to tell the underlying Node.js process to free up unused memory sooner than it would otherwise.

To do this, you should use the alternative node-red-pi command and pass in the max-old-space-size argument.

node-red-pi --max-old-space-size=256

Running as a service

If you intend to make the Raspberry Pi an autonomous home automation hub, meaning Node-RED runs in the background automatically on boot, we need to set it up as a service.

The following commands are provided to work with the service:

  • node-red-start – this starts the Node-RED service and displays its log output. Pressing Ctrl-C or closing the window does not stop the service; it keeps running in the background
  • node-red-stop – this stops the Node-RED service
  • node-red-restart – this stops and restarts the Node-RED service
  • node-red-log – this displays the log output of the service

To enable Node-RED to automatically start on boot. Enter the following into your terminal.

sudo systemctl enable nodered.service

To disable it, simply enter :

sudo systemctl disable nodered.service

Opening the editor

You can access the Node-RED editor with any device connected to your network using a web browser. The address of the editor is http://localhost:1880. Basically, your Raspberry Pi’s address plus the port number 1880. To know your Pi’s IP address enter hostname -I or ifconfig into the terminal. The address is somewhere inside the WLAN section.

Installing Node-RED Dashboard

Node-RED Dashboard is a module that provides a set of nodes in Node-RED to quickly create a live data dashboard. This will serve as the GUI of your home automation hub.

To install the Node-RED Dashboard, enter:

node-red-stop
cd ~/.node-red
npm install node-red-dashboard

Then, restart your Pi.

sudo reboot

To open the dashboard, type your Pi’s address in a web browser followed by :1880/ui as shown below:

http://Your_RPi_IP_address:1880/ui

The dashboard is still empty because we haven’t worked with the editor yet.

MQTT

MQTT stands for MQ Telemetry Transport and it is a nice lightweight publish and subscribe system where you can publish and receive messages as a client. It is a simple messaging protocol, designed for constrained devices and with low-bandwidth. So, it’s the perfect solution for Internet of Things applications.

Installing Mosquitto Broker

In MQTT, the broker is what’s responsible for receiving and filtering messages, deciding who is interested in it and then publishing the message to all subscribed clients.

There are several brokers you can use. In this tutorial, we’re going to use Mosquitto Broker.

First, enter these commands into the terminal:

sudo apt update
sudo apt install -y mosquitto mosquitto-clients

Press Y to proceed with the installation then wait for it to finish. Enter the command below to autostart Mosquitto on boot. Lastly, restart the Pi.

sudo systemctl enable mosquitto.service

Run the following command to test if the program is successfully installed.

mosquitto -v

This returns the version of Mosquitto you have in your system. It also tries to reinitialize the program so if it is already running, it will return an error message. If you see it, don’t worry as the program is already installed properly.

Getting Started With Your Home Automation Hub

Our goal is to have a Raspberry Pi control an ESP8266 NodeMCU using Node-RED. The ESP8266 will be connected to an LED and a DHT Temperature and Humidity Sensor. With both input and output components, we will have an ample demonstration of how Node-RED works with the Raspberry Pi.

Preparing the Hardware

What you need:

  • Raspberry Pi
  • Raspberry Pi power supply
  • NodeMCU
  • LED
  • DHT sensor
  • Breadboard
  • Jumper Wires

Connect the components as shown below:

Figure 4: Hardware Connections

Creating the Layout

Open Node-RED editor. On the top right corner, open the dashboard tab and select layout. Create a tab called Home and inside the tab, create two groups:LED and DHT Sensor as shown in the figure below:

Figure 5: Layout

Making the Flow

Drag the following nodes to the workspace. You can find all available nodes in the left side of your editor.

Figure 6: Adding Nodes

The switch node will determine the message Node-RED will send. Then, the MQTT output node will publish the message according to the switch’s state, turning the LED on or off.

On the other hand, the two MQTT input nodes are subscribed to the temperature and humidity topics. These nodes will fetch the data and send it to the gauge node to display the values.

Connecting Node-RED to Mosquitto

To establish the wireless connection between the Raspberry Pi and NodeMCU, we need to connect Node-RED and the MQTT broker, Mosquitto. Double click the MQTT output node:

Click the Add new mqtt-broker option.

Figure 7: MQTT Node Properties

Type localhost in the server field

Figure 8: MQTT Broker Properties

This will add the broker to the other nodes as well.

Configuring the Nodes

Everything is pretty straightforward in this section. Configuring the nodes means changing labels, units, topics, and etc. Just copy the changes in the following images to your editor.

  • Switch
Figure 9: Switch Properties
  • MQTT Output Node
Figure 10: MQTT Output Properties
  • MQTT Input Node (Temperature)
Figure 11: MQTT Output Temperature Properties
  • Gauge (Temperature)
Figure 12: Gauge Temperature Properties
  • MQTT Input Node (Humidity)
Figure 13: MQTT Input Humidity Properties
  • Gauge (Humidity)
Figure 14: Gauge Humidity Properties

Drag the end of the first node to the next to connect them.

Figure 15: Dashboard

Now, to apply the changes and display it into your dashboard, Click the Deploy button on the top right corner. Visit the Node-RED dashboard to view the actual switch and gauges. It is still blank since the NodeMCU isn’t setup yet.

Preparing your Arduino IDE

In this tutorial, we will use the Arduino IDE to program the NodeMCU. First, install the required libraries: PubSubClient by Nick O’Leary and DHT Sensor by Adafruit.

The PubSubClient library provides the client with easy publish/subscribe messaging with a server that supports MQTT. On the other hand, The DHT sensor library allows the user to interface any DHT sensor to read temperature and humidity with an Arduino compatible board.

To install libraries in the IDE, open the Library Manager under Tools. You can also use the shortcut CTRL+SHIFT+I. Search for the name of the library in the manager and click install.

After installing the libraries, restart the program. Then, go to Tools. Inside boards, select “NodeMCU 1.0 (ESP-12E Module)”. After that, choose the port number of your device in Tools > Port > COM7 (in my case).

Code

Copy the code below to your Arduino IDE.

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include "DHT.h"

#define DHTTYPE DHT11   

const char* ssid = "HUAWEI-6HxE";
const char* password = "K4H9khUh";
const char* mqtt_server = "192.168.100.23";

WiFiClient espClient;
PubSubClient client(espClient);

const int DHTPin = D1;
const int lamp = D2;

DHT dht(DHTPin, DHTTYPE);

long now = millis();
long lastMeasure = 0;

void callback(String topic, byte* message, unsigned int length) {
  Serial.print("Message arrived on topic: ");
  Serial.print(topic);
  Serial.print(". Message: ");
  String messageTemp;
  
  for (int i = 0; i < length; i++) {
    Serial.print((char)message[i]);
    messageTemp += (char)message[i];
  }
  Serial.println();

  if(topic=="home/led"){
      Serial.print("Changing Room lamp to ");
      if(messageTemp == "on"){
        digitalWrite(lamp, HIGH);
        Serial.print("On");
      }
      else if(messageTemp == "off"){
        digitalWrite(lamp, LOW);
        Serial.print("Off");
      }
  }
  Serial.println();
}

void setup() {
  pinMode(lamp, OUTPUT);
  dht.begin();
  Serial.begin(115200);
  
  delay(10);

  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  
  Serial.println("");
  Serial.print("WiFi connected - ESP IP address: ");
  Serial.println(WiFi.localIP());
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);

}

void loop() {

  if (!client.connected()) {
    while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");

    if (client.connect("ESP8266Client")) {
      Serial.println("connected");  
      client.subscribe("home/led");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      delay(5000);
    }
   }
 }
  if(!client.loop())
    client.connect("ESP8266Client");

  now = millis();
  if (now - lastMeasure > 3000) {
    lastMeasure = now;
    float h = dht.readHumidity();
    float t = dht.readTemperature();

    if (isnan(h) || isnan(t)) {
      Serial.println("Failed to read from DHT sensor!");
      return;
    }
    
    static char temperatureTemp[7];
    dtostrf(t, 6, 2, temperatureTemp);
    static char humidityTemp[7];
    dtostrf(h, 6, 2, humidityTemp);

    client.publish("home/temperature", temperatureTemp);
    client.publish("home/humidity", humidityTemp);
    
    Serial.print("Humidity: ");
    Serial.println(h);
    Serial.print(" %\t Temperature: ");
    Serial.println(t);
  }
} 

This code will read the temperature and humidity reading from the DHT sensor and publish it into the home/temperature and home/humidity topics via MQTT. Additionally, this code will subscribe your NodeMCU to the room/led topic, receiving messages published by the switch flow inside the Node-RED dashboard.

Be sure to update your Wifi name, password, and Raspberry Pi address. I won’t change mine so you can see where to put them. Lastly, you can change the indicated DHT sensor if you are using a different kind.