knolleary / pubsubclient

A client library for the Arduino Ethernet Shield that provides support for MQTT.
http://pubsubclient.knolleary.net/
MIT License
3.82k stars 1.47k forks source link

Fail to connect , state -4 MQTT_CONNECTION_TIMEOUT #159

Open kbialkowski opened 8 years ago

kbialkowski commented 8 years ago

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 ?

altinokdarici commented 8 years ago

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

kbialkowski commented 8 years ago

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

fjnieto15 commented 8 years ago

@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.

jtonti commented 7 years ago

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.

TomCarlosCumming commented 6 years ago

Setting MQTT_VERSION to 3.1 fixed my error -4 also.

Thanks!

stritti commented 6 years ago

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

This should be improved:

krifi07 commented 6 years ago

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.

DCTECHS2015 commented 3 years ago

If you are using multiple "Things", you have to create different Certificates for each IoT Device... Also, when you connect the client, you have to change the name:

if (client.connect("CHANGE_THIS_IT_HAS_TO_BE_DIFFERENT_FOR_EACH_DEVICE"))

Note: Check this video for more reference about Certificates