knolleary / pubsubclient

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

Connection failed all the time, why ? #108

Open donnib opened 8 years ago

donnib commented 8 years ago

Hi, I have a sensor on ESP8266 and i use this library to post to CloudMQTT and i always get :

Attempting MQTT connection...failed, rc=-2 try again in 5 seconds Attempting MQTT connection...failed, rc=-2 try again in 5 seconds Attempting MQTT connection...connected

It never connects and sends, this is a major issue for me since it consumes power and i am running on batteries so the quicker and more robust this operation is the better, is there something i can do to minimize this ? I have tried different wifi networks to see if it solves problem but it has not.

Any ideas would be appreciated.

knolleary commented 8 years ago

Have you tried adding a small delay in before attempting the first connect? The return code of -2 indicates it is the tcp connection that is failing - which is reliant on the underlying ESP8266 client. So there may be a delay in it being ready to perform connections.

gadjet commented 8 years ago

Hi, Just trying your example and I've got the same problem, I added a half second delay before the client.setServer(mqtt_server, 10388);

But still getting the same -2 error.

I then added a 5 sec delay and now the error code returned is 5, I'll sees if I can google the error codes.

Anything else I can try?

gadjet commented 8 years ago

OK, I found out that the error code 5 was from cloudmqtt " not authorised".

I had setup a new user "ESP8266Client" in cloudmqtt with a blank password, I'm guessing this isn't the way to do it.

Anyone successfully connected to Cloumqtt ? any pointers

chaeplin commented 8 years ago

use

connect (clientID, username, password).

http://pubsubclient.knolleary.net/api.html#connect3 https://github.com/knolleary/pubsubclient/blob/master/examples/mqtt_auth/mqtt_auth.ino#L34

CloudMQTT is a cloud version of mosquitto.

gadjet commented 8 years ago

Thanks for the help, I'll try setting up a password.

chenirl commented 7 years ago

For those still having trouble:

All the example i tested were WRONG! Use: IPAddress mqttServer(xxx.xxx.xxx.xxx); Instead of: const PROGMEM char* mqttServer = "xxx.xxx.xxx.xx"; For your setServer: mqttClient.setServer(mqttServer, mqttPort);

mrohner commented 7 years ago

The IDE compiler does not like your suggested statement though: Garage_ESP_MQTT_for_Nano.ino:37: error: too many decimal points in number IPAddress mqtt_server(192.168.178.59);

chenirl commented 7 years ago

Replace the dot "." with a "," xxx,xxx,xxx,xxx IPAddress mqtt_server(192l,168,178,59);

gabrielklein commented 6 years ago

One of my issue was to create the ethClient in the constructor of my class. It doesn't work.

If I do it after having called the WiFiManager, it works perfectly.

Not a big issue, but you may spend a few hours on understand why it doesn't work depending on the way you build your application.

Something like ethClient = new WiFiClient(); client.setClient(*ethClient);

Don't forget the delete(ethClient);

rupaligubyadkar commented 6 years ago

I have a sensor on ESP8266 and i use this library to post to CloudMQTT and i always get :

Attempting MQTT connection...failed, rc=-2 try again in 5 seconds Attempting MQTT connection...failed, rc=-2 try again in 5 seconds Attempting MQTT connection...connected

It never connects and sends, this is a major issue for me since it consumes power and i am running on batteries so the quicker and more robust this operation is the better, is there something i can do to minimize this ? I have tried different wifi networks to see if it solves problem but it has not.

arissaraiva commented 6 years ago

same here anyone?

kahlkritz commented 6 years ago

I've got the exact same problem; Attempting MQTT connection...failed, rc=-2 try again in 5 seconds. I read here (https://ubidots.com/community/t/solved-mqtt-connection-fails/923) that it has to do with the network security. I get the error on three different WIFI networks, but not when I connect to my iPhone's personal hotspot. I still haven't been able to get it working on my home, uni or wireless router networks.

trentbrown13 commented 6 years ago

Another possible issue to look at is the quality of the 2.4ghz wifi channel you are using. At work our 2.4 bands are are pretty full and I get the above error messages quite often. The ESP can connect to the network, but traffic does not always get through. At home with a much cleaner wifi environment the same code on my esp's works flawlessly.

arissaraiva commented 6 years ago

I was having a problem with my router that was setting same IP for 2 devices, since I reset my router and fixed IP for all my davices it connected

SteveRMann commented 4 years ago

I know this is old, but I had the exact same problem as "kahlkritz". Turns out that my router is ancient and does not support WPA. As of ESP8266WiFi library Version 2.5.0, WPA is is the default mode. The solution is to get a better router, or add WiFi.enableInsecureWEP(); before calling WiFi.begin().

dfz20071113 commented 3 years ago

真的出问题了