Half of all "my ESP32 won't talk MQTT" problems are really broker problems, and you can find them in two minutes without touching any hardware. The Mosquitto command-line tools let you publish and subscribe from a terminal, simulate sensors and devices, test logins and retained messages, and watch every message on the broker.
Quick answer: install the tools (sudo apt install mosquitto-clients on Linux or a Pi; they're included in the Mosquitto installer on Windows and in brew install mosquitto on macOS). In one terminal run mosquitto_sub -h BROKER -t 'test/#' -v, in another run mosquitto_pub -h BROKER -t test/hello -m 'hi'. If test/hello hi appears, the broker works. Add -u USER -P PASS if it needs a login, and use the broker's IP rather than localhost when testing from another computer.
This page is a practical recipe book: the options you'll actually use, simulating devices, testing retained messages and the Last Will, and a GUI alternative.
1. Install the Tools
| System | Command |
|---|---|
| Raspberry Pi OS, Debian, Ubuntu | sudo apt install -y mosquitto-clients |
| Windows | Install Mosquitto from mosquitto.org (untick the service if you only want the tools), then run from C:\Program Files\mosquitto or add it to PATH |
| macOS | brew install mosquitto |
Check they work with mosquitto_pub --help. The first line shows the version.
2. The First Test
Open two terminals. Terminal A subscribes:
mosquitto_sub -h 192.168.1.20 -t 'test/#' -vTerminal B publishes:
mosquitto_pub -h 192.168.1.20 -t 'test/hello' -m 'hello from my PC'Terminal A prints test/hello hello from my PC. -v (verbose) shows the topic before each message, which you'll want almost every time. Quote messages containing spaces.
If the broker needs a login, add -u USER -P PASSWORD to both commands.
3. The Options You'll Actually Use
| Option | Tool | What it does | Example |
|---|---|---|---|
-h | both | Broker host | -h 192.168.1.20 |
-p | both | Port (default 1883) | -p 8883 |
-u / -P | both | Username / password | -u nodered -P secret |
-t | both | Topic (wildcards only when subscribing) | -t 'home/#' |
-m | pub | Message text | -m '{"t":21.5}' |
-r | pub | Retain the message | -r |
-n | pub | Send an empty message (with -r, deletes a retained one) | -r -n |
-q | both | QoS 0, 1 or 2 | -q 1 |
-i | both | Client ID | -i pc-test |
-v | sub | Print the topic with each message | -v |
-C | sub | Exit after N messages | -C 1 |
-W | sub | Exit after N seconds | -W 10 |
-R | sub | Ignore retained (old) messages | -R |
--retained-only | sub | Show only retained messages, then exit | --retained-only |
-F | sub | Custom output format | -F '%I %t %p' (time, topic, payload) |
-d | both | Debug: show the MQTT packets | -d |
localhost or the IP? localhost means the computer you're typing on. It works on the Pi that runs the broker. From any other computer, use the broker's IP. If localhost works on the Pi but the IP fails from a PC, the broker has no network listener (Mosquitto 2.0's default): see installing Mosquitto on the Raspberry Pi.
4. Watch Everything on the Broker
The single most useful debugging command subscribes to every topic with a timestamp:
mosquitto_sub -h 192.168.1.20 -u USER -P PASS -t '#' -v -F '%I %t %p'Leave it running while you reset an ESP32 or deploy a Node-RED flow, and you see exactly which topics are published, how often, and with what payload. That's how spelling and case mistakes (home/Garage vs home/garage) show up immediately.
Wildcards: + matches exactly one level (home/+/temperature matches every room's temperature), and # matches everything below a level (home/garage/#). They work only in subscriptions, never in published topics.
Broker statistics: Mosquitto publishes its own status under $SYS, which # doesn't include:
mosquitto_sub -h 192.168.1.20 -u USER -P PASS -t '$SYS/broker/clients/connected' -t '$SYS/broker/version' -v5. Simulate Devices
A temperature sensor publishing every 5 seconds (Linux or a Pi, in bash):
while true; do
t=$(awk -v r=$RANDOM 'BEGIN { printf "%.1f", 20 + r % 100 / 10 }')
mosquitto_pub -h 192.168.1.20 -u USER -P PASS -t home/test/temperature -m "$t" -r
sleep 5
donePoint a Node-RED gauge or a Home Assistant sensor at home/test/temperature and build the whole dashboard before the hardware exists.
A JSON payload, as most sensors send:
mosquitto_pub -h 192.168.1.20 -u USER -P PASS -t home/test/climate -m '{"temperature":22.4,"humidity":51}'A device that obeys commands: send the command your ESP32 expects, and watch the state topic for its reply:
mosquitto_pub -h 192.168.1.20 -u USER -P PASS -t home/livingroom/lamp/set -m ON
mosquitto_sub -h 192.168.1.20 -u USER -P PASS -t home/livingroom/lamp/state -v -C 1-C 1 waits for one reply and exits, which is handy in scripts.
6. Test Retained Messages and the Last Will
Retained: publish with -r, then start a new subscriber. It receives the message immediately, even though it subscribed afterwards:
mosquitto_pub -h 192.168.1.20 -u USER -P PASS -t test/retained -m 'still here' -r
mosquitto_sub -h 192.168.1.20 -u USER -P PASS -t test/retained -v -C 1Delete it again with an empty retained message: mosquitto_pub ... -t test/retained -r -n.
Last Will: start a client that registers a will, then kill it uncleanly:
mosquitto_sub -h 192.168.1.20 -u USER -P PASS -t test/x --will-topic test/status --will-payload offline --will-retainIn a second terminal watch test/status. Pressing Ctrl+C in the first terminal disconnects cleanly, so no will is sent. Closing the terminal window or kill -9-ing the process is an unclean exit, and after about 1.5 × the keep-alive (60 s by default for these tools, so about 90 s) test/status offline appears. More in MQTT retained messages and Last Will.
Test the login is enforced: a publish without -u/-P should fail with Connection Refused: not authorised. If it succeeds, the broker accepts anonymous clients.
7. A Graphical Alternative: MQTT Explorer
MQTT Explorer (free, Windows, macOS, Linux) connects to a broker and draws every topic as a tree, with live values, message history, charts of numeric topics, and buttons to publish or delete retained messages. Enter the broker's IP, port 1883, and a username and password, then click Connect. MQTTX is a similar free client that also supports MQTT 5 and WebSockets.
They're ideal for seeing the whole topic structure at a glance; the command-line tools are better for scripts, quick checks over SSH, and logging.
Troubleshooting
| Message | Meaning | Fix |
|---|---|---|
Error: Connection refused | Nothing accepting connections at that IP and port | Broker stopped, wrong IP or port, or no network listener |
Connection Refused: not authorised. | Login rejected | Add or correct -u/-P; check the broker's password file |
Error: Unknown host | Hostname doesn't resolve | Use the IP address |
| Hangs with no output | Connected, but nothing published to that topic | Subscribe to # to see what is published |
| Subscriber prints old values immediately | Retained messages | Add -R to hide them, or clear them with -r -n |
Error: Problem setting will | --will-topic without a payload | Add --will-payload |
| Works on the Pi, not from a PC | Mosquitto 2.0 local-only default, or a firewall | Add a listener; open port 1883 |
Once the command-line tests pass, any remaining problem is in the device or app. Compare its host, port, login and topic with the command that worked. Return codes are decoded in MQTT troubleshooting: rc=-2 and rc=5.
FAQ
How do I test an MQTT broker without an ESP32? Use mosquitto_sub in one terminal and mosquitto_pub in another, or a GUI client such as MQTT Explorer.
How do I see all MQTT topics? Subscribe to #: mosquitto_sub -t '#' -v. Broker statistics are under $SYS/#.
Can I use a public test broker? For quick experiments, test.mosquitto.org and broker.hivemq.com accept anonymous connections. Everything you publish there is visible to anyone, so never send passwords, real sensor data or commands.
How do I delete a retained message? mosquitto_pub -t TOPIC -r -n.
Next Steps
Test transport security and clean up after experiments
Once a plain connection works on a trusted LAN, test the broker's security configuration separately. If the broker exposes a TLS listener, use its configured port and CA certificate rather than assuming that changing 1883 to 8883 is enough. The client must validate the certificate chain and connect using a hostname that matches the certificate. A test that disables certificate verification can help isolate a certificate problem temporarily, but it is not a safe production configuration.
A disciplined test sequence makes failures easier to locate:
- Subscribe with the expected account to a single test topic.
- Publish a short message with the same account and verify the exact topic and payload.
- Repeat with an incorrect password; the broker should reject the connection.
- Repeat with an account that lacks permission to publish to that topic; the broker should deny the operation.
- Test from a second machine using the broker's LAN address, not localhost.
- If TLS is enabled, verify both a trusted certificate and a deliberately invalid certificate.
Do not leave broad diagnostic subscriptions running longer than necessary. Subscribing to # can expose device names, telemetry and command payloads from every permitted topic. Use it only on a broker you administer and only while debugging. Public test brokers are shared infrastructure: never send credentials, personal data, private sensor readings or commands to them.
Retained messages can also make a test look as if a device is still online. A subscriber may receive an old retained payload immediately after connecting. Check whether the message is retained, publish a new timestamped test value, and clear test retained data when finished using an empty retained publish to the exact topic. Avoid clearing production topics unless you understand which consumers depend on them.
Save a reproducible test record
For a broker you maintain, record the broker version, listener address and port, authentication mode, topic, QoS, retain setting, client ID and exact command used. Redact passwords and private addresses before sharing logs. This small record makes it possible to reproduce a failure after a broker update or network change, and helps separate client-side problems from broker configuration problems.




