IMD SolutionComputing LibraryShop main store

Troubleshooting

Flexy AWS IoT MQTT: fix the CA file and the event callback

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

Short answer

A Flexy running Java ETK 1.4.4 fails its MQTT connection to AWS IoT for two separate reasons. The cafile option names /usr/root-CA.crt while the stored file is CA.crt, so rename the file to match. The old callMqttEvent callback calls readMessage() on event 1, a status event, which makes the gateway unreachable after the JVM starts; handle the status event first and read only on the other events.

On this page
  1. Why does a Flexy Java MQTT client fail to connect to AWS IoT?
  2. Why does the event log show MQTT-Not connected and MQTT-Problem configuring SSL/TLS?
  3. What must the cafile option match for the TLS handshake to succeed?
  4. Why does the Flexy go unreachable a few seconds after the JVM starts?
  5. How should callMqttEvent separate status events from MQTT messages?
  6. How do I verify the repair on the live AWS IoT broker?
  7. Questions asked

A Flexy fails its MQTT connection to AWS IoT for two independent reasons, and you can fix both in one editing session. First, the cafile option passed to Mqtt_c.setOption must name the certificate exactly as the file is stored on the gateway; when the option names a file the folder does not contain, the TLS setup fails before the client reaches the broker. Second, the old callMqttEvent sample reads a message on every callback, but the library also delivers status events, and processing one of those as a message takes the gateway offline within seconds of the JVM start. Rename the certificate so the configured path is real, then make the callback test the event number before it calls readMessage(). This article walks both faults in the order you meet them.

Why does a Flexy Java MQTT client fail to connect to AWS IoT? #

Two separate faults produce the failure: a cafile option that names a certificate file the gateway folder does not contain, which aborts the TLS setup, and an outdated callMqttEvent that reads a message during status callbacks, which makes the gateway unreachable seconds after the JVM starts.

The faults are independent and you meet them in sequence: leave the certificate fault in place and the callback fault stays hidden, because a client that never connects never receives status events. Fix the certificate first, then the callback, then verify on the live broker.

  • MQTT-Not connected not connected together with MQTT-Problem configuring SSL/TLS in the event log — the cafile option names a certificate file the on-board storage does not hold under that name. Correction: make the configured path and the stored filename identical, then restart the JVM.
  • Gateway reports itself unreachable within seconds of the JVM start — the sample callback reads a message on every callback, including status callbacks that carry no message. Correction: test the event number before calling readMessage().

The reported case ran Java ETK 1.4.4 against AWS IoT Core, and both corrections are edits to option strings and client code, so nothing about the toolkit installation changes.

Why does the event log show MQTT-Not connected and MQTT-Problem configuring SSL/TLS? #

The pair of log entries means the JVM client never finished configuring TLS: your code calls Mqtt_c.setOption("cafile", "/usr/root-CA.crt"), but the certificate stored in /usr/ is named CA.crt, so the option points at a file that does not exist and the client reports the SSL/TLS problem before contacting the broker.

AWS IoT Core requires TLS with server certificate validation, and cafile is where the Java MQTT client loads the CA certificate that performs that validation. When the string names a file that is not present, the client fails while building its TLS context, and the log records a configuration problem rather than a connection refusal.

Decision: rename the stored certificate to the name your option already uses — the change that cleared the fault in the reported case — or edit the option string to the stored name; both satisfy the same requirement, that the two strings agree exactly.

A matching name only helps while the file exists: when a reboot or power loss wipes the certificate from storage, the same log pair returns, and the article on MQTT connect failures after a power loss when the CA certificate is missing walks that variant. Keep the folder's contents private, too: a gateway backup that carries certificates and keys must never be attached to a public post or an unstripped support file.

The same name-versus-storage mismatch shows up on other clients that talk to AWS IoT; on the PLC side, the S7-1500 walkthrough for resolving TIA Portal MQTT certificate errors against AWS IoT TLS covers the equivalent fault.

