Troubleshooting
Why a Flexy BASIC script publishes MQTT once then stops
Short answer
The SENDDATA timer handler publishes only when MQTT STATUS returns 5, and the script never calls MQTT CONNECT again after a failure, so one LSTERR 28 at startup or any later drop silences every following cycle. Trace STATUS on each cycle, add a controlled reconnect to the connect label, and move the SUBSCRIBE loop after CONNECT.

On this page
- Rule out the firmware-specific one-publish drop
- Trace the status gate on every timer cycle
- One failed connect stops every later publish
- Move the SUBSCRIBE loop after the CONNECT call
- Publish a QoS 0, retain 0 baseline
- Worked example: the counts a healthy line produces
- Confirm the path to the broker and the panel
- Decisions by trace result
- Questions asked
A Flexy BASIC script that publishes every tag once and then goes quiet usually still has a working timer; what stopped is the gate in front of the publish loop. In the common sample layout behind this symptom — MQTT "OPEN", SETPARAM, a SUBSCRIBE loop, MQTT "CONNECT", ONMQTT and ONTIMER — the SENDDATA handler runs its tag loop only when MQTT "STATUS" returns 5, and no line ever calls MQTT "CONNECT" again after the first attempt. One failed connect (GETSYS PRG,"LSTERR" returns 28, the WAN interface not yet ready) or one later drop is therefore permanent: the timer keeps firing every 5 seconds and every cycle ends without a message and without an error. Trace the status on every cycle, add a controlled reconnect, and move the SUBSCRIBE calls after the connect; the checks below follow that order.
Rule out the firmware-specific one-publish drop #
On firmware 14.0s01 a Flexy MQTT client can drop right after its first publish for reasons that sit in the firmware, not in this script's logic; that case is covered in why a Flexy MQTT client drops after one publish on firmware 14.0s01. Check the gateway's running firmware version first. If it is 14.0s01, apply that fix before editing the script. On any other version, or once that cause is ruled out, continue with the status trace below.
Trace the status gate on every timer cycle #
Timer 1 is armed with ONTIMER 1,"GOTO SENDDATA" and TSET 1,5, so the handler runs every 5 seconds. SENDDATA reads ConnStatus% = MQTT "STATUS" and reaches the tag loop only when ConnStatus% = 5; every other value falls through to END with no print and no publish. That silent skip is why the gateway itself looks healthy while the broker receives nothing.
Add one print before the IF so every cycle reports its state (ConnStatus% is an integer):
SENDDATA:
ConnStatus% = MQTT "STATUS"
PRINT "mqtt status " + STR$ ConnStatus%
Read the trace against these cases:
- The status line repeats on every cycle and no publish line follows: the timer and handler are alive and the session is down; continue at the connect check.
- The status line stops repeating: the timer itself died; re-check the ONTIMER and TSET registration and whether an unhandled error cleared the event.
- The status stays at 5 and publish lines repeat: the publisher works; continue at the QoS baseline and the downstream checks.
- Publish lines appear for some tags only: compare GETSYS PRG,"NBTAGS" with the Topic$ entries filled at init and with j%, because a shifted count moves the loop bounds.
Each value MQTT "STATUS" can take is laid out in checking MQTT connection status in an Ewon Flexy BASIC script.
One failed connect stops every later publish #
The CONNECTMQTT block runs once, at script start. It opens the client with MQTT "OPEN", SerNum$, MQTTBrokerURL$ against tools.ewonsupport.biz on port 1883, then sets SETSYS PRG,"RESUMENEXT",1 so execution continues past an MQTT "CONNECT" failure. The script reads GETSYS PRG,"LSTERR"; a value of 28 means the WAN interface was not yet ready, the normal state right after a power failure while the WAN link is still coming up. The script logs that, clears RESUMENEXT, arms ONMQTT and ONTIMER and ends. Nothing ever returns to CONNECTMQTT, so an error 28 at startup is permanent until a manual restart.
The same applies to any later drop. The MQTT 3.1.1 specification requires the broker to disconnect a client that has sent nothing for one and a half times the keepalive interval; with KEEPALIVE set to 10, that limit is 15 seconds. After a WAN failure the session is gone quickly, MQTT "STATUS" stops returning 5 and the gate stays shut. A cellular link can also leave the client looking open locally after the broker has already ended the session — why an MQTT client stays open after a cellular WAN drop covers that split.
Add the reconnect as an ELSE branch on the same status check:
IF ConnStatus% = 5 THEN
Rem existing tag publish loop
ELSE
GOTO CONNECTMQTT
ENDIF
Keep SETSYS PRG,"RESUMENEXT",1 active across the jump, so a failed reconnect returns control instead of stopping the script, and leave the reconnect behind the timer interval rather than in a tight loop, so a broker outage does not turn into a continuous reconnect storm. Where the connect-time error itself comes from is traced in why the Flexy AWS IoT script fails at MQTT CONNECT with error 28; the point here is that this script never retries it.
Move the SUBSCRIBE loop after the CONNECT call #
As written, the script subscribes before it connects: the For i% = 1 To j%-1 loop calls MQTT "SUBSCRIBE" on SerNum$ + ":" + a$ for every tag, and MQTT "CONNECT" comes only afterwards. A subscription belongs to an established session, so a subscribe issued before the connect cannot attach to a live session. Move the loop to after MQTT "CONNECT", once MQTT "STATUS" reaches 5, keeping the QoS argument of 1 each call passes today. This corrects the inbound direction; it does not by itself restart the outbound publisher, which is what the reconnect branch above does.
Publish a QoS 0, retain 0 baseline #
Run one test with the trailing arguments of the publish call set to zero: MQTT "PUBLISH", TopicToPublishOn$, MsgToPublish$, 0,0. QoS 1 obliges the broker to acknowledge each message, and retain 1 asks it to keep the last payload per topic; the baseline strips both behaviors, so whatever continues or stops is the live path. If samples now flow continuously, the boundary sits in the acknowledgment or retained-message handling between broker and panel: restore 1,1 one argument at a time and watch the broker-side receipt after each change. If samples still stop at the baseline, QoS and retain are not the fault; the gate and the reconnect are.
Worked example: the counts a healthy line produces #
Take a gateway with four tags configured. GETSYS PRG,"NBTAGS" returns 4, the init loop increments j% once per discovered tag and leaves it at 5, and the publish loop runs from 1 to j% - 1, which is four MQTT "PUBLISH" calls per cycle. TSET 1,5 fires the timer every 5 seconds: 12 cycles per minute, 720 cycles per hour, so a connected session delivers 4 × 720 = 2,880 publishes per hour. Once the gate closes, the handler still executes all 720 cycles per hour and produces zero publishes; the console flips from a steady stream of publish lines to silence without a single error message. If the console count sits far below that rate while the status print reads 5, recount the Topic$ entries against NBTAGS before blaming the connection.
Confirm the path to the broker and the panel #
- Pick one tag and note its exact name after RTRIM, because the runtime topic is built from that trimmed string.
- Subscribe a test client at the broker to the runtime topic: serial number, colon, tag name — the string SENDDATA assembles in SerNum$ + ":" + a$. The two CONFIG paths,
"/topic/flexy/" + SerNum$ + "/data"and"/topic/flexy/" + SerNum$ + "/command", are declared but never reach the broker: SENDDATA overwrites the publish string before every call, and the command string is never passed to MQTT "SUBSCRIBE". The topic layout behind these colon-delivered names is covered in how to publish eWON MQTT tags as separate topics. - Watch the console while the status print reads 5: one publish line per tag per cycle, including consecutive identical payloads, because a QoS 1, retain 1 publish sends the same value again and the broker must receive it again.
- Change that tag's value and confirm the new payload crosses the same topic into the downstream panel.
- If the console and the broker both repeat but Grafana shows a single point, the script has done its job and the fault sits downstream: timestamping, duplicate-value handling or the panel's query interval.
Decisions by trace result #
- Status other than 5 on the trace, or a first burst that stops: the session is down and nothing retries it — add the GOTO CONNECTMQTT branch and keep RESUMENEXT across the attempt.
- Status at 5 with publish lines missing or partial: the loop bounds are wrong — recount Topic$ against GETSYS PRG,"NBTAGS".
- Continuous at the 0,0 baseline but not at 1,1: the acknowledgment or retained-message path between broker and panel — step the arguments back one at a time while watching the broker receipt.
- The broker receives every cycle but the panel shows one point: downstream timestamping, duplicate handling or query interval; the script is not the cause.
- Firmware 14.0s01: apply the firmware-specific fix first; the script edits above come after that cause is ruled out.
Questions asked
Does the SendTagValues flag stop the publishes?
No. SendTagValues% = 1 in the CONFIG block only marks that tag values should be sent; no line in this script reads the variable afterwards, so setting it to 0 neither stops nor starts the publish loop. The loop is controlled by the MQTT STATUS check in SENDDATA alone, so a silent script cannot be fixed by toggling this flag.
How many messages should each timer cycle produce?
One MQTT PUBLISH call per discovered tag: the loop runs from 1 to j% minus 1, so four configured tags give four messages per cycle. The Hello From Flexy payload defined in the CONFIG block is overwritten by the tag value inside that loop, so the greeting never reaches the broker; seeing it on the wire would mean a different code path is publishing.
Why does the panel show one value after publishing stopped?
With retain 1 set on every publish, the broker keeps the last payload for each topic and hands it to a subscriber at connect. A single point in the panel can therefore be that retained delivery rather than live traffic. Compare the message receipt time at the broker with the point timestamp in the panel: if nothing has arrived since the drop, the script went silent and the point is stale.
Do the same topics carry writebacks to the tags?
Yes. The ONMQTT handler reads MQTT MSGTOPIC and MQTT MSGDATA, matches the incoming topic against the same serial-colon-tag string and writes the value to the tag with SETIO. Because both directions use the same runtime names, a command sent to the declared command path updates nothing; it must be published to serial number, colon, tag name.
Should I test against the sample broker?
Run one test with MQTTBrokerURL$ left at the sample default, tools.ewonsupport.biz. If publishes flow there but not on the production broker, the script and its gate are working and the fault sits in the broker listener, authentication or access rules. Restore the production URL once the comparison is made.


