Troubleshooting
Why MQTT publish returns error code 4 on your PLC
Short answer
Error code 4 is a defined status your PLC firmware added in version 3.21: the MQTT broker closed the TCP connection. Cycle the subscriber enable bit off for at least one scan, then on, and wire an auto-retry with a 3 second dwell, escalating back-off and a retry counter so the client heals itself without an operator.

On this page
- What does MQTT error code 4 mean when your PLC publishes?
- Why do the help files stop at code 3?
- Why did the broker close the TCP connection?
- How do you clear error code 4 and get the session back?
- How do you automate recovery so nobody toggles bits by hand?
- Why does error code 4 come back at a fixed interval?
- How do you verify the fix end to end?
- Questions asked
Error code 4 in the MQTT instruction's error register means the broker closed the TCP connection underneath your session. It is a defined status, not a corrupted value: the code was added in firmware version 3.21 and never made it into help files that stop at codes 1 through 3. The client will not re-establish the session on its own, so the fix is to cycle the subscriber enable bit off and back on, then wire that same cycle into the ladder as an automatic retry with back-off. The sections below cover the code's meaning, the broker-side causes, the recovery sequence, an auto-recovery pattern, and the keep-alive number that stops the fault from returning.
What does MQTT error code 4 mean when your PLC publishes? #
Error code 4 means the MQTT broker closed the TCP connection: the remote peer tore down the socket, not your ladder logic. The status was added in firmware version 3.21, so older help files that stop at code 3 simply do not list it.
When the value latches, the failure is transport-layer: the client had a session or was maintaining one, and the broker side ended the socket. Nothing in the ladder logic, topic strings or payload mapping is inherently wrong when this code appears. The symptoms travel together:
- The publish instruction stops transferring payloads and the error word latches at 4.
- Topics the PLC subscribes to stop updating their mapped data registers.
- The Ethernet link, IP settings and broker address are all unchanged; only the session is gone.
That last point matters for diagnosis. If your client has never managed to connect at all, that is a different fault with a different playbook, covered in fixing a PLC MQTT client that never reaches your broker.
Why do the help files stop at code 3? #
The code list in your help file predates the firmware on the CPU: code 4 was introduced with version 3.21 and was never added to that documentation, so a value the list calls undefined is a defined status on the controller.
The gap is documentation lag, not a fault in your project. If you see 4 on a CPU whose printed list ends at 3, your build is newer than the help file you are reading, and the release notes for that firmware level are where the code should have appeared. Treat 4 exactly as you would a documented code: read it, log it, act on it. What you should not do is spend an afternoon chasing a memory fault or rewriting payload logic because an old error table does not recognize the value the CPU is reporting.
Why did the broker close the TCP connection? #
The PLC reports only that the socket closed and carries none of the broker's reasoning, so the broker's own log is the authoritative record and each candidate cause below has a matching signature in it. Work the list against that log:
- Duplicate client ID — a second client connected with the same identifier, so the broker dropped the first. The log shows a new CONNECT with the PLC's client ID immediately before the disconnect; look for a cloned project, a test client or a second CPU left on a default identifier.
- Keep-alive expiry — no PINGREQ or publish arrived within the negotiated window. The log records a keep-alive timeout for the client ID; correlate with scan load or network congestion.
- Credential or permission rejection — the log shows an authentication failure or a topic-permission denial for the username in use.
- Broker restart or configuration reload — every client drops at the same timestamp, not just the PLC. The reconnect behavior after that kind of event is covered in why an MQTT subscriber stops receiving after a broker restart.
- Broker-side connection limit or rate throttle — the fault appears under high publish rates and the log carries a limit or quota message.
- An intermediate device closing idle sessions — firewall, NAT gateway, VPN concentrator or managed switch. The interval between faults is suspiciously regular while the broker log shows only a socket close with no reason. The keep-alive arithmetic for this case is in the fixed-interval section below.
- TLS failure on a secured port — the log shows a handshake or certificate failure at the moment of close. A TLS hop that dies before CONNACK is its own fault pattern, covered in finding the failing hop when MQTT TLS connects but no CONNACK arrives.
How do you clear error code 4 and get the session back? #
Turn the subscriber enable bit off, hold it off for at least one full PLC scan, then turn it on again; the client then issues a fresh TCP connect and CONNECT packet and the error register clears to 0 once the broker accepts the new session.
- Read the error register and confirm it holds 4.
- Turn the Subscriber Enable bit off.
- Hold it off for at least one full scan; if the broker enforces a reconnect back-off, a deliberate dwell of 1 to 5 seconds is safer than a single-scan blip.
- Turn the bit on. The client issues a new TCP connect and a CONNECT packet.
- Re-check the register: it should clear once the session is re-established.
This sequence is a fix, not a design. A broker restart in the middle of the night or a firewall that flushes idle sessions will re-trigger the fault with nobody at the panel, which is why the recovery belongs in the ladder.
How do you automate recovery so nobody toggles bits by hand? #
Drive the same off/on cycle from the error value itself: the fault latch resets the enable bit and starts a 3 second dwell timer, the timer's done bit sets the enable again, and a retry counter raises an alarm after 5 failed attempts. Build it from four rungs using spare bits and a timer in your project's free tags:
- Rung 1 — detect: compare the error register equal to 4 and set a fault latch bit.
- Rung 2 — drop the enable: while the latch is set, reset the enable bit and run an on-delay timer as the dwell.
- Rung 3 — re-enable: when the timer's done bit fires, set the enable bit, reset the latch and count one retry.
- Rung 4 — escalate: when the retry counter reaches 5, raise a communications alarm and hold it until someone acknowledges it.
Three details decide whether this heals quietly or chatters. Give the dwell a floor of 3 seconds and stretch it toward 10 to 30 seconds after consecutive failures, because a sub-second retry loop hammers the socket and can trip the same broker-side limits that caused the drop. Latch the last non-zero error value into a separate register before the recovery logic clears the live one, so the HMI shows what actually happened. And alarm on staleness: timestamp the last successful publish and flag it when it ages out, because error 0 alone does not prove data is flowing.
Keep this retry logic out of every path that feeds a safety or interlock function: the alarm belongs in the annunciator, never as a stand-in for the hardwired behavior the function requires. If the MQTT link carries setpoints, arbitrate them in the PLC program itself, as argued in why MQTT setpoint gating belongs in the controller rather than the client.
Why does error code 4 come back at a fixed interval? #
A disconnect that returns on a clean schedule points to a fixed idle timeout somewhere on the path — firewall, NAT gateway or VPN concentrator — and the MQTT keep-alive timer is the number that decides whether the session survives it.
Worked example. Say a firewall on the path tears down TCP sessions that have been idle for 60 s. With the MQTT keep-alive set to 30 s, the client sends PINGREQ every 30 s, and 30 s is less than 60 s — no quiet stretch on the wire ever reaches the firewall's limit, so the session survives indefinitely. Raise the keep-alive to 300 s and the picture inverts: the first quiet stretch reaches the firewall's limit long before the next PINGREQ is due, the firewall closes the socket, and the PLC latches error 4 roughly a minute into every quiet window. The decision falls straight out of the two numbers: set the keep-alive below the smallest idle timeout anywhere on the path — 30 s or less against a 60 s firewall. Most brokers allow about 1.5 times the keep-alive before they judge the client dead, so a short keep-alive costs one PINGREQ every 30 s and buys a session that every middlebox between PLC and broker leaves alone. If the fault interval is irregular instead, the idle-timeout theory is wrong and the broker-log signatures in the cause section are your next stop.
How do you verify the fix end to end? #
Verify three things before you call it fixed: the error register moves from 4 to 0 after the enable bit cycles, the broker log records a new CONNECT from the PLC's client ID, and payloads actually arrive at a subscriber. Work the checks in order, because each proves a different layer:
- Register transition: monitor the error register live and cycle the enable bit by hand; the value should clear once the session is rebuilt.
- Broker side: the log must record a new CONNECT from the PLC's client ID at the moment you re-enable — that is the definitive proof the session was rebuilt, not just that the PLC cleared its own flag.
- Payload flow: subscribe to the PLC's publish topic with an independent MQTT client, toggle a source register, and confirm messages arrive with changed values.
- Subscribe direction: publish a test message to a topic the PLC subscribes to and confirm the mapped register updates; a rebuilt TCP connection does not by itself re-register subscriptions, and the pattern in subscribing to MQTT and writing the value back to the PLC is the reference here.
- Auto-recovery: stop the broker service, confirm the PLC latches error 4 and begins retrying, restart the broker, and confirm the client reconnects with no operator action.
Questions asked
Can I just ignore error code 4 if publishes resume after a power cycle?
No. A power cycle clears the latch the same way the enable-bit cycle does, but it also drops every other communication path on the CPU and leaves the cause in place. The fault returns at the next broker restart or idle timeout, so treat a power cycle as a diagnostic, never as the fix.
Can a duplicate client ID cause error 4 even when the PLC project never changed?
Yes. When a second device connects with the same client identifier, the broker drops the older session, and the PLC reports that drop as code 4 even though nothing in its project changed. Give every device on the broker a unique client ID and check the log for the competing CONNECT.
Should I shorten the publish interval to keep the connection alive?
No. Publishes and PINGREQ both count as keep-alive traffic, so the keep-alive timer alone holds the session open at whatever interval your data actually needs. Shortening the publish period to babysit the socket adds messages on the wire without fixing an idle-timeout mismatch somewhere on the path.
What do I do when the retry counter keeps hitting its alarm limit?
Stop widening the retry limit. A counter that saturates means the broker or the path is still dropping the session, and the alarm is doing its job. Go back to the broker log and the idle-timeout arithmetic: a saturated counter with a regular interval points at a middlebox timeout, an irregular one at broker-side limits or credentials.


