MQTT on the Raspberry Pi – Mosquitto Broker & MQTT Explorer Setup
As soon as you start letting several smart home devices, sensors or microcontrollers talk to each other, you will sooner or later run into MQTT. The protocol is the de facto standard in the IoT world – lightweight, reliable and a perfect fit for the Raspberry Pi as the central hub. In this article I explain what MQTT actually is, how to set up the Mosquitto broker on the Raspberry Pi and how to watch and debug your messages comfortably with MQTT Explorer.
What Is MQTT? The Publish/Subscribe Principle Explained Simply
MQTT stands for Message Queuing Telemetry Transport – sounds clunky, but it is remarkably simple. The protocol works on the publish/subscribe model:
- Publisher sends a message to a specific topic (e.g.
home/livingroom/temperature) - Broker receives the message and distributes it
- Subscriber has subscribed to the topic and receives the message immediately
The key difference from classic HTTP requests: publisher and subscriber do not know each other. Both only talk to the broker – the central middleman. That makes the system extremely flexible: your ESP32 sends temperature readings, Home Assistant receives them, Node-RED processes them further – all at the same time, without the three systems having to be connected to each other directly.

Topics: Addressing in MQTT
Topics are hierarchical, similar to file paths:
home/livingroom/temperature
home/kitchen/motion
garden/irrigation/status
You can also use wildcards: # subscribes to all sub-topics, + to exactly one level. That makes debugging and monitoring very convenient.
QoS – How Reliable Should the Delivery Be?
| QoS level | Meaning | Typical use |
|---|---|---|
| 0 – At most once | Message is sent once, no acknowledgement | Temperature sensor (loss is tolerable) |
| 1 – At least once | Delivery guaranteed, duplicates possible | Switch states, alarms |
| 2 – Exactly once | Exactly once, no duplicates | Critical control commands |
For most smart home projects QoS 1 is perfectly sufficient.
Why MQTT? Sensible Use Cases
MQTT makes sense wherever many small devices need to exchange data with little overhead:
- Home Assistant: MQTT is the preferred integration method for your own sensors, Zigbee devices via Zigbee2MQTT, Shelly devices and DIY hardware
- Node-RED: flows can receive, process and forward MQTT messages – ideal for automations
- ESP32 / ESP8266: microcontrollers with the PubSubClient library send sensor data (temperature, humidity, motion) straight to the broker
- Zigbee2MQTT: converts Zigbee radio traffic into MQTT messages – that is how IKEA, Aqara and friends talk to your system
- Monitoring: collect server metrics, power consumption data or camera events in one place
The Raspberry Pi is the ideal broker host: low power, always online, and with Mosquitto the broker runs reliably even on a Pi Zero.
Install the Mosquitto Broker on the Raspberry Pi
Mosquitto is the most widely used open-source MQTT broker and ships directly in the Raspberry Pi OS package repositories.
Tip for Home Assistant users: if you already run Home Assistant on the Raspberry Pi, you can install Mosquitto directly as an add-on – no command line needed. In HA go to Settings → Add-ons → Add-on Store and search for “Mosquitto broker”. The add-on configures the broker automatically and integrates seamlessly with Home Assistant’s MQTT integration. For everyone else – or if you want a standalone broker for several systems – the native installation is the better choice.
Installation
sudo apt update
sudo apt install mosquitto mosquitto-clients -y
This also installs the command-line tools mosquitto_pub and mosquitto_sub for testing.
sudo systemctl enable mosquitto
sudo systemctl start mosquitto
Check the status:
sudo systemctl status mosquitto
If you see active (running), the broker is already listening on port 1883.
Set Up Authentication (Recommended)
By default Mosquitto accepts anonymous connections – you will want to lock that down, even on your home network. First create a user:
sudo mosquitto_passwd -c /etc/mosquitto/passwd mqttuser
You will be asked for a password. Then open the configuration file:
sudo nano /etc/mosquitto/conf.d/default.conf
And add the following:
listener 1883
allow_anonymous false
password_file /etc/mosquitto/passwd
Save with Ctrl+O, then restart Mosquitto:
sudo systemctl restart mosquitto
From now on only connections with a username and password are accepted.
First Test with mosquitto_pub and mosquitto_sub
Open two terminal windows on the Raspberry Pi (or connect twice via SSH).
Terminal 1 – subscriber (waits for messages):
mosquitto_sub -h localhost -t "test/sensor" -u mqttuser -P yourPassword
Terminal 2 – publisher (sends a message):
mosquitto_pub -h localhost -t "test/sensor" -m "Temperature: 22.5°C" -u mqttuser -P yourPassword
Temperature: 22.5°C should appear in the first terminal right away. The broker works.
Install MQTT Explorer and Connect
The command line is fine for testing, but for everyday use a graphical tool is far more comfortable. MQTT Explorer is a free desktop client for Windows, macOS and Linux that shows all topics in a clear tree view.
Download and Installation
Download MQTT Explorer from mqtt-explorer.com and install it on your computer (not on the Pi). There is an AppImage for Linux and classic installers for Windows and macOS.
Connect to the Raspberry Pi
On first launch you will see a connection dialog. Enter the following values:
| Field | Value |
|---|---|
| Host | IP address of your Raspberry Pi (e.g. 192.168.1.100) |
| Port | 1883 |
| Username | mqttuser |
| Password | the password you chose |

