IMD SolutionComputing LibraryShop main store

Troubleshooting

Python paho MQTT connection refused Errno 111 at the broker

Edge computing and IIoT Published 6 min read

Short answer

Errno 111 happens at the TCP connect stage, before any MQTT traffic: nothing is listening at the address and port you set, or a firewall rule rejects the connection. Confirm the broker listener, give the publisher scoped access to the MQTT port, and only then publish JSON and map it to tags with an MQTT Engine custom namespace.

On this page
  1. Read the error's position in the data path
  2. Pick the address the listener actually serves
  3. Open the port to the publisher, and only the publisher
  4. Work the checks in this order
  5. Example: separating a refusal from a dropped port by timing
  6. Turn the publish into tags
  7. The decision for each symptom
  8. Questions asked

socket.error: [Errno 111] Connection refused is a TCP failure, not an MQTT failure. Paho raises it during connect(), before any MQTT packet exists, so topic, payload and namespace settings cannot fix it. The refusal means the address and port your broker_address points at accepted nothing: either no broker listener is bound there, or a filtering rule rejected the attempt. The documented fix for this exact error was a firewall rule giving the publisher access to the broker's plain MQTT listener on 1883/tcp; once that rule was in place, the connect succeeded and the JSON payload could be mapped to tags through an MQTT Engine custom namespace. The sections below give the check order, a timing example that separates a refusal from a silent drop, and the Engine configuration that turns the publish into tags.

Read the error's position in the data path #

The connect stage produces three distinct outcomes, and each one has a different fix:

  • Immediate refusal (Errno 111). Nothing is listening at the destination address and port, or a rule actively rejected the attempt. Fix the listener bind address or the filtering rule.
  • Connect hangs, then times out. Packets are being silently dropped somewhere along the path: routing, or a firewall that drops instead of rejecting. Trace the path and the rules on each hop.
  • TCP opens but the broker rejects the session. Credentials, access control or the listener's security mode are at fault. That is broker configuration, not sockets.

Keep the investigations separate. When the port test passes but the MQTT handshake still fails, MQTT connect fails when TCP works: diagnose the handshake covers the CONNACK stage; a refusal never gets that far.

Pick the address the listener actually serves #

broker_address must be a hostname or IP at which a broker listener accepts connections. The Gateway web interface and the MQTT listener are separate services even on the same host: reaching the Gateway in a browser proves nothing about the MQTT socket. Read the listener's configured bind address and compare it with the address you pass to connect(). A broker bound to a single interface refuses connections addressed to any other address of that machine, including from a script running on the machine itself. The same-host case has its own traps; why a device cannot publish MQTT to a broker on your PC walks through them.

Open the port to the publisher, and only the publisher #

Confirm the broker service is running and a listener is actually configured — a module being installed is not a listener. Then add a filtering rule that allows the publisher's address to reach the MQTT listener. In the resolved case, setting the access rule for port 1883/tcp removed the refusal immediately, with no change to the client code. Scope the rule to the publisher and the listener port; do not disable host filtering globally or expose an unauthenticated plain-MQTT listener to the whole plant network, because that is a standing OT security exposure. Keep credentials and transport security aligned with the listener's mode: an open TCP socket says nothing about whether the broker will accept the MQTT session.

Work the checks in this order #

  1. Confirm the broker service is running and read the listener's bind address, port and security mode.
  2. List the listening sockets on the broker host and confirm the configured port is listening.
  3. From the publisher, run a TCP connect test against that address and port. Refusal means listener or firewall; timeout means routing or a drop rule.
  4. Add a firewall rule scoped to the publisher's address and the listener port, nothing wider.
  5. Run the paho client and capture the connect result together with the broker's own connection log.
  6. Only after a connect succeeds, publish a test topic and confirm it arrives — with a subscriber or in the broker log — before touching any Engine configuration.

Example: separating a refusal from a dropped port by timing #

A plain TCP port test tells you which of the two failure modes you have, if you give it an explicit timeout and count the wall-clock time. The script below uses only documented standard-library names; host, timeout_s and attempts are illustrative variables with their values declared.

import socket

host = "ip-of-the-broker-host"  # illustrative: your broker_address
port = 1883                     # the broker's plain MQTT listener port
timeout_s = 5                   # illustrative: seconds allowed per attempt
attempts = 3                    # illustrative: tries before declaring the path down

for _ in range(attempts):
    try:
        socket.create_connection((host, port), timeout=timeout_s)
        print("port open")
        break
    except ConnectionRefusedError:
        print("refused")        # answer came back; retrying will not change it
        break
    except TimeoutError:
        continue                # silence: retry, then declare the path down

