Troubleshooting
MQTT connect fails when TCP works: diagnose the handshake
Short answer
A successful raw TCP open to the broker port proves only routing and an open port; MQTT error 28 at Connect means the gateway ended the call with no usable session. Read the broker log during the attempt: no logged attempt means the gateway sent no MQTT data, a logged refusal means authentication or broker configuration, and an accept followed by an immediate close means a duplicate client identifier.
On this page
An MQTT Mqtt "Connect" that fails with Operation failed (28) 6 while a raw TCP open to the same address and port succeeds is not a contradiction. The TCP test proves routing and an open port; it says nothing about the MQTT exchange above it. Error 28 is the gateway's no-connection return, and its sub-code has no published definition in the material available, so the failing stage has to be read from the broker side. Trigger one connect while the broker log is open and commit to the branch it shows: no logged attempt means the gateway sent no MQTT data, a logged refusal means authentication or broker configuration, and an accept followed by an immediate close means a duplicate client identifier.
What error 28 establishes #
The case: an Ewon Flexy 205 on firmware 14.8s0 (#2633) opens a session with Mqtt "Open","EwonClient","192.168.1.7", sets Mqtt "SetParam","Port","1883", then calls Mqtt "Connect" — and the call fails every time, although the connection wizard passes and the broker is listening.
Error 28 is the script-level no-connection return. BASIC examples shared for this gateway family handle it by testing IF ErrorReturned= 28 THEN, executing MQTT "CLOSE", and jumping back to the connect step: the code means "no usable MQTT session at the moment of the call", not "firewall", not "credentials", not "broker fault". Writeups that cover the code treat a 28 appearing immediately after Mqtt "Connect" as a WAN-interface-not-ready condition — the gateway's data path was not up at that instant, typically because the script ran before the interface finished coming up.
The sub-code does not name a stage. The vendor's support portal carries several threads on (28) 6 without a published table, and a separately reported (28) 186 case pointed at a wrong private-key file. Decision: treat the code as "session not established" and spend your time on the broker log, not on the number.
What the passing TCP test proves #
MQTT is an application protocol that rides on TCP, so the two tests measure different layers. The successful OPEN "tcp:192.168.1.7:1883,5" FOR BINARY OUTPUT AS 1 established three things at the moment it ran: the route to 192.168.1.7 worked, port 1883 was open, and the firewall rule did its job.
It established nothing about the CONNECT/CONNACK exchange, authentication, or session lifetime — and it is a point-in-time result. The wizard and the TCP test can both be green while the interface drops seconds later, which is precisely the timing error 28 reports. Decision: stop re-testing the firewall; the evidence already cleared that path. The fault sits above the transport layer, or in timing.
The deciding test: one attempt, three outcomes #
Run the connect once with the broker watching.
- On the PC hosting Mosquitto, open the broker log so new lines appear as written, raising the log level if the default is quiet.
- Trigger a single
Mqtt "Connect"from the gateway — a manual run or one pass of the script. - Take the branch the log shows, and only that branch.
| Broker log during the attempt | What it establishes | Action |
|---|---|---|
| No connect attempt at all | The gateway sent no MQTT data | Branch A: session target, path timing, or protocol mismatch |
| A connect refused with a reason | The broker rejected the exchange | Branch B: fix the named condition in the config the service loads |
| Accepted, then closed at once | The session failed, not the transport | Branch C: duplicate client identifier |
Branch A: nothing reaches the log #
- Confirm the MQTT session targets the path the TCP test proved.
Mqtt "Open","EwonClient","192.168.1.7"withMqtt "SetParam","Port","1883"must describe the same endpoint astcp:192.168.1.7:1883,5; a session pointed anywhere else fails while your transport evidence stays green. - Re-check connectivity at the moment of failure, not an hour before it. A wizard pass at boot says nothing about the interface state when the script actually ran.
- Add the 28 handling around the connect: on
ErrorReturned= 28, runMQTT "CLOSE", wait, then retry. A 10 s interval covers the boot-time race where the script beats the interface. - Settle any dispute with a capture on the broker PC, filtered to the gateway address 192.168.1.18 and port 1883. A completed handshake with no bytes after it confirms this branch; MQTT bytes the broker does not log send you to branch C.
- Check which protocol the listener actually serves. A plain connect against a TLS listener opens TCP and dies at the first byte — the reported
(28) 186private-key case sits in that same family of mismatch.
If the wide-area leg itself is the flapping component, the durable answer is PLC MQTT failover to a redundant second broker rather than an ever-tighter retry loop.
Branch B: the log refuses the connect #
The refusal line names the connecting address and the reason. For MQTT 3.1.1, the CONNACK return codes are:
- 0 — connection accepted
- 1 — unacceptable protocol version
- 2 — identifier rejected
- 3 — server unavailable
- 4 — bad user name or password
- 5 — not authorized
The trap in this case file: allow_anonymous true is set in mosquitto.conf, but a line in a file proves nothing about the running process. The service may have loaded a different configuration path. Confirm which file the active broker process reports, put the listener and the anonymous setting in that file, restart, and read the log again. Decision: an anonymous refusal is a loaded-configuration problem, not a network problem — and on a production segment the fix is credentials, not anonymous access.
Branch C: accepted, then closed at once #
The CONNECT was processed; the session is what broke. First suspect: another live client holding EwonClient. Under MQTT 3.1.1, a broker that receives a second connection with an identifier already in use disconnects the existing one — so two auto-reconnecting clients sharing one identifier trade kicks indefinitely, and the gateway reports 28 whenever its session is the one displaced. Look for a desk test client left connected during commissioning, a second instance of the script or task, or a replaced gateway whose predecessor never went away. Fix: one unique identifier per client, duplicates stopped, then retest.
Worked example: timing the collision #
Suppose the log shows a flap. A desk client connects as EwonClient at 14:02:05. The gateway retries every 15 s, so its connect lands at 14:02:20 (14:02:20 − 14:02:05 = 15 s); the broker accepts it and disconnects the desk client. The desk client auto-reconnects 10 s later at 14:02:30 (14:02:30 − 14:02:20 = 10 s) and displaces the gateway, whose next try comes at 14:02:35. One full kick cycle is 15 s + 10 s = 25 s, and no session survives longer than 15 s.
Now the keepalive: with KeepAlive 60 s, a broker holds an idle session for 1.5 × 60 s = 90 s before its own timeout cleans it up. The kicks arrive every 10–15 s, well inside 90 s, so that timeout never fires and the flap runs until an identifier changes. Decision: rename the desk client, reserve EwonClient for the gateway alone, and require 300 s of log showing one connect line and zero displacements — at the old cadence those 300 s would have produced about twelve kicks (300 s ÷ 25 s = 12).
Verify the fix #
Mqtt "Connect"returns withoutOperation failed (28) 6and the broker log shows the gateway connected. Both conditions, not either.- Pull the gateway's network link for a minute and restore it: the close-and-retry loop should re-establish the session, with the recovery visible in the log.
- A second raw TCP test adds no evidence; transport was never the open question.
- Once the session holds, delivery failures belong to a different layer — start from fixing a PLC MQTT client that never reaches your broker when connects succeed but publishes vanish.
If the outcome is a broker re-hosted on dedicated plant-floor hardware, settle first whether to run an MQTT broker in the controller or on a separate device. A fanless mini PC can carry that local broker in the cabinet.
Questions asked
What does error 28 from Mqtt "Connect" actually mean?
It is the gateway's no-connection return: at the moment of the call it had no usable MQTT session. The shared handling pattern runs MQTT "CLOSE" and retries the connect. The sub-code is not a stage indicator — (28) 6 has no published definition and a reported (28) 186 case traced to a private-key file — so the failing stage comes from the broker log, not from decoding the number.
My TCP test to port 1883 succeeds. Is the firewall cleared?
Yes, for that path at that moment: routing, the open port, and the firewall rule were proven together by one test. It stays proven only until something changes — a policy push, a profile switch, an interface that drops after boot. If MQTT still fails after a passing TCP test, the fault is above the transport layer or in timing, not in the firewall.
The broker log refuses the gateway as anonymous, but allow_anonymous true is in mosquitto.conf. Why?
Because the running service probably loaded a different configuration file than the one you edited. Find the configuration path the active process reports, put the listener and allow_anonymous true in that file, restart the service, and confirm from the log that the refusal is gone — never assume the edit reached the process.
Why would a broker accept the gateway and close it a second later?
Usually a duplicate client identifier. MQTT 3.1.1 makes the broker disconnect the existing client when the same identifier reconnects, so two auto-reconnecting clients trade kicks and neither stays up. Check first for a desk test client or a second script instance using the same identifier, then give every client a unique one.
How long does a broker hold a session after a gateway disappears?
Up to 1.5 times the keepalive with no traffic — 90 seconds at KeepAlive 60 — unless the client sends a clean DISCONNECT, which ends it at once. A reconnect with the same identifier inside that window is legal and simply replaces the old session, so a stale session by itself never blocks a reconnect.