IMD SolutionComputing LibraryShop main store

How-to

Checking MQTT connection status in an Ewon Flexy BASIC script

Edge computing and IIoT Published 6 min read By IMD Solution engineering team

Short answer

On an Ewon Flexy with firmware 14.x or later, a BASIC script reads the MQTT link state with the MQTT Status command: a returned 5 means the connection is operational and any other value means it is not. The firmware owns disconnection and reconnection, so the script only monitors and logs, and you investigate the internet path when the broker never comes back on its own.

On this page
  1. How do you read the MQTT connection status in a Flexy BASIC script?
  2. Which returned value means the MQTT link is operational?
  3. Should the script force the MQTT reconnect itself?
  4. How do you log an overnight outage without flooding the log?
  5. What do you check when the Flexy never reconnects by itself?
  6. Does the status read tell you the MQTT QoS level?
  7. What should the script do for each status result?
  8. Questions asked

You do not need to infer the MQTT link state from failed publishes. On an Ewon Flexy running firmware 14.x or later, a BASIC script reads the state directly with _status% = MQTT "Status": the returned value 5 means the connection is operational, and any other value means it is not. Reconnection stays under firmware control, so the script's job is monitoring and logging. QoS is a separate publish setting that this read does not report.

How do you read the MQTT connection status in a Flexy BASIC script? #

From firmware 14.x onward, you read the state with the documented command _status% = MQTT "Status", which stores an integer result in a variable of your choice for use anywhere the script needs the link state. The command and its quoted word are the documented parts; the variable name is yours, and _status% is only the name the vendor's guidance uses.

  1. Pick an integer variable to hold the reading; any integer name works, and _status% keeps the example recognizable.
  2. Place the read where the script runs repeatedly, or anywhere a branch should depend on the link.
  3. Compare the stored value with a plain integer test; the operational value is fixed, and that comparison is the entire integration.

The test is binary: a returned value of 5 means the MQTT connection is operational at the instant of the call, and any other value means it is not, with no further meanings documented for the other numbers. Do not build a cause table from values you happen to observe, because the documentation defines two outcomes only.

Returned value Meaning What it establishes
5 MQTT connection operational The session to the broker is up at the instant of the read
Anything else MQTT connection not operational The session is down; the number does not encode the cause

The vendor's support discussions show the identical test inside a function named ONMQTTSTATUS, checking IF $status% = 5 THEN, so the same binary comparison holds wherever you take the reading.

Should the script force the MQTT reconnect itself? #

No: the Flexy firmware manages MQTT disconnection and reconnection, the documented guidance is to do nothing about reconnection in the script, and a non-operational reading is a signal to record, not a trigger to act on. A script-side retry loop of sleep, re-check and republish duplicates work the firmware already does and can interleave with the firmware's own session setup.

Worse, a script that reconnects on its own masks the one fact you need for diagnosis: whether automatic recovery works. Leave the recovery to the firmware and let the script watch.

How do you log an overnight outage without flooding the log? #

Poll on a fixed interval, for example every 60 seconds, and write a log entry only when the returned value changes, so one night with a single drop produces three lines instead of hundreds.

  1. Call the status read where the script runs repeatedly, on the interval you chose.
  2. Keep the previous reading in an integer of your own, named prevStatus% in this example and initialized to -1, a sentinel picked because the unit does not return it, so the first read always logs.
  3. When the fresh reading differs from the stored one, log the new state with a timestamp and store it.
  4. Touch nothing about reconnection; the log exists to show when the link dropped and whether the firmware restored it.
_status% = MQTT "Status"
IF _status% <> prevStatus% THEN
  IF _status% = 5 THEN
    PRINT "MQTT link operational"
  ELSE
    PRINT "MQTT link not operational"
  ENDIF
  prevStatus% = _status%
ENDIF

_status% is an integer set by the documented read, prevStatus% is the illustrative integer holding the previous state, and the PRINT lines are example output.

Worked example with illustrative numbers: with a 60-second interval, an unattended window from 22:00 to 06:00 is 8 h × 3600 s/h = 28800 s, so the script performs 28800 s ÷ 60 s = 480 reads. Suppose the mobile provider drops the data path at 02:10 and the firmware restores the session at 02:25: the outage lasts 15 min × 60 s/min = 900 s, so 900 s ÷ 60 s = 15 consecutive reads return a non-operational value. Transition-only logging turns the 480 reads into three lines — up at the start, down at the first read after 02:10, up again at the first read after 02:25 — and the worst-case detection latency is one interval. Recovery time belongs to the firmware and the data path, not to the script.