After clicking Connect you will see all active topics in a tree view. You can subscribe to topics, watch messages and also publish messages to any topic yourself – ideal for debugging your smart home integrations.

Practical Example: ESP32 Temperature Sensor with MQTT and Home Assistant
Now let’s connect theory and practice: an ESP32 with an attached DHT22 sensor sends temperature and humidity to the Raspberry Pi via MQTT every 30 seconds. Home Assistant displays the values.
ESP32 Sketch (Simplified)
Using the PubSubClient library for Arduino:
#include <WiFi.h>
#include <PubSubClient.h>
#include <DHT.h>
const char* mqttServer = "192.168.1.100"; // IP of your Pi
const int mqttPort = 1883;
const char* mqttUser = "mqttuser";
const char* mqttPassword = "yourPassword";
DHT dht(4, DHT22); // GPIO 4
WiFiClient espClient;
PubSubClient client(espClient);
void loop() {
float temp = dht.readTemperature();
float hum = dht.readHumidity();
client.publish("home/sensor1/temperature", String(temp).c_str());
client.publish("home/sensor1/humidity", String(hum).c_str());
delay(30000);
}
Home Assistant Integration
In Home Assistant you add your broker via Settings → Devices & Services → MQTT. After that you can define a sensor in configuration.yaml:
mqtt:
sensor:
- name: "Living Room Temperature"
state_topic: "home/sensor1/temperature"
unit_of_measurement: "°C"
device_class: temperature
The sensor shows up on the dashboard immediately and updates every 30 seconds. With Node-RED you can process the data further – for example send a notification when the temperature exceeds a threshold.
FAQ about MQTT on the Raspberry Pi
Which port does MQTT use?
Mosquitto listens on port 1883 by default (unencrypted). Encrypted connections via TLS/SSL use port 8883. For most home network setups port 1883 is sufficient.
Can I run Mosquitto as a Docker container?
Yes, the official eclipse-mosquitto Docker image runs without problems on the Raspberry Pi. For a simple installation we recommend the native apt package though – less overhead, direct systemd support.
Is MQTT secure enough for the home network?
With password authentication enabled, Mosquitto is adequately secured for the home network. If you want to reach MQTT over the internet, you should definitely enable TLS and run MQTT behind a reverse proxy (e.g. Nginx or a Cloudflare Tunnel).
What is the difference between MQTT and HTTP?
HTTP is request/response – one side asks, the other answers. MQTT is publish/subscribe – messages are distributed without a direct exchange between sender and receiver. MQTT is far more lightweight and better suited to permanently connected IoT devices.
How many devices can Mosquitto handle at once?
Mosquitto on a Raspberry Pi 4 or 5 can easily handle hundreds of concurrent connections – more than enough for a typical smart home setup.
Does MQTT work with Zigbee devices too?
Yes – Zigbee2MQTT translates Zigbee radio signals (IKEA Tradfri, Aqara, Sonoff Zigbee) into MQTT messages. You need a Zigbee coordinator (e.g. Sonoff Zigbee 3.0 USB dongle) attached to the Raspberry Pi.