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