IMD SolutionComputing LibraryShop main store

Troubleshooting

Why Azure IoT Hub MQTT connections fail on port 1883

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

Short answer

IoT Hub secures every device connection with TLS and keeps a single MQTT listener, the TLS one on port 8883, so a Flexy script set to the plaintext port 1883 fails inside connect before any subscribe or publish runs. Change the port to 8883 first, hold every other option, and let each logged retry expose the next fault.

On this page
  1. The stack names a stage, not two faults
  2. Why the plaintext port cannot work against IoT Hub
  3. Change the port first, one option per retry
  4. Working the case: the Flexy 205 script against IoT Hub
  5. What each later retry will expose
  6. Verify on the hub, not just the console
  7. Questions asked

The two exceptions are one failure, not two: the gateway never established the MQTT session. Azure IoT Hub requires TLS on every device connection and keeps a single MQTT listener, the TLS one on port 8883 — there is no plain-MQTT listener on port 1883 to reach. A Flexy script whose port option is set to 1883 therefore throws inside connect() before subscribe or publish ever runs, which is exactly what the two stack frames record. Change the port first and hold every other option for that first retry, because each remaining suspect — TLS version, CA file, device username, token, firewall path — leaves a different line in the log.

The stack names a stage, not two faults #

The trace carries two frames: com.ewon.ewonitf.MqttClient.startConnectionImpl(Native Method) and com.ewon.ewonitf.MqttClient.connect(Unknown Source). Read them as one event. connect is the public call the script makes; startConnectionImpl is the native routine it enters to open the session. One exception, two lines of its traceback — and because the whole run() body sits inside a single try/catch, execution never reached subscribe or the publish loop.

  • Evidence: both frames sit on the client's connect path and carry no broker error code. Meaning: the session was never established, so subscribe and publish behavior is untested, not broken.
  • A connect-stage failure that does carry a broker error number is a different diagnosis — the AWS IoT case is worked in why the Flexy AWS IoT script fails at MQTT CONNECT with error 28 — and these frames carry none.
  • The configuration fact in play: the script's port option points at the plaintext MQTT port while the target is IoT Hub.

Why the plaintext port cannot work against IoT Hub #

Microsoft's documentation is unconditional: all device communication with IoT Hub must be secured using TLS, and for that reason the hub does not support insecure MQTT connections over port 1883. No hub tier, setting or flag opens a plaintext listener — that port is not closed by default, it does not exist.

Two documented limits sit behind that door:

  • TLS version: IoT Hub no longer supports TLS 1.0 or 1.1, while every hub supports TLS 1.2. A client offering only the old versions fails at the handshake, after its TCP connection has already succeeded.
  • Network path: the same protocol reference warns that these ports cause problems on closed networks. A plant firewall rule scoped to the plaintext port must be re-pointed at the TLS port before any script change can show up in the log.

Change the port first, one option per retry #

  1. Edit only the port option, from the plaintext port to the TLS port, 8883. Everything else stays byte-identical for this attempt.
  2. Leave the log option at 1 so the retry prints the client's own diagnostic lines.
  3. Confirm the outbound path from the Flexy's subnet to the hub's host on the TLS port; an allow rule written for the old port covers none of the new traffic.
  4. Re-run and read the output: the port fault is cleared only when connect() returns without an exception. Anything that fails after that point is the next fault, not this one.

The edit is one line inside the script's option block:

MyMqttClient Mqtt_c = new MyMqttClient("my IotHub url", "my DeviceID");
Mqtt_c.setOption("port", "8883");
Mqtt_c.setOption("log", "1");
Mqtt_c.connect();

MyMqttClient is the script's own class, built on the Ewon client the traceback names, and the constructor's two strings are the hub's MQTT host name and the device ID — keep both exactly as your script already has them.

Working the case: the Flexy 205 script against IoT Hub #

