Exploring IoT Applications with Node-RED
1. Overview
Here you have the opportunity to get to know the open-source software Node-RED to network some sensors and actuators. Node-RED runs in your dollhouse on a single-board computer named Raspberry Pi, but could also be operated on a PC or in the cloud.
After Node-RED is started on the Raspberry Pi, a web server on the single-board computer is accessible. The web server provides a graphical programming interface, which can be opened with a web browser.
Learning objectives
You will lern with the article Exploring IoT Applications with Node-RED …
- describe what Node-RED can be used for.
- create your own dashboards on your smartphone.
- use the MQTT protocol.
- describe some possibilities of IoT.
- explain the message flow in NodeRED.
2. Starting Node-RED
- Close the web browser on the Raspberry Pi if it is still open.
- On the Desktop of the Raspberry Pi, we have created an executable script file named start-mit-volume.sh for you. By double-clicking on it, commands are executed to start NodeRED on the Raspberry Pi and load some example flows. After double-clicking, please wait about 5 seconds, then the web browser with the loaded Node-RED frontend should appear.
For your information: You can't do much harm in this environment, clicking on loesche-volume.sh again will restore the original state. Be aware: this process will discard your changes.
3. Access to the NodeRED Webinterface with your computer
If you are in the same WLAN (SSID: IoTB) as the dollhouses, the NodeRED web interface can be accessed from your computer.
- Connect your Computer with the WLAN (SSID: IoTB). Ask your teacher for the password.
- Open a web-browser (e.g. Chrome Browser)
- The url looks like that, but change the
Xy
according to the label on the screen of the raspberry pi
http://koffer-Xy:1880
Example for koffer-1a:
4. Messages in NodeRED
4.1 Example 1: Lighting
In the first tab, I have prepared an example flow for you to turn on or off lighting in the dollhouse. Click alternately on the two buttons for a test. Report to the course instructor if the light does not turn on in the dollhouse:
Node-Red is based on the object-oriented programming language JavaScript. Therefore, the messages exchanged between the nodes are JavaScript objects. The message flow is from left to right.
The objects that are forwarded over the connections, for example, have the following properties:
var msg = {payload: "Some data", topic: "Topic of the message", _msgid: "99dbeb61.db6b28"};
The properties mean the following:
Properties of the msg object |
payload |
stores the payload of a message to be transmitted and can take any conceivable form and complexity |
topic |
stores the overarching topic of a message, so that filtering / classification is possible at the receiver |
_msgid |
contains an internal ID for identifying the message |
To see during development what messages are exchanged between the nodes, the green debug nodes can be used.
Assignment (numbers correspond to the screenshot below):
- Connect the left two nodes to the debug node.
- Make sure that logging is turned on (green button must be extended)
- Press “Übernahme (deploy)”
- Click on the bug in the sidebar on the right
- Trigger a message by pressing the upper or lower switch
- Observe the messages in the debug window, you can extend the view by clicking on the small arrow.
Here are some remarks on this exercise:
5. The MQTT Protocol
NodeRED supports many network protocols, such as UDP, TCP, HTTP, and MQTT. The MQTT protocol is very popular in IoT devices because it is lightweight and easy to use. For example, the MQTT protocol allows controlling actuators over the Internet or reading the values of remote sensors.
5.1 Publish / Subscribe
Unlike HTTP with its request/response method, MQTT implements a publish/subscribe architecture. In HTTP, a point-to-point connection with request and response is common. The transmission in MQTT works differently: devices that have something to report simply send their data to the broker (central server in the IoT system), that is, they publish their messages. The broker forwards these messages to other devices that have subscribed to these messages.
Devices that can be networked with MQTT include:
- Network-capable micro controller board (e.g. Wio Terminal or ESP32)
- PC (Windows)
- MAC (OS X)
- Raspberry Pi (Linux)
- Smartphone (Android or iOS)
- Generally: Network-capable, intelligent units in which the MQTT protocol is implemented
All devices connected to an MQTT broker are also referred to as MQTT clients. An MQTT network thus consists of several MQTT clients and one MQTT broker.
5.2 Example in the Local Network
It is not always necessary for the data to be accessible on the Internet. For example, if you only need sensor signals within your own home, networking in the local network is sufficient.
A local MQTT broker runs in the dollhouse. This is only accessible within the same network (in this case, the IoTB WLAN).
5.2.1 Investigating the Flow
- Click on the “MQTT Beispiel” (MQTT Example) tab
- Investigate with the help of the green debug nodes:
- What data is sent from the window of the dollhouse?
- Do the values of the solar cell power change when you cover them with your hand?
- What temperature does the sensor measure on the 1st floor of the dollhouse?
- Some blue nodes are used to create a user interface (dashboard). Open the dashboard as follows:
- Click the small arrow ∇ in the top right corner
- Click on Dashboard
- Click on the small arrow symbol in the top right corner
5.2.2 Heating Control Task
Now it's your turn! Get the temperature control running!
-
The flow MQTT Beispiel (MQTT Example) is not yet completely wired. Try to implement a two-point control for the temperature by correctly connecting the nodes. The following is still missing:
-
Test the controller by changing the desired temperature on the dashboard. You need some patience, the control loop is rather sluggish.
6. Window Monitoring
Barbie and Ken forgot to close the window on the 2nd floor last week and left for the weekend. A storm completely destroyed the room. Let's make sure this doesn't happen again! In the following example, we want to send an email as soon as the window remains open for a certain amount of time.
- Switch to the “Email Example” tab
- For testing purposes, we want to set the trigger time to 5 seconds (in practice, several hours would be more practical). Configure the trigger node accordingly.
- Configure the email node so that the warning email lands in your account.
- Finally, click on Übernahme (deploy)
- Test whether an email arrives if the window remains open longer than the set time.
- Try to figure out how you can change the content of the email.
7. Clever Boiler Control
7.1 Switching on at High Solar Power
The dollhouse is equipped with a photovoltaic system. The current power is measured and can be subscribed to in the local network under the topic meinHaus/Dach/Solarzelle/Leistung
.
Electric energy is notoriously difficult to store. This leads to many homeowners with photovoltaic systems having to sell their surplus electricity very cheaply to their grid operator. However, there may be devices in the house that can serve as energy storage:
- Electric car in the garage
- Electric boiler (for hot water)
- Freezers
Or devices that can be switched on flexibly in terms of time:
- Battery-operated devices
- Washing machine
- Tumbler
We want to turn on the electric boiler in the dollhouse when more than 70% power is available from the solar cells. Of course, in practice, the current consumption in the house would also have to be measured, but we simplify this in this school example.
Proceed as follows:
- Take a look at the boiler dashboard (Tab Keller)
- Manually switch the boiler heating on and off (click on the lightning bolt).
- Turn the shower on and off.
- Observe what happens.
- In Node-RED, go to the “Boiler Ladung” tab
- Drag an
mqtt in
node into the flow area and configure it:
- Under Server, select Local MQTT Broker.
- Subscribe to the topic
meinHaus/Dach/Solarzelle/Leistung
- Use a debug node to examine what comes from the solar cell.
- Drag a
switch
node into the flow area.
- Double-click on it to get to its configuration menu
- Forward the message from the solar cells to output 1 if the solar cell power exceeds 70%. Note that you must define the comparison value as a
number
(default is string
) and specify it without '%'.
- Forward the message from the solar cells to output 2 if the solar cell power falls below 65%. Note that you must define the comparison value as a
number
(default is string
) and specify it without '%'.
-
Drag two change
nodes into the flow area, connecting them to output 1 and output 2 of the switch
node, respectively. Change the payload of the current message to “on” or “off,” whichever makes sense to you.
-
Check with a debug node whether the logic works.
-
Now drag an mqtt-out
node into the flow area.
-
Send “on” or “off” via MQTT to the boiler with the topic meinHaus/UG/Boiler/Heizung
. Choose the local MQTT Broker for this.
-
Test with a flashlight and by covering the solar cell to see if anything happens at the boiler.
-
At the end, your flow should look like that:
8. Work together with an app inventor group
So far, we have only exchanged data between different devices within our local network (our own WLAN ). Now we want to make it possible to access the dollhouse from anywhere in the world. Now work together with the group that has been working with the MIT App Inventor. Try together to control the light in the doll's house with the smartphone app.
8.1 Overview
With a smartphone, you should be able to switch the light (or another actuator) on or off in the dollhouse. This endeavor could be simplified via MQTT as follows:
8.2 Implementation on the Smartphone (MQTT Client 1)
Your partners have been working on a smartphone app that you can use to control your light in the doll's house. Talk to them and find out how the app works.
8.3 Implementation on the Raspberry Pi (MQTT Client 2)
- Select Tab "Beleuchtungs- Beispiel"
- In this flow, you will find a purple 'mqtt in' node. It is used to subscribe to any topic at a broker. This node has been pre-configured by the instructor. Therefore, a secure connection to an MQTT broker on the internet is already established. For authentication with this broker, a username and password are already set. You are now connected to the same broker as the smartphone of your partners.
- Double-clicking on this node will display the following settings. Change the number here (unless you have position 3), according to the label visible on the upper left side of the dollhouse wagon.
-
Connect the output of the purple node to the input of the lighting in the 1st floor.
-
Also, draw a connection to a debug node to check later if data is coming from the MQTT Broker.
8.4 Testing and Reflecting!
Now, the remote control with the smartphone should work. Discuss with your neighbor:
-
What path do the data take from the smartphone to the dollhouse?
-
Do the data stay within the same room?
-
Are the data transmitted securely?
-
Who else has control over your lighting?
Tip: You can locate the MQTT Broker by entering the broker's URL 'schnupperkurs-mqtt-broker.s2.eu.hivemq.cloud' here:
https://www.handy-sofort-orten.de/ip-adresse-orten/ip-adresse-und-standort-von-webservern-zeigen/
8.5 Develop the app further
Publish the current temperature via MQTT so that you can retrieve the data later using your smartphone. Work together and find out how you can do this.
9. Work together with a IoT Cube group
Now work together with a that has been working with the IoT Cube. Try together to receive data from the IoT Cube. Here you can find some help to do that.
Now you should be able to receive any data from the IoT Cube in the doll's house.