Pubsubclient: Fail to connect , state -4 MQTT_CONNECTION_TIMEOUT

Created on 1 May 2016  路  7Comments  路  Source: knolleary/pubsubclient

I'm trying to run "Basic ESP8266 MQTT example" with nodemcu v3 and mosquitto broker set up on raspberry pi.
Unfortunately all the time I'm getting connection state -4
"Attempting MQTT connection...failed, rc=-4 try again in 5 seconds"

According to doc, its
MQTT_CONNECTION_TIMEOUT - the server didn't respond within the keep alive time

This is strange because I cant publish/subscribe using .net client with m2mqtt library.
On raspberry pi I can ping nodemcu module
Any ideas what should I check ?

Most helpful comment

When running MQTT broker mosquitto armhf 0.15-2 on Raspberry Pi wheezy,
to avoid timeout errors "MQTT_CONNECTION_TIMEOUT" -4 when connecting,
try setting the version in PubSubClient.h to the older version 3_1
// MQTT_VERSION : Pick the version
#define MQTT_VERSION MQTT_VERSION_3_1 _<<- uncomment this line_

Mosquitto 0.15-2 apparently does not support the newer version 3_1_1.
This got it working for me.

All 7 comments

I'm trying to connect Azure IoT Hub from ESP8266 via MQTT and I'm getting exactly same rc which is -4.

My proposal is something about SSL.
In addition,
I tried with cloudmqtt.com, it is working without ssl, but when I tried to connect with SSL port to cloudmqtt.com with exactly same credentials , I got rc -4,

I guess, the common point is SSL/TLS,

So following changes may help you,

add #include
and chage WiFiClient with WiFiClientSecure

Ok I've managed to connect to cloudmqtt without ssl. The same code fails when I try mosquitto broker.

@smashpl Exactly the same here. I'm trying to connect with NodeMCU v2 and v3 to mosquitto broker in a Raspberry Pi and I'm getting the same exact error.

When running MQTT broker mosquitto armhf 0.15-2 on Raspberry Pi wheezy,
to avoid timeout errors "MQTT_CONNECTION_TIMEOUT" -4 when connecting,
try setting the version in PubSubClient.h to the older version 3_1
// MQTT_VERSION : Pick the version
#define MQTT_VERSION MQTT_VERSION_3_1 _<<- uncomment this line_

Mosquitto 0.15-2 apparently does not support the newer version 3_1_1.
This got it working for me.

Setting MQTT_VERSION to 3.1 fixed my error -4 also.

Thanks!

I also searched for hours. thanks @jtonti ! Thought my firewall is the problem.

This should be improved:

  • get different error for version discrepancy
  • autoresolve protocol version by doing retry of call and store version for current session.

Hi anyone,
I'm very much afraid I'm having exactly the same issue (see my post above).
Even I'm fairly ok in doing some Linux via ssh, can anyone tell me the necessary steps to compile everything needed and install mosquitto once I changed the parameter?
Thanks.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mr2c12 picture mr2c12  路  9Comments

winterzh picture winterzh  路  10Comments

HobbytronicsPK picture HobbytronicsPK  路  4Comments

himanshu-hearthacker picture himanshu-hearthacker  路  8Comments

zvone141 picture zvone141  路  3Comments