What must the cafile option match for the TLS handshake to succeed? #

The string you pass for cafile must equal the stored file's folder and name exactly, byte for byte: with the certificate saved in /usr/ as CA.crt, either rename the file to root-CA.crt so the configured /usr/root-CA.crt becomes real, or edit the option to /usr/CA.crt.

Verify the match in one listing: open the folder the option points into, copy the certificate's exact name, and make the option string agree with it character for character, including case and the leading slash. The option is read when the client object builds its TLS context, so there is nothing to negotiate — the string either resolves or the configuration fault fires.

  1. List the files in /usr and note the certificate's exact name.
  2. Rename the file to match the option string, or edit the option string to match the file.
  3. Restart the JVM and read the event log before touching anything else.

Decision: if anything else on the gateway references the old filename — a second client, a script — edit the option instead; otherwise rename the file, because the configured path then stays stable across future edits.

Why does the Flexy go unreachable a few seconds after the JVM starts? #

A Flexy that drops offline seconds after the JVM starts has the old callback, not a network fault: that callMqttEvent calls readMessage() on every callback, but event 1 is a status event carrying no message, so the read is invalid and the failure takes the gateway offline.

The MQTT client library drives callMqttEvent for more than messages: connection-state changes arrive through the same callback as status events, and they start the moment the session comes up. That timing explains the collapse — the certificate fix lets the session establish, status events begin to fire, and the old callback's unconditional read runs into an event with no message attached. The first fault was masking the second.

A drop later in the session has other causes: a client that dies right after its first successful publish on firmware 14.0s01 is a version-specific fault, covered in the article on a Flexy MQTT client that drops after one publish. And if a gateway is already wedged before you can edit anything, recover it first — the Flexy 203 guide to troubleshooting a stuck JVM process covers getting the Java runtime back under control.

Decision: replace the callback body wholesale rather than patching it; the corrected version below is the whole fix, and a partial edit that still reads on some events leaves the fault in place.

How should callMqttEvent separate status events from MQTT messages? #

Test the event number first: when callMqttEvent receives event 1, treat it as a status event and return without reading, and call readMessage() only for every other event value, closing each non-null MqttMessage after you convert its payload.

Paste this over the whole old method in the class that implements your MQTT client:

public void callMqttEvent(int event) {
    // When a message is received through a subscribed topic,
    // the function callMqttEvent is called
    try {
        if (event == 1) {
            // Status event
        } else {
            MqttMessage msg;
            msg = readMessage();
            if (msg != null) {
                String Message = new String(msg.getPayload(), "UTF-8");
                Log.addLog("Incoming message : " + Message, Log.info);
                msg.close();
            }
        }
    } catch (Exception e) {
        Log.addLog("Error callMqttEvent: " + e.toString(), Log.error);
    }
}

Three details carry the fix. The event == 1 branch returns empty-handed, which is all a status event requires. readMessage() runs only in the other branch, and its result is null-checked before use. Exceptions land in the gateway's event log through Log.addLog with the Log.error level — on a headless gateway the old sample's System.out.println path went nowhere readable, which is why the fault stayed invisible. The two log prefixes are your probes afterwards: Incoming message : for received payloads and Error callMqttEvent: for callback faults.

How do I verify the repair on the live AWS IoT broker? #

Verify in this order: start the JVM, confirm the SSL/TLS configuration entry is gone from the event log, confirm the gateway stays reachable past the first minute, then watch your topic in the AWS IoT console's MQTT test client, for example the sdk/test/js topic from the AWS IoT walkthrough.

  1. Restart the JVM and read the event log: no new MQTT-Problem configuring SSL/TLS entry appears.
  2. Leave the gateway running past the first minute with the JVM up — that is the window in which the old callback used to take it offline.
  3. Subscribe to your publish topic in the MQTT test client and confirm each publish arrives.
  4. Check the application log for Incoming message : lines on subscribed traffic and for the absence of Error callMqttEvent: entries.

