A temperature sensor, a dashboard, and a light should not each need to know how the others are built. MQTT gives them a shared message system: one device publishes to a topic, and interested devices subscribe.
A Raspberry Pi running Mosquitto is a useful place to learn that pattern. This first build keeps the broker on the Pi itself and uses two terminal sessions to send a message. Real networked sensors can come afterward.
Understand the three moving parts
The broker receives and distributes messages. A publisher sends a payload to a topic such as workshop/bench/temperature. A subscriber listens to a topic or a matching group of topics. The broker does not automatically know whether a number means Celsius, humidity, or battery charge; you define that convention.
Choose predictable topic names before writing firmware. Include location, device, and measurement where useful. Keep commands separate from observations so a dashboard cannot accidentally treat a temperature reading as an instruction.
Install a local broker
On Raspberry Pi OS, install the broker and command-line clients. The packaged service should start automatically; explicitly enabling it also makes the intended reboot behavior clear.
sudo apt update
sudo apt install mosquitto mosquitto-clients
sudo systemctl enable --now mosquitto
systemctl is-active mosquitto
sudo ss -ltnp | grep ':1883'
A default Mosquitto 2 installation without a configured listener operates in local-only mode. Verify the actual listener output on your machine. If you previously configured a network listener, these assumptions no longer apply: review the files under /etc/mosquitto before continuing.
Send your first message
Open two SSH sessions to the Pi. In the first, subscribe to the test topic. The command stays running while it waits for messages.
mosquitto_sub -h localhost -t 'workshop/bench/temperature' -v
In the second, publish a sample reading. This is illustrative data, not a measurement from a connected sensor.
mosquitto_pub -h localhost -t 'workshop/bench/temperature' \
-m '{"celsius":21.5,"source":"demo"}'
The subscriber should display the topic and JSON payload. Press Ctrl-C to stop the subscriber. Repeat with a different value so you know you are seeing new messages rather than reading a line left in the terminal.
Decide what a message means
A good sensor payload needs context. Plan units, timestamps, device identity, and how a consumer should treat stale data. An absent reading is different from a real measurement of zero. Your dashboard should make that distinction visible.
Retained messages can give new subscribers the most recent value immediately. They are helpful for state, but can surprise you with old information after a device goes offline. Do not casually retain commands that could be replayed to an actuator. Start without retention until your consumer behavior is clear.
Add networked devices deliberately
A sensor on another device cannot reach a broker bound only to localhost. For a real deployment, configure a listener on the intended private interface, disable anonymous access, create individual credentials, and define topic-level access controls. Use TLS or an appropriately protected network transport; a password alone does not encrypt MQTT traffic.
Those choices depend on your network and devices, so do not “fix” a connection failure by exposing an anonymous listener to every interface. Follow Mosquitto's authentication and configuration references for that next stage, and test a disallowed topic as well as an allowed one.
Build a useful next step
Before connecting physical hardware, write down the contract for a single sensor: topic, payload, units, update interval, and offline behavior. Then simulate it with the publisher command until the dashboard behaves correctly.
If nothing arrives locally, confirm both clients use exactly the same topic; topic names are case-sensitive. Inspect journalctl -u mosquitto --since '10 minutes ago' for broker errors. Once the message path works, connect it to your Home Assistant project or your own sensor application.
Sources and further reading
- Mosquitto: broker behavior and local-only mode
- Mosquitto: configuration, listeners, and access control
- Mosquitto: publisher command reference
- Mosquitto: subscriber command reference
Researched from the official documentation linked above on September 10, 2026. Setup examples are starting points and have not been hardware-tested by Open Pi. Check your board, OS, and the project’s current instructions before installing.