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