Inputs, each with its source:

  • Port option in the script: 1883 (from the script as posted).
  • The hub's only MQTT listener: 8883, TLS required (Microsoft's IoT Hub protocol reference).
  • Log option: 1 (from the script).
  • Keepalive option: 6000 (from the script; held unchanged).
  • TLS version option TLSVERSION: tlsv1 (from the script; held for the first retry).
  • CA file option cafile: /usr/BaltimoreCyberTrustRoot.pem (from the script; held).
  • Username: the hub host name and device ID carrying api-version=2016-11-14 (from the script; held).
  • Password: the script's SASToken string variable (from the script; held).
  • Publish pause: Thread.sleep(1000) between messages (from the script).
  • TLS floor: 1.2, with 1.0 and 1.1 retired (Microsoft's TLS support notice).

The rule, in words: the port option must equal the hub's one listener before any TLS, credential or topic check can begin; connect() must return before subscribe or publish can execute; and each retry changes one option so the log attributes each remaining fault to exactly one cause.

The arithmetic: the script makes seven setOption calls — port, log, keepalive, TLSVERSION, cafile, Username, Password — so this step changes one and holds the rest, 7 − 1 = 6 held options. Verification rate once connect() returns: the loop sleeps 1000 ms between publishes, so each cycle is 1 s and 60 s ÷ 1 s = 60 publish attempts each minute, about sixty MQTT : value published lines on the console.

The result, in the expected order: run 1, with only the port changed, gets TCP through to the hub's TLS listener and — if the client still offers only the retired TLS versions — fails at the handshake. Run 2 moves the TLS version option up to the hub's floor. Run 3 should see connect() return and the console print steadily, and the next refusal, when it comes, will be the hub rejecting the free-form subscribe topic, not the connection.

The decision: stop editing options the moment connect() returns and the console prints once each second; from there every change must be forced by a logged line. If a retry still dies inside connect() with the current TLS version in place, the fault is the network path or the credentials — test outbound reachability on the TLS port from the Flexy's own subnet before touching the username or the token.

What each later retry will expose #

  • TLS version: move TLSVERSION to the value that selects TLS 1.2; the exact value string is listed in the Java client's option documentation for the installed firmware, and the configured tlsv1 is an old-generation setting the hub no longer accepts.
  • A wrong clock fails the same handshake, because certificate validation reads the gateway's time before any credential is examined — the mechanism is shown in why ping works but AWS IoT Core MQTT fails when the clock is wrong.
  • CA file: keep /usr/BaltimoreCyberTrustRoot.pem unless the log names it; sourcing and loading a CA file on this Java client is worked in fixing the CA file and the event callback on the Flexy Java client.
  • Credentials: the username stays device-scoped — hub host name, device ID and the api-version query, formatted exactly as Microsoft's MQTT page prints them — and an expired token, or one scoped to the wrong device, is refused after the TLS session is up, at authentication.
  • Subscribe topic: IoT Hub is not a general broker. It grants only its documented device-facing subscriptions, so the free-form test topic in the script is refused once the session stands; on a general broker that same subscribe would simply succeed, a difference worked in how MQTT clients publish and subscribe through the broker. Replace it with the hub's cloud-to-device topic for this device, as its MQTT page documents it.

Verify on the hub, not just the console #

MQTT : value published proves the statement after publish() ran on the Flexy — nothing more. With the publish call inside an endless loop and a catch that only prints, the console line says nothing about cloud-side receipt. Confirm delivery where it counts: watch the hub's telemetry for the device, through its built-in metrics or a telemetry reader you already run, and only then call the link done. If the hub shows nothing while the console prints steadily, compare the publish topic string, character for character, against the hub's device-events topic.

Questions asked

Can I enable plain MQTT on IoT Hub for devices on an isolated plant network?

No. The TLS requirement belongs to the service, not to a hub setting: no tier, flag or firewall arrangement opens a plaintext MQTT listener. If a device genuinely cannot perform TLS, terminate its plain MQTT on a broker or gateway you control inside the plant network, then bridge that link to the hub over TLS from a client that can.

Does IoT Hub Device Provisioning Service make an exception for provisioning clients?

No. The provisioning service documents the same rule for MQTT v3.1.1: every device connection must be secured with TLS, and its MQTT listener is the TLS port only. A provisioning client pointed at the plaintext port fails the same way, inside its connect call, before any registration operation is attempted.

Would switching the gateway to AMQP avoid the TLS work?

No. IoT Hub secures all device communication whatever the protocol, and its AMQP endpoint is TLS-only on port 5671. Changing protocol swaps the client library and the port number, not the security requirement, so the same port-then-TLS diagnosis applies.

How do I tell a blocked firewall from a refused connection when connect still fails?

Test from the Flexy's own subnet, not from an office PC, by opening a TCP connection to the hub's host on the TLS port. An immediate reset means something on the path actively refused the flow; a long silence ending in a timeout means packets are being dropped, and the outbound firewall rule must be re-pointed before any script change can show up in the log.

Keep reading

  • Explainer

    Java ETK MQTT connection status and offline buffering

    A Flexy Java program can publish MQTT to Azure IoT Hub only over TLS, and one working session proves nothing about device-to-cloud, cloud-to-device or file upload. Offline buffering stands or falls on the ETK's MQTT connection-status call: absent before the firmware 14.0 plan, so verify the installed build and ETK before designing store-on-disconnect.

    Edge computing and IIoT

  • Explainer

    Why your gateway talks MQTT to Azure but not AMQP

    MQTT works because Azure IoT Hub natively speaks MQTT v3.1.1 over TLS and the gateway's Java 1.4 runtime can carry that exchange. AMQP does not, because Event Hubs' client-side ingress is AMQP 1.0 on port 5671 and no supported AMQP client runs on Java 1.4. Keep the gateway on MQTT to IoT Hub and route to Event Hubs from there, or bridge with a separate box.

    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

Canary historian to Azure Blob Storage: the file path