Carry the numbers through. In the silent-drop case, every attempt burns the full timeout: 3 attempts × 5 s = 15 s of waiting, plus 2 × 2 s = 4 s of deliberate delay between the first two attempts, so the script takes 19 s to conclude that the path drops packets. In the refused case the rejection returns within one round trip: on a plant LAN with a round-trip time of about 1 ms, the failing attempt answers in roughly a millisecond and the script exits almost immediately. The decision follows from that timing: an answer inside a second or two means refusal, so fix the listener bind or the filtering rule; the full 19 s of silence means packets are being dropped, so fix routing or the drop rule instead.

Turn the publish into tags #

With the connect working, the remaining path is publisher → broker → MQTT Engine → tags. Add a custom namespace definition in the MQTT Engine configuration that matches your topic hierarchy; when the payload is JSON, Engine creates tags mirroring the object structure you publish to that topic. The mapping from payload fields to tag values is worked through in write tags on an edge gateway from an MQTT JSON payload. Confirm the publish itself first with a subscriber or the broker log: a successful publish() call returns before the broker has necessarily processed the message.

For a small fixed topic set with explicit status handling in your own code, a plain custom namespace is the lighter choice. Sparkplug exists to add a defined birth, death and timestamp lifecycle on top of MQTT, so you can trust that a value is fresh and the device is alive; the distinction between a broker that carries MQTT and a setup that provides that lifecycle is laid out in MQTT broker support is not Sparkplug support. If the created tags hold the first value and stop changing on later publishes, the MQTT Engine JSON payload not updating on second publish knowledge-base page covers that fault.

The decision for each symptom #

Symptom Stage that failed Fix
Immediate Errno 111 on connect TCP connect Listener bind address, or a scoped firewall rule for the MQTT listener port
Connect waits, then times out Network path Routing or a drop rule; the listener was never reached
TCP opens, broker rejects the session MQTT CONNACK Broker credentials, access control or listener security mode
Connect and publish succeed, no tag appears Engine mapping Custom namespace matching the topic hierarchy, and the JSON object structure

The decision for Errno 111 itself: verify the listener is bound at the address you connect to, then give the publisher scoped access to that listener port — that rule alone resolved the documented case — and treat any payload or namespace change as work for after the connect succeeds.

Questions asked

Does Errno 111 mean my MQTT username or password is wrong?

No. Credentials are checked after the TCP connection opens, at the MQTT CONNACK stage. Errno 111 is raised by the socket layer during connect, before any MQTT packet is sent, so it points at a missing listener or a firewall rejection. Wrong credentials produce a completed TCP connection followed by a not-authorized result instead.

The script runs on the same machine as the broker, so can the connection still be refused?

Yes. Same-host traffic still crosses the host's own filtering rules, and a broker bound to one interface will refuse connections addressed to another of the machine's addresses. Check the listening socket's bound address first, then the local firewall, before assuming the local path is safe.

Will MQTT Engine create tags from a plain JSON publish without Sparkplug?

Yes, once the topic is covered by a custom namespace definition in the MQTT Engine configuration. A JSON payload is mapped so the created tags mirror the object structure you publish. What you give up compared with Sparkplug is the defined birth, death and timestamp lifecycle, so freshness and device state become your application's responsibility.

Which port should the paho client connect to?

The port of the broker listener you actually configured, not the Gateway web port. A plain MQTT listener uses 1883/tcp by default and paho's connect method defaults to that port, but a secured or nonstandard listener differs, so read the listener configuration and pass the matching port explicitly.

Keep reading

  • Troubleshooting

    Fix a PLC MQTT client that never reaches your broker

    Most PLC MQTT failures sit in one of three stages: the broker listener, the network path to the port the client is actually configured for, or the client's own publish logic. Test in that order; a connect timeout points to the first two, a silent publish to the third.

    Edge computing and IIoT

  • Troubleshooting

    Fix Telegraf MQTT connecting to localhost in Docker

    Point the Telegraf MQTT consumer at the Compose service name tcp://hivemq:1883, not tcp://localhost:1883. Each container has its own network namespace, so localhost inside Telegraf is Telegraf itself and the connection to port 1883 never reaches the broker container. Service-name resolution on the Compose network is what restored sensor capture in a four-container stack.

    Edge computing and IIoT

  • How-to

    MQTT store and forward backfill after a connection outage

    Yes. Tag data collected on the edge during the outage fills the server gap after reconnection, but only when Store and Forward is enabled in MQTT Transmission on the edge and history is enabled on the receiving MQTT Engine tags. Matching historian database names on both systems does nothing.

    Edge computing and IIoT