Exploring IoT Applications with Node-RED (by Fabian Reifler)

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):

  1. Connect the left two nodes to the debug node.
  2. Make sure that logging is turned on (green button must be extended)
  3. Press “Übernahme (deploy)”
  4. Click on the bug in the sidebar on the right
  5. Trigger a message by pressing the upper or lower switch
  6. 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:

  • Take a look at the configuration of all nodes by double-clicking:

    • The two blue inject- nodes on the left generate a message when pressed. The message content is 0 for the upper button and 1 for the lower button.
    • The light blue GPIO- node on the right establishes a connection to the digital inputs and outputs (GPIOs) of the Raspberry Pi. A relay is connected to pin 40, which turns on the lighting in the dollhouse.

4.2 Example 2: Objects

Now click on the “Objekte” tab.

Complete and examine the flow in the following steps:

  1. Connect the two inject nodes to the debug node.
  2. Click on Übernahme (deploy)
  3. Click on the button of the inject node on the left to trigger a message.
  4. Observe what happens in the debug window. You can expand the message with the small arrow:
  5. Take a look at how the nodes are configured by double-clicking on them.
  6. Drag a new inject node into the flow area (found under “General” on the left). Double-click to access the configuration.
  7. Next to msg.payload, enter your name. You should set the data type to string. Next to msg.topic, enter Name. Pay attention to capitalization.
  8. Connect the inject node to the existing function node.
  9. Click on Übernahme (deploy)
  10. Observe again what happens in the debug window when you press the button of the new inject node. If you did it right, the following should be displayed: "Gut gemacht! Dein Name ist also ... !", “Well done! So your name is … !”
  11. Take a look with a double-click on the “function” node to see what is being done there. In Node-RED, it is thus possible to intervene at any point with your own Java Script functions, for example, to change messages.

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:

    • The actual temperature is not yet evaluated by the controller

    • The controller is not yet sending control commands to the heating in the dollhouse.

  • 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:

  1. Take a look at the boiler dashboard (Tab Keller)
    1. Manually switch the boiler heating on and off (click on the lightning bolt).
    2. Turn the shower on and off.
    3. Observe what happens.
  2. In Node-RED, go to the “Boiler Ladung” tab
  3. Drag an mqtt in node into the flow area and configure it:
    1. Under Server, select Local MQTT Broker.
    2. Subscribe to the topic meinHaus/Dach/Solarzelle/Leistung

       
  4. Use a debug node to examine what comes from the solar cell.
  5. Drag a switch node into the flow area.
    1. Double-click on it to get to its configuration menu
    2. 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 '%'.
    3. 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 '%'.
  6. 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.

  7. Check with a debug node whether the logic works.

  8. Now drag an mqtt-out node into the flow area.

  9. Send “on” or “off” via MQTT to the boiler with the topic meinHaus/UG/Boiler/Heizung. Choose the local MQTT Broker for this.

  10. Test with a flashlight and by covering the solar cell to see if anything happens at the boiler.

  11. 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.

  • In Node-RED, take an mqtt in node
  • Configure the server like that:
  • Change the topic like that, the bold part eui-20XX-c-XX depends on the label of the cube:

    ttn/v3/app-iot-wuerfel-klassensatz-c@ttn/devices/eui-2023-c-01/up

  • The debug node can be used to display data as soon as the cube has sent something.
  • A lot of data is received that we are usually not interested in. The relevant Information is contained in the decoded payload:
  • The change- node helps you, to extract the relevant information:


 

Now you should be able to receive any data from the IoT Cube in the doll's house.

  • Het arrangement Exploring IoT Applications with Node-RED (by Fabian Reifler) is gemaakt met Wikiwijs van Kennisnet. Wikiwijs is hét onderwijsplatform waar je leermiddelen zoekt, maakt en deelt.

    Auteur
    Vetskilling
    Laatst gewijzigd
    2024-11-04 14:18:02
    Licentie

    Dit lesmateriaal is gepubliceerd onder de Creative Commons Naamsvermelding 4.0 Internationale licentie. Dit houdt in dat je onder de voorwaarde van naamsvermelding vrij bent om:

    • het werk te delen - te kopiëren, te verspreiden en door te geven via elk medium of bestandsformaat
    • het werk te bewerken - te remixen, te veranderen en afgeleide werken te maken
    • voor alle doeleinden, inclusief commerciële doeleinden.

    Meer informatie over de CC Naamsvermelding 4.0 Internationale licentie.

    Aanvullende informatie over dit lesmateriaal

    Van dit lesmateriaal is de volgende aanvullende informatie beschikbaar:

    Toelichting
    In a half-day session, students experiment with a smart home model (dollhouse). The edge devices are pre-programmed and provided to ensure a smooth introduction.
    Eindgebruiker
    leerling/student
    Moeilijkheidsgraad
    gemiddeld
  • Downloaden

    Het volledige arrangement is in de onderstaande formaten te downloaden.

    Metadata

    LTI

    Leeromgevingen die gebruik maken van LTI kunnen Wikiwijs arrangementen en toetsen afspelen en resultaten terugkoppelen. Hiervoor moet de leeromgeving wel bij Wikiwijs aangemeld zijn. Wil je gebruik maken van de LTI koppeling? Meld je aan via info@wikiwijs.nl met het verzoek om een LTI koppeling aan te gaan.

    Maak je al gebruik van LTI? Gebruik dan de onderstaande Launch URL’s.

    Arrangement

    IMSCC package

    Wil je de Launch URL’s niet los kopiëren, maar in één keer downloaden? Download dan de IMSCC package.

    Meer informatie voor ontwikkelaars

    Wikiwijs lesmateriaal kan worden gebruikt in een externe leeromgeving. Er kunnen koppelingen worden gemaakt en het lesmateriaal kan op verschillende manieren worden geëxporteerd. Meer informatie hierover kun je vinden op onze Developers Wiki.