Troubleshooting
Why edge gateway MQTT stops sending after a reboot
Short answer
The script calls its MQTT connect before the WAN interface has an address, so Autorun runs it into a failed connect and the startup section stops before the publish timer is armed. Gate the first connect on a WAN address other than 0.0.0.0, guard it so the script continues past the error, and never reconnect from the publish loop: after the first session, the gateway's MQTT task reconnects on its own while the script only checks for status 5.
On this page
The gateway is not forgetting its MQTT configuration, and the broker is not dropping you. The script's startup section issues its MQTT connect seconds after power-on, while the WAN interface still has no address. Autorun is doing what it promises, starting the script early, and early is exactly the problem: the connect fails, and with error handling off, the startup section stops on that line before the publish timer is ever armed. Nothing reaches the broker until someone stops and restarts the script by hand, by which time the network is up and everything works again. Two changes make it permanent: gate the first connect on a WAN address other than 0.0.0.0, and never call connect again after the first session, because the gateway's own MQTT task reconnects by itself.
What the reported symptom points to #
In the reported case, two identical units ran fine and then stopped sending, with Autorun checked on both. Each observation rules something out:
- The script runs at boot and its banner prints, so Autorun itself is not broken.
- A manual stop and start of the script puts data back on the broker within one publish cycle, so the logic works once the network is up.
- Deleting the reconnect call from the publish loop stretched the runs to a reported eight hours, but the reboot failure stayed. Two faults, not one.
- A full reboot also revives sending, for the same reason the manual restart does: by then the network is ready.
A manual restart fixing what Autorun cannot is the signature of a startup-order fault. When you walk over and restart the script, the WAN interface has an address; at power-on it does not. If your units never connect at all, even hours after boot, the cause is different and lives on the broker side; that case is sorted at the end.
Fault one: the publish loop rebuilds the MQTT session #
The publish function read MQTT('status') and treated any value other than 5 as a reason to call the connect function again, up to three attempts in one publish cycle. That fails twice over. The connect call is asynchronous: it returns immediately and the session comes up in the background, so an instant recheck still reads not connected no matter how many times you call it. And after a first session has been established, reconnection belongs to the gateway's MQTT task. The script's only remaining job before a publish is to confirm the status is 5 and skip the cycle when it is not.
The retry counter allowed those three attempts with no delay between them, so all three fired while the first handshake was still in flight, and the script then printed its failure line and abandoned the cycle. Delete the connect call from that path: when the status is not 5, skip, and let the next timer tick find the task reconnected. In the reported case, that deletion alone carried the units to a run of about eight hours. That is a reported result, not a promise.
Fault two: the connect runs before the WAN interface is up #
At power-on, the script's init section runs before the network stack finishes. GETSYS PRG, "WANIP" returns 0.0.0.0 until the interface has an address, and on a cellular link the vendor's own support guidance flags the wait as longer still. A connect issued inside that window raises an error, and with error handling off, the startup section ends on that line.
The documented guard switches on continue-on-error for the connect, reads the last-error value and logs error 28 as WAN-not-ready:
SETSYS PRG, "RESUMENEXT", 1 // continue in case of error at MQTT "CONNECT"
MQTT "CONNECT"
ErrorReturned% = GETSYS PRG, "LSTERR"
IF ErrorReturned% = 28 THEN
PRINT "[MQTT SCRIPT] WAN interface not yet ready"
ENDIF
SETSYS PRG, "RESUMENEXT", 0
SETSYS PRG, "RESUMENEXT", 1 lets the script survive the failed connect. It does not wait for the WAN and it does not reconnect; the deferral is your code to write.
The corrected startup sequence #
- Read the local configuration file exactly as before.
- In the connect function, read
GETSYS PRG, "WANIP"first; while it still shows the all-zero address, log that and connect nothing. - Issue the connect once, inside the RESUMENEXT guard, and log the WAN-not-ready code.
- Re-run the connect function from a short deferral timer until
MQTT('status')reads 5; the status check at the top of the function makes the extra calls harmless once the session is up. - Make the publish function check
MQTT('status')for 5 and skip the cycle when it is not connected. It must never call the connect function. - Arm the publish timer as before and leave every later reconnection to the gateway's MQTT task.
FUNCTION mqtt_connect()
$status% = MQTT('status')
IF ($status% = 5) THEN GOTO $DONE
$wanIP$ = GETSYS PRG, "WANIP"
IF ($wanIP$ = "0.0.0.0") THEN
PRINT "[MQTT SCRIPT] WAN address not ready - connect deferred"
GOTO $DONE
ENDIF
SETSYS PRG, "RESUMENEXT", 1
MQTT "CONNECT"
ErrorReturned% = GETSYS PRG, "LSTERR"
IF ErrorReturned% = 28 THEN
PRINT "[MQTT SCRIPT] WAN interface not yet ready"
ENDIF
SETSYS PRG, "RESUMENEXT", 0
$DONE:
ENDFN
Trailing % marks an integer variable and $ a string variable in this dialect. $status%, $wanIP$ and the deferred log line are this example's own names; MQTT('status'), GETSYS PRG, "WANIP", SETSYS PRG, "RESUMENEXT", 1, GETSYS PRG, "LSTERR", MQTT "CONNECT" and error code 28 are the vendor-documented ones, shown exactly as the guidance prints them.
Worked example: sizing the deferral window #
Example inputs, chosen conservative for an Ethernet unit: the script starts 5 s after power-on, DHCP delivers the WAN address 35 s after power-on, the deferral timer fires every 5 s, and the window is capped at 120 s.
- Deferred attempts before the address appears: 35 ÷ 5 = 7 checks that log and retry.
- Attempts available in the whole window: 120 ÷ 5 = 24. If all 24 expire with the address still all-zero, startup order is no longer the cause; the link itself is (cable, switch port, APN or SIM state).
- Publish opportunities in the reported eight-hour run on the 10-second cadence: 8 h × 3,600 s/h = 28,800 s, and 28,800 s ÷ 10 s = 2,880 cycles.
On cellular, widen the cap rather than the step (240 s gives 240 ÷ 5 = 48 checks) because registration is the slow part, and a tighter interval only fills the log.
Verify with a power cycle, not a script restart #
A stop-and-start of the script cannot prove this fix, because the WAN interface is already up when you do it. The test that proves it:
- Full power off and back on.
- Exactly one WAN-not-ready line inside the deferral window, then a connect that reaches status 5.
- Data at the broker within one 10-second cycle of the session coming up.
- With the session established, drop the WAN link for a minute and restore it: the script should skip cycles while the status is not 5 and resume with no connect call of its own. If it stalls, a connect call is still hiding in the publish path.
- If the same link also carries control messages, run this test with the process able to tolerate a stopped link.
Which fix applies #
- Data stops after a reboot and a manual script restart revives it: startup order. Apply the WAN-gated connect with the RESUMENEXT guard and the deferral timer.
- Data stops after hours of running and the connect function sits in the publish retry loop: delete that call. The gateway's MQTT task reconnects; the script only checks the status.
- The gateway never connects, even minutes after a valid WAN address: not startup order. Work the broker side, address, port, credentials and TLS, with the client-side checklist in fixing a PLC MQTT client that never reaches your broker.
- Connects fail only right after boot when the broker is configured by hostname: name resolution is lagging the interface, and the same deferral covers it. The pattern is the one behind the MQTT sink that stayed offline until the broker IP address was used.
Questions asked
What does error 28 mean when the MQTT connect fails at boot?
In this startup path it means the connect was issued while the WAN interface still had no address, so the session could not even be attempted. It is a startup-order signal, not a broker or credential fault. Run the connect under continue-on-error, read the last-error value afterward, log it as WAN-not-ready, and let the deferral timer retry once the interface reports a real address.
Does the Autorun checkbox mean the network is ready when the script starts?
No. Autorun only guarantees that the script executes early in the boot process. The WAN interface, DHCP lease, cellular registration and name resolution can all still be pending at that moment. Treat Autorun as a promise that the script will run, not that the network is up, and gate each network call on state you can read at runtime, such as the WAN address.
Why does a broker hostname make the first connect fail more often?
A hostname adds a second dependency: name resolution must be working before the connect can even find the broker, and DNS can lag the WAN address by seconds. If connects keep failing after a valid WAN address is present, test once with the broker's IP address. If the IP form connects, the deferral window needs to cover DNS as well as the interface.
How large should the deferral window be before it hides a real fault?
Cap it where startup normally ends, plus margin; on cellular, registration is the slow case, so widen the cap rather than the check interval. If every check in the window still finds the all-zero address, stop deferring and troubleshoot the link itself, because startup order is no longer the cause.
The script logs the WAN-not-ready message on every boot now. Is that a problem?
One such line followed by a connect that reaches the connected status is the fix working: the script hit the not-ready window, continued past it, and connected once the address appeared. Only investigate if the message repeats after a valid address exists or the status never reaches 5; then the cause is on the broker side, address, port, credentials or TLS, not in the startup order.