Overview
In order to use MQTT devices, the following is required:
- An MQTT broker installed
- MQTT Binding installed in openHAB
- MQTT Bridge Thing configured in openHAB
This tutorial was written with openHAB3 running on a headless Raspberry Pi 3B with Raspberry Pi OS, with SSH access.
Method
1. Install the MQTT broker
Install Mosquitto
On the Raspberry Pi, perform the following commands to install Mosquitto, and ensure it runs when re-starting the Pi:
sudo apt install mosquitto
sudo systemctl start mosquitto
sudo systemctl daemon-reload
sudo systemctl enable mosquitto
1a. Configure newer versions of Mosquitto
Default behaviour has changed in Mosquitto v2 and up - without editing the configuration file Mosquitto will only listen on the local host, and not allow anonymous connections.
In order to allow anonymous connections and allow any device within your network to communicate with Mosquitto, modify the mosquitto.conf configuration file as follows:
sudo nano /etc/mosquitto/mosquitto.conf
and add the following two lines to the end of the file:
listener 1883
allow_anonymous true
Save (Ctrl+x, then hit y and enter) and re-start Mosquitto.
sudo systemctl restart mosquitto
2. Install the MQTT binding in openHAB3
Settings → Bindings → Blue + → MQTT → Install
3. Connect openHAB3 to the MQTT broker
OpenHAB3 is connected to the MQTT broker through a Bridge Thing. To create a Bridge Thing:
- Settings → Things → Blue + → MQTT Binding → MQTT Broker
Configure the Bridge to suit.
- Unique ID: You can leave this default, or provide a more convenient ID. Once created, the ID cannot be changed.
- Label: Friendly name for your MQTT Bridge Thing
- Broker Hostname/IP: Enter the IP address of the device hosting the Mosquitto MQTT broker.
If you didn’t change any Mosquitto settings after install, you can leave the rest of the options as default.
Click Create Thing. Your Bridge Thing will be created and appear in your list of Things. After a couple of seconds, it will appear as ONLINE.
YAML Code
You can view the YAML code for you Bridge Thing by opening the Code tab within your Bridge Thing.
The code for this example is below:
UID: mqtt:broker:MosquittoMqttBroker
label: Mosquitto MQTT Broker
thingTypeUID: mqtt:broker
configuration:
lwtQos: 0
publickeypin: true
keepAlive: 60
clientid: 98397d65-4f22-41f9-b259-40e6ddde640f
qos: 0
reconnectTime: 60000
host: 192.168.1.92
secure: false
certificatepin: true
lwtRetain: true
enableDiscovery: true
location: Loft
Summary
You now have a working MQTT broker, and openHAB is connected to the MQTT broker via the Bridge Thing. You can now start to add individual devices as a Generic MQTT Thing. If you have a Tasmota device, this tutorial may be useful.