If publishes arrive but the session later tears down, both faults in this article are behind you and the cause sits elsewhere; the walkthrough on why a Flexy 205 disconnects from AWS IoT over MQTT takes you into that next set of causes.

Decision, case by case: for the MQTT-Not connected not connected pair with the SSL/TLS entry, make the cafile string and the stored filename identical and restart. For unreachability seconds after JVM start, install the event-aware callback and redeploy. Accept the repair only when the log is clean, the gateway stays reachable with the JVM running, and your test subscription receives what the client publishes.

Questions asked

Does the cafile mismatch also break a BASIC-script MQTT connection to AWS IoT?

No. The cafile option belongs to the Java ETK MQTT client, so a BASIC-script connection that follows the vendor's AWS IoT guidelines can connect and publish while the Java client still fails on the same gateway. That split is a reliable tell: if the BASIC path works, look at the Java option strings and the callback, not at the broker, the certificate chain or the network.

Which Java ETK version produced the faults described here?

The reported case ran Java ETK 1.4.4. Both fixes are edits to project option strings and client code, not to the toolkit installation, so apply them in your own MQTT client class and re-test on your exact toolkit build before trusting the result. The event-aware callback and a matching cafile path are what the corrected sample expects.

Can the cafile option keep its own path instead of renaming the certificate?

Yes. The handshake only needs the option string and the stored file to agree exactly. Rename the stored certificate to root-CA.crt when nothing else references the old name, or change the option string to the folder and name the file actually carries. Either edit clears the SSL/TLS configuration fault, because the mismatch itself is the fault.

What should the application log show after the corrected callback is running?

Two things and nothing else: an Incoming message line for each payload received on a subscribed topic, and no Error callMqttEvent entries. Status events produce no log line at all, because the corrected callback returns without reading. If the gateway still drops offline with this callback in place, stop blaming the callback and check the power supply, the network path and the state of the JVM process.

Why did the gateway become unreachable only after the certificate fix?

Because the first fault was hiding the second. Before the rename, the client never completed its connection, so no status events reached the callback and the faulty read path never executed. Once the certificate fix let the session come up, status events began to arrive, the unconditional read ran, and the gateway went offline, which is why the two failures show up in sequence rather than together.

Keep reading

  • Troubleshooting

    Why a Flexy Java MQTT client crashes or throws NoSuchMethodError

    A Flexy Java MQTT application that reboots the device is calling readMessage() on the connection-status event; return immediately when the event number is 1 and read messages only on 0. java.lang.NoSuchMethodError on IOManager.readTagAsFloat is a version boundary, not a missing library: the method needs ETK 1.4.4 and firmware 14.0 together.

    Edge computing and IIoT

  • Troubleshooting

    PLC ping works but AWS IoT Core MQTT fails when the clock is wrong

    Check the controller clock first. Ping and DNS prove IP reachability only; with the PLC clock set to 2007, TLS rejects every current AWS IoT Core certificate as not yet valid and the handshake dies before MQTT CONNECT, which is why the same project works in simulation on the PC's clock. Connect the clock battery, set the time, enable the NTP client against a plant time server, then retest DNS, port 8883, TLS, CONNACK and publish.

    Edge computing and IIoT

  • Troubleshooting

    Trace an edge gateway MQTT connection failure to Azure Event Grid

    A desktop MQTT client that connects and publishes with thumbprint authentication clears only that endpoint, certificate and permission combination on that host. When the gateway fails against the same Event Grid namespace, test four stages in order on the gateway itself: network path and DNS to TCP 8883, the TLS handshake with both certificate directions and the clock, the CONNECT identity, and publish authorization on the exact topic. The first stage that fails names the fix.

    Edge computing and IIoT

Why Telegraf MQTT consumer writes nothing to InfluxDB Cloud