Troubleshooting
Why a device cannot publish MQTT to a broker on your PC
Short answer
Point the device's client at the PC's LAN address, not 127.0.0.1 — loopback always means the device that opens the connection. Then confirm the broker listener is bound to the PC's LAN interface on TCP 1883 and that Windows Firewall admits inbound connections on that port from the plant subnet. Fix those three and the publish path works.
On this page
The answer is the address. A broker installed on a Windows PC is reachable from a plant device only at the PC's LAN address, on the port the listener is actually bound to, with the firewall open to it. When the device's script carries 127.0.0.1 or localhost, the connection stops on the device itself, because loopback always names the machine that opens the connection. Two more faults look identical from the device side: a broker bound to loopback only, and an inbound TCP 1883 block in Windows Firewall. Fix the address, the binding and the firewall rule, in that order, and the publish path works.
Where the connection stops #
The publishing device is the MQTT client: it opens the TCP session, so the address in its broker setting decides where the packets go. A working publish crosses seven stages, and each one is a place to be wrong:
- The device's LAN interface has an active link and a valid address.
- The device sends toward the PC's LAN address.
- Every switch or router between them forwards the packets.
- The PC's wired interface receives them.
- Windows Firewall admits the connection to the broker's port.
- The broker accepts the TCP session and processes the connect request.
- The broker's access policy accepts the client and its published message.
A failure anywhere up to the TCP accept is addressing, routing, firewall or listener binding. A failure after TCP is established is broker policy. Publish logic is in neither group: a script that already publishes to a cloud broker does not become wrong because the broker moved, so leave the topic and payload alone until the connection is healthy.
Loopback never leaves the device #
RFC 1122 sets the 127.0.0.0/8 block aside as internal host loopback and requires that traffic addressed to it never leave the host. Whichever machine opens the connection, 127.0.0.1 and localhost name that machine and no other. The address in the client's broker setting therefore decides the outcome on its own:
| Address in the client | What it names from the device | Verdict for a broker on the PC |
|---|---|---|
| localhost | The device itself | Wrong target |
| 127.0.0.1 | The device's loopback interface | Wrong target |
| The PC's LAN address | The PC's interface on the plant network | Correct |
| A routed address | A host reached through a router | Valid, but needs a route and firewall at every hop |
A broker test that succeeds on the PC through loopback proves only the local process. Replace the loopback entry with the PC's LAN address, and make that address stable — static assignment or a DHCP reservation — so a lease change cannot break the client later.
Prove the two hosts share a subnet #
Worked example. The PC holds 192.168.1.20 with mask 255.255.255.0; the device holds 192.168.1.50 with the same mask. AND each address with the mask to get its network:
- 192.168.1.50 AND 255.255.255.0 = 192.168.1.0
- 192.168.1.20 AND 255.255.255.0 = 192.168.1.0
Both hosts land on 192.168.1.0/24, so the traffic goes device, switch, PC with no router involved. Decision: with the address now correct, only the listener binding and the firewall rule remain as faults.
The failure case: a device at 192.168.10.50 with mask 255.255.255.0 computes 192.168.10.0, which is not 192.168.1.0. That path needs a route, and every router and firewall along it must pass the connection. Decision: when you control both ends, put the broker PC and the publishing device on the same subnet and routing leaves the fault surface; when you cannot, verify the route in both directions before touching the broker.
Read the PC's address from the wired adapter actually cabled to that network — not from a virtual, VPN or wireless adapter the PC also lists.
Bind the listener to the LAN interface #
A broker reported running with its port open can still be bound to loopback, and loopback accepts no external client. Verify what the socket actually holds: run netstat -ano on the PC, find the broker's port in the local-address column, and match the PID to the broker service. 127.0.0.1:1883 means loopback only; 0.0.0.0:1883 or the LAN address means external connections are accepted.
In Mosquitto, the listener directive takes the port and an optional bind address: listener 1883 0.0.0.0 accepts connections on every interface, and listener 1883 192.168.1.20 pins it to the plant network. The bind_address localhost form restricts the default listener to loopback, so remove it or point it at the LAN. A fresh Mosquitto 2.x install starts with both doors shut — loopback-only listening and anonymous clients refused — so a LAN test must change both. These directives are Mosquitto's; another broker exposes the same choice as a listener bind address in its own configuration. Restart the service after editing mosquitto.conf so the new binding takes effect.
Open TCP 1883 in Windows Firewall #
Inbound connections reach a Windows service only through an admitting rule. Create an inbound rule for TCP 1883 scoped to the plant subnet — 192.168.1.0/24 in the worked example — and to the network profile the PC's adapter actually uses, instead of a rule open on every network. Then test from a second device on that subnet, never from the PC itself: on another Windows machine, Test-NetConnection 192.168.1.20 -Port 1883 reports whether the TCP path is open. A ping proves only ICMP, not the port. If that test fails while the listener is correctly bound, the firewall rule is the remaining cause.
When TCP connects but the broker refuses #
If the TCP session opens and the broker drops it at once, the binding and the firewall are proven and the fault is policy — most often a no-credentials client meeting a broker that refuses anonymous connections. For the first controlled test, set the broker's anonymous-client option to permit the connection, scoped to the plant network, and leave every other client setting as the script already uses it. Connection-level failures at this stage have their own diagnostic path, starting with diagnosing the MQTT handshake when TCP already works.
Verify end to end, then tighten #
Run a subscriber on a second PC, subscribed to the topic the device publishes, and watch the broker log during the device's attempt: no new entry means the connection stopped before the broker; an entry followed by a disconnect is policy. Seeing the device's value arrive at the subscriber is the proof that matters — the round trip behind it is how MQTT clients publish and subscribe through a broker.
One limit to hold: never leave an anonymous listener reachable beyond the plant subnet; put credentials on the broker and re-scope the firewall rule before the connection enters normal service, following the pattern in running a secure Mosquitto lab broker.
The fix in order #
- Replace localhost or 127.0.0.1 in the device's broker setting with the PC's LAN address, and reserve that address.
- Confirm both hosts share a subnet with the AND calculation; readdress or route if they do not.
- Bind the broker listener to the LAN interface and restart the service; confirm the socket now shows 0.0.0.0 or the LAN address.
- Add the inbound firewall rule for TCP 1883, scoped to the plant subnet.
- Align the broker's anonymous-client setting with the no-credentials test client.
- Prove the path from a second device: TCP connect, then subscribe and watch for the device's values.
- Add credentials and re-scope the rule before normal service.
Questions asked
The broker answers on the PC at 127.0.0.1 — why does that not count as proof?
Because that test never leaves the PC. Loopback traffic stays inside the host's own network stack, so it exercises the broker process and nothing else: no Ethernet interface, no switch path, no firewall rule, no listener binding on the external interface. Only a connection opened by a second device on the plant network proves the path the publishing device will actually use.
Should the client use the PC's hostname instead of its IP address?
It can, as long as the plant network resolves that name to the PC's LAN address for every device that uses it. A static address or a DHCP reservation is the more dependable choice for a broker host. If any device resolves the name to a loopback address, that device connects to itself, so check what the name resolves to before you rely on it.
Is port 1883 the only port I need?
Port 1883 is the IANA-registered default for plain MQTT and the one this fix assumes. A listener serving TLS conventionally uses 8883 instead. The requirement is that the number in the client's broker setting, the port in the listener configuration and the port in the firewall rule are the same number — one mismatch among the three reproduces the exact symptom you are chasing.
Do I need to restart the broker after changing the listener?
Yes. Brokers that read their configuration from a file apply the listener binding at startup, so an edited mosquitto.conf takes effect only after the service is restarted or reloaded. After the restart, list the broker's sockets again and confirm the local address shows the LAN interface or 0.0.0.0 rather than loopback.