Prove the monitor with a single interruption: pull the WAN link on a bench unit, or during a maintenance window on the production gateway, because the test stops live data. The console must show the not-operational line within one interval; restore the link, and the operational line returns with no script action. That sequence is the pass criterion.

What do you check when the Flexy never reconnects by itself? #

When the reading stays non-operational and the firmware does not restore the session, move off the script and check the internet path first, because that is the documented suspect when a Flexy on firmware 14.x never comes back on its own.

  1. Data path: confirm the Flexy's internet connection is up — on a mobile unit, the data SIM's session and the signal. If the data path is down, the MQTT symptom is a consequence; repair the connection and the firmware reconnects.
  2. Broker reachability: verify the configured address and port from the Flexy, and that the broker itself is running.
  3. Name resolution: if publishes reach the broker by IP address but fail by hostname, the fault is DNS, traced in why an Ewon Flexy 205 publishes MQTT by IP but not hostname.
  4. TLS: a status that parks at 3 right after you enable TLS is a distinct symptom with its own fix, described in why an Ewon MQTT status stays at 3 after enabling TLS.
  5. Script logic: only when the four checks above pass, examine the publish calls themselves.

Does the status read tell you the MQTT QoS level? #

No: QoS is a separate publish setting, the Flexy supports all three MQTT QoS levels 0, 1 and 2, and the status value reports only whether the connection is operational, never which level a message used. In MQTT, QoS 0 sends at most once, QoS 1 sends at least once against an acknowledgement, and QoS 2 sends exactly once through its full handshake; choose per tag, not per link state. If you rely on buffering to carry data through outages, a replay that freezes is its own failure with its own trace, covered in why buffered MQTT replay freezes on an edge gateway.

What should the script do for each status result? #

The decision has three cases: keep logging while the link is operational, log both transitions when the firmware recovers a drop on its own, and escalate to the internet-path checks when it never does.

Reading over time What it means Script decision
Operational on every read The link is healthy Log state changes only
Non-operational, then operational again The firmware reconnected after a drop Log both transitions; take no action
Non-operational with no recovery The firmware cannot restore the session No script retries; run the data-path, broker, DNS and TLS checks

That is the whole division of labor: the read gives you the fact, the firmware owns the recovery, and the script owns the record.

Questions asked

Does the MQTT Status command work on firmware older than 14?

The read is documented from firmware 14 onward, so an older build gives the script no supported way to query the link. The workable path is upgrading to a release in that series, after which the same command and the same value test apply; guessing the state from failed publishes is not a substitute.

Does an operational status prove the broker received my message?

No. The value reports the session state at the instant it is read, not the fate of any message. Delivery evidence comes from the QoS machinery instead: QoS 0 offers none, QoS 1 confirms with an acknowledgement, and QoS 2 completes its handshake. Use the status read for link health and the acknowledgements for delivery.

Does polling the status faster make the Flexy reconnect sooner?

No. The command is a passive query: it reports the state and plays no part in the reconnection, which the firmware runs on its own schedule. A shorter interval only narrows the detection latency in your log; it does not shorten the outage, and a one-minute interval is a sensible monitoring choice.

Keep reading

  • Troubleshooting

    Why edge gateway MQTT stops sending after a reboot

    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.

    Edge computing and IIoT

  • How-to

    Buffering MQTT messages while the broker connection is down

    From firmware 14 the Flexy can detect its own MQTT disconnection from BASIC: the script publishes live while the connection is up, appends each payload as a line to /usr/mqttmsg_db.txt while it is down, and replays the file at QoS 2 when the connection returns. The 5000000-byte cap covers roughly a day of outage at a 5-second publish rate for 300-byte payloads.

    Edge computing and IIoT

  • Troubleshooting

    Cyclic communication enabled but no Edge connection or key

    An enabled cyclic communication setting is permission, not a connection. Put the Edge endpoint port inside the documented 33333 to 33340 range, prove the service reachable on that port instead of trusting an ICMP ping, then complete the documented key provisioning; commissioned means r4446.17 reads Yes and r4446.0 reads Yes.

    Edge computing and IIoT

Why an ASP Historian to PI Web API connection test fails