This page is part of the MOOC (Massive online open Course) for the use of “Node-Red” with MQTT to build a small own IoT network. Node-RED is a web-based editor for visual programming devices as part of Internet of Things with a flow editor written in JavaScript. More information can be found here: https://en.wikipedia.org/wiki/Node-RED

Starting Node-RED

After the installation of the Node-RED package on the Raspberry Pi described in “IoT Starter Kit ‘Raspberry Pi'”, the following command should be typed into the shell of the Raspberry Pi:

node-red-start

If you want to start the Node-RED server automatically after booting the Raspberry Pi, then type in the following command:

sudo systemctl enable nodered.service

Node-RED Editor

After starting Node-RED, a browser should be opened and the IP address or “localhost”, added by the port “:1880”, should be entered to access the editor.

Layout

In the central part of the screen you can find the flow editor with the programming components, also called nodes, sorted in categories on the left side and the debugging console on the right side.

Functionality

The most important aspect of the Node-RED nodes is the msg object that forwarded from one node to the next as JSON object. Within the msg object, a payload is saved in which commands, numbers or strings can be sent to the device.

The following example shows how you can set a string as payload within a “function node”.

msg = { payload:"Hello World!" }; return msg;

Note: Functions in Node-RED are written in JavaScript. A python extension exists but will not be described further here.

Sense HAT

The Sense HAT nodes enables the developer to read and send data to the Sense HAT of the Raspberry Pi with an input and output node, whereas the input node is used to get the data from the sensors and the output node is used to control the LED’s on the SenseHat.

Exercises

General information

In this example, the SenseHat outputs the string of the inject node to the LED’s. To start the injection, the button on the left of the inject node must be clicked.

The MQTT nodes in Node-RED are an easy way to connect a publisher or/and subscriber to an MQTT broker.

The first thing to decide is which node to pick (input or output). The input node gets data from the broker and the output node sends data to the broker. Like in the example below, it is important to fill the fields of the node correctly.

A topic can be seen as the channel on which the data is transferred. The output and input nodes must subscribe to the same topic to communicate with each other. But when a different payload is used, a new, different channel must be used. The screenshot below shows this concept visually.

Exercise 1: Temperature Warning

This example shows how to create a simple system that warns the user when the temperature reaches a predefined value. For this example, a temperature that will not harm the Raspberry Pi will be used to demonstrate the functionality.

First of all, a Sense HAT input node is needed which needs to be connected with a control node. The control node can be seen as an if condition which can be used to check if the temperature is within a selected range. To be sure that the if statement works correctly, the msg.payload.temperature property should be checked (and not only the msg.payload like in the default settings of the node).

In the next step, a MQTT output node should be connected with the control node. This is needed to send the data to the MQTT broker if the temperature is within a given range. To get data from the MQTT broker, a MQTT input node is needed. Afterwards, you can change the payload so that it shows a warning message with the use of a function node. As of now, the flow of this example can look like in the screenshot below.

Example for function node:

msg = { payload: "Warning! High temperature!" }; return msg;

At last, the function node is connected with an e-mail output node to be able to send an e-mail. These are possible settings of the e-mail node:

Exercise 2: Working with the MQTT Dash App

To get the temperature from the temperature sensor in the smartphone app MQTT Dash, a range display can be created in the app. Then, the Sense HAT input node should be connected with an MQTT output in Node-RED. A function node in which the msg.payload is changed to the output of the temperature value is also needed so that the range display in the MQTT Dash app is displayed correctly. Example for the function node:

msg = { payload:msg.payload.temperature }; return msg;

In the MQTT Dash app, the same topics should be used as within the input and output node in Node-RED so that the communication works.