Print client.state() after a failed connect. The number tells you where it failed.
What the message means
The state is -4 for a timeout, -3 for a lost connection, -2 when the network connection failed, -1 when disconnected, and 1 to 5 for refusals by the broker.
Fix it in this order
- State -2: check the broker address and port, and that the board is on the network. Port 1883 is the usual plain port.
- State -4: check the network and any firewall between you and the broker.
- State 2: the client ID was rejected. Use a unique ID for each device.
- State 4 or 5: check the user name and password, and the access rules on the broker.
- Call client.loop() often, and reconnect when the state is not 0.
- Raise the buffer size with setBufferSize if large messages fail to publish.
Why it happens
PubSubClient keeps the result of the last connect attempt and returns it from state(). In the library’s header file, as I read it, -4 is a connection timeout, -2 is a failed connection and 1 to 5 are the refusal codes from the broker: bad protocol, bad client ID, unavailable, bad credentials and unauthorised. Negative numbers come from the network side and positive numbers come from the broker, which tells you where to look first.
Large messages fail for a different reason. The header sets a default maximum packet size of 256 bytes in the version I checked, and the packet includes the topic and a small header as well as your payload. A publish that is too big does not work unless you raise the buffer with setBufferSize. Check the header of the version you installed.
If you see this, do this
| You see | Do this |
|---|---|
| -2 on every try | Address, port or network. |
| 4 or 5 | Credentials or permissions. |
Related
MQTT on the ESP32, Wi-Fi keeps disconnecting.
Last reviewed: November 2026. These are the usual causes and the order that finds them fastest. Test on your own board; clones and cables differ.
Why this happens
PubSubClient stores the result of the last connection attempt and returns it through client.state(). The values split into two groups. Negative values come from the network side: the TCP connection could not be made, timed out or dropped. Positive values from 1 to 5 are replies from the broker that refused the connection, each with a different reason, so the number tells you which side to examine.
Publishing has its own limit. The library has an internal buffer, and a message is sent only if the topic, a small header and the payload all fit in it. The buffer is 256 bytes in the versions most examples use. A message that is too long returns false from publish() with no other warning.
How to confirm the cause
- Print client.state() right after a failed connect, using the table of codes below, and act on the group it falls in.
- From a PC on the same network run a command-line client against the same broker address and port. If that works, the broker is fine and the problem is on the board.
- Check the board is connected to Wi-Fi when you call connect(); print WiFi.status() first.
- Use a distinct client ID for each device. Two devices with the same ID kick each other off in a loop.
- For failed publishes, print the length of the topic and the payload and add them up.
Worked example
State codes, and a check on the largest payload for a 27-character topic with the default 256-byte buffer:
// -4 timeout -3 lost -2 connect failed -1 disconnected 0 connected
// 1 bad protocol 2 bad client ID 3 unavailable
// 4 bad credentials 5 unauthorised
if (!client.connect("esp32-livingroom-01")) {
Serial.println(client.state());
}
// "home/livingroom/temperature" is 27 characters
// space left = 256 - 5 (header) - 2 (length) - 27 = 222 bytes
client.setBufferSize(512); // if payloads are largerCommon mistakes
- Calling connect() once and never again after a drop.
- Not calling client.loop() often enough, so the keep-alive times out.
- Using localhost or a name that the board cannot resolve as the broker address.
- Using the same client ID on two boards.
- Blocking for a long time in loop(), so the connection is closed by the broker.
If this did not fix it
- A broker that listens only on its own machine and not on the network address you use.
- A firewall rule on the broker’s computer that blocks port 1883.
- The broker requires encrypted connections and a different port, which a plain client cannot use.