pubsubclient icon indicating copy to clipboard operation
pubsubclient copied to clipboard

strange issue Attempting MQTT connection…failed, rc=-2 try again in 5 seconds

Open smarta1980 opened this issue 5 years ago • 35 comments

Hi, Guys I have a very strange issue when add some lines in the void setup then then get this message Attempting MQTT connection…failed, rc=-2 try again in 5 seconds

int R1 = D0; int R2 = D1; int R3 = D2; int R4 = D3; int R5 = D4; int R6 = D5; int R7 = D6;

pinMode(R1, OUTPUT); // sets the digital pin D0 as output pinMode(R2, OUTPUT); // sets the digital pin D1 as output pinMode(R3, OUTPUT); // sets the digital pin D2 as output pinMode(R4, OUTPUT); // sets the digital pin D3 as output pinMode(R5, OUTPUT); // sets the digital pin D4 as output pinMode(R6, OUTPUT); // sets the digital pin D5 as output pinMode(R7, OUTPUT); // sets the digital pin D6 as output

digitalWrite(R1, HIGH); digitalWrite(R2, HIGH); digitalWrite(R3, HIGH); digitalWrite(R4, HIGH); digitalWrite(R5 , HIGH); digitalWrite(R6 , HIGH); digitalWrite(R7 , HIGH);

then if I delete this line digitalWrite(R7 , HIGH); then esp8266 connected to mqtt server

could you please tell me what is the problem?

smarta1980 avatar Apr 15 '19 09:04 smarta1980

No idea - sorry. My guess would be that toggling D6 is causing some interference with the network hardware. A quick google doesn't reveal anything. You may want to ask on a more ESP-focussed forum - the PubSubClient uses whatever network client you give it. This issue is at a lower level then anything the PubSubClient has to handle.

knolleary avatar Apr 15 '19 10:04 knolleary

dear, I tried D6 and D7 and D8 same result Attempting MQTT connection…failed, rc=-2 try again in 5 seconds if I delete digitalWrite(R7 , HIGH); then then esp8266 connected to mqtt server

this is the code at callback

void callback(char* topic, byte* payload, unsigned int length) {

if (strcmp(topic,string1) ==0){ // whatever you want for this topic Serial.print("Message arrived ["); Serial.print(topic); Serial.print("] "); stringTwo = ""; for (int i = 0; i < length; i++) { Serial.print((char)payload[i]); stringTwo.concat((char)payload[i]); } Serial.println();

if (stringTwo == "connect"){ //--------------------------------------------- if (out1_state == 1){ val = "out1 on"; client.publish(sender, val.c_str(),0); }

if (out1_state == 0){ val = "out1 off"; client.publish(sender, val.c_str(),0); } //---------------------------------------------

if (out2_state == 1){ val = "out2 on"; client.publish(sender, val.c_str(),0); }

if (out2_state == 0){ val = "out2 off"; client.publish(sender, val.c_str(),0); } //---------------------------------------------

if (out3_state == 1){ val = "out3 on"; client.publish(sender, val.c_str(),0); }

if (out3_state == 0){ val = "out3 off"; client.publish(sender, val.c_str(),0); } //---------------------------------------------

if (out4_state == 1){
val = "out4 on";
client.publish(sender, val.c_str(),0);

}

if (out4_state == 0){
val = "out4 off";
client.publish(sender, val.c_str(),0);

}

if (out5_state == 1){
val = "out5 on";
client.publish(sender, val.c_str(),0);

}

if (out5_state == 0){
val = "out5 off";
client.publish(sender, val.c_str(),0);

}

if (out6_state == 1){
val = "out6 on";
client.publish(sender, val.c_str(),0);

}

if (out6_state == 0){
val = "out6 off";
client.publish(sender, val.c_str(),0);

} //--------------------------------------------- }

if (stringTwo == "out1 on"){
digitalWrite(R1, LOW);
out1_state = 1;
val = "out1 on";
client.publish(sender, val.c_str(),0);

}

if (stringTwo == "out1 off"){
digitalWrite(R1, HIGH);
out1_state = 0;
val = "out1 off";
client.publish(sender, val.c_str(),0);

}

if (stringTwo == "out2 on"){
digitalWrite(R2, LOW);
out2_state = 1;
val = "out2 on";
client.publish(sender, val.c_str(),0);

}

if (stringTwo == "out2 off"){
digitalWrite(R2, HIGH);
out2_state = 0;
val = "out2 off";
client.publish(sender, val.c_str(),0);

}

if (stringTwo == "out3 on"){ digitalWrite(R3, LOW); out3_state = 1; val = "out3 on"; client.publish(sender, val.c_str(),0); }

if (stringTwo == "out3 off"){
digitalWrite(R3, HIGH);
out3_state = 0;
val = "out3 off";
client.publish(sender, val.c_str(),0);

}

if (stringTwo == "out4 on"){
digitalWrite(R4, LOW);
out4_state = 1;
val = "out4 on";
client.publish(sender, val.c_str(),0);

}

if (stringTwo == "out4 off"){ digitalWrite(R4, HIGH); out4_state = 0; val = "out4 off"; client.publish(sender, val.c_str(),0); }

if (stringTwo == "out5 on"){
digitalWrite(R5, LOW);
out5_state = 1;
val = "out5 on";
client.publish(sender, val.c_str(),0);

}

 if (stringTwo == "out5 off"){
digitalWrite(R5, HIGH);
out5_state = 0;
val = "out5 off";
client.publish(sender, val.c_str(),0);

}

  if (stringTwo == "out6 on"){
digitalWrite(R6, LOW);
out6_state = 1;
val = "out6 on";
client.publish(sender, val.c_str(),0);

}

if (stringTwo == "out6 off"){
digitalWrite(R6, HIGH);
out6_state = 0;
val = "out6 off";
client.publish(sender, val.c_str(),0);

} }

smarta1980 avatar Apr 15 '19 10:04 smarta1980

Hello, i've got very similar issue but with D11. Have you found any link for solution?

regards, Ondranen

ondranen avatar May 14 '19 21:05 ondranen

Hello,

I can get the same "periodic reconnection" behavior with a simplified version of one of the git examples. And without changing pins, no hardware involved.

Console Out:

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

Client:

ESP8266 (Wemos d1 mini) Arduino 1.8.9 PubSubClient 2.7.0

Server:

mosquitto version 1.6.1 debian 8.11

The sketch is a simplified version of the git example

/*
 * RCA 17/02/2019  Basado en: Basic ESP8266 MQTT example
 * 
 */

#include <ESP8266WiFi.h>
#include <PubSubClient.h>

// Constants (erased for privacy) 

WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;

void setup_wifi() {
  delay(10);
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  randomSeed(micros());
  Serial.println("");
  Serial.println("WiFi connected");
}

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  Serial.println();
}

void reconnect() {
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    String clientId = "ESP8266Client-";
    clientId += String(random(0xffff), HEX);
    if (client.connect(clientId.c_str(), mqtt_usr, mqtt_pass, mqtt_will, 1, 1, "0" )) {
      Serial.println("connected");
      // ...  resubscribe
      client.subscribe("test/#");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
  client.publish(mqtt_will, "1", true);
}

void setup() {
  pinMode(BUILTIN_LED, OUTPUT);     // Initialize the BUILTIN_LED pin as an output
  Serial.begin(115200);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
}

void loop() {

  if (!client.connected()) {
    reconnect();
  }
  client.loop();
  long now = millis();
  if (now - lastMsg > 5000) {
    lastMsg = now;
    ++value;
    Serial.print(".");
  }
}

Did someone find a solution? Maybe I've understood something wrong and I'm not using the library in the right way. If so, I would appreciate any indication

Regards,

Ric

rcan2000 avatar May 23 '19 18:05 rcan2000

Maybe it`s power issue - try another port, cable, computer (or powerbank or phone charger).

saloid avatar Jul 20 '19 02:07 saloid

Thanks for your suggestion Dmytro. At this time it works better, only a few reconnections per day. The main problem was a strange configuration in the router's DHCP service.

Regards, Ric

rcan2000 avatar Jul 24 '19 21:07 rcan2000

Thanks for your suggestion Dmytro. At this time it works better, only a few reconnections per day. The main problem was a strange configuration in the router's DHCP service.

Regards, Ric

What strange configuration?

sjardi avatar Sep 24 '19 16:09 sjardi

I'm late to this party and have had the same issues. I tracked mine down to a Tp-link router configured with short (1 min) DHCP lease time. The continual lease expiry drags down the MQTT connection.

thamesmud avatar Oct 02 '19 14:10 thamesmud

Thanks for your suggestion Dmytro. At this time it works better, only a few reconnections per day. The main problem was a strange configuration in the router's DHCP service. Regards, Ric

What strange configuration?

Sorry for the late reply, The problem was the same as thamesmud says, my router unexpectedly had a DHCP lease time set to 1 minute

rcan2000 avatar Oct 14 '19 21:10 rcan2000

Hi, I have had the same problem. I tried everything, but no success. Then, when I install back over Board Manager core 2.4.2, problem with mqtt connection was solved. When I used ssl to mosquitto server with core 2.5.2 it was no success. Conenection without SSL was ok with core 2.5.2. Conclusion: SSL connection to mosquitto server with arduino core 2.5.0 didn't success. Last good core to SSL connection to mosquitto was with Arduino core is 2.4.2.

mirecpda avatar Oct 31 '19 19:10 mirecpda

I found this thread because I was having similar issues. I was using this DHCP/DNS windows software https://sourceforge.net/projects/dhcp-dns-server/ but switched to dnsmasq running on raspberry pi. That's when my problems started. Now I get this message on the serial port: Attempting MQTT connection...failed, rc=-2 try again in 5 seconds and it never manages to connect.

The Wemos D1 Minis connect to my wireless access point just fine, dnsmasq assigns them fixed IPs based on their mac addresses and I can ping them without any problems. But, they just seems incapable of connecting to the MQTT broker which worked fine before. After reading this thread I thought maybe dnsmasq was somehow assigning very short leases but I have tried overriding the lease time with 48h and "infinite" and it although the lease times get changed, it doesn't fix the issue.

In my case, it does appear that changing my dhcp/dns server caused the problem but switching back is sadly not an option as the windows server suffered a hardware failure (the reason to switch to dnsmasq on the Pi). Other than this MQTT issue, the rest of my network is working fine with dnsmasq, I have 70+ networked devices, 30 of which connect via wifi and 10 of these are wemos minis. The other wifi devices work fine (phones, tablets, laptops, Amazon Echos, TP-Link HS100/HS110s)

I'll continue testing and report back.

dissemin8 avatar Nov 02 '19 12:11 dissemin8

I fixed my issue although I suspect my problem was unrelated to the problems others have reported in this thread.
My issue seems to have been that MQTT couldn't make sense of the CNAME I had set up for my MQTT server. The servers real host name is HS14 but it has 2 aliases: MQTT and RED as I run both node red and mqtt on this server. My wemos minis try to connect to a server named "mqtt" but this was failing. I fixed the issue by adding the highlighted line to my dnsmasq config: . . dhcp-host=DC:A6:32:xx:xx:xx,hs14,192.168.0.14 cname=red.domain-name-here.co.uk,hs14 cname=mqtt.domain-name-here.co.uk,hs14 cname=mqtt,hs14 . . Maybe mosquitto couldn't understand that
mqtt.domain-name-here.co.uk is the same as mqtt

Strangely, Windows and Linux didn't have any issues pinging "mqtt" even before I added cname=mqtt,hs14 they just seem to understand that they all live in the same domain.

Just a thought but now I can see why I was getting the Attempting MQTT connection...failed, rc=-2 it would make sense that the same error would be issued if the MQTT server had changed IP address or was otherwise uncontactable. Anyone getting that message should probably check that their MQTT server has a fixed IP and/or hostname name that can be resolved to the correct IP.

Sorry I couldn't help with the OP's issue.

dissemin8 avatar Nov 02 '19 14:11 dissemin8

jkjk

berguinsson avatar Nov 24 '19 20:11 berguinsson

Hi, I have had the same problem. I tried everything, but no success. Then, when I install back over Board Manager core 2.4.2, problem with mqtt connection was solved. When I used ssl to mosquitto server with core 2.5.2 it was no success. Conenection without SSL was ok with core 2.5.2. Conclusion: SSL connection to mosquitto server with arduino core 2.5.0 didn't success. Last good core to SSL connection to mosquitto was with Arduino core is 2.4.2.

I also installed back over Board Manager core 2.4.2, and it started working again! Thanks for your hint!!

danielcugler avatar Dec 07 '19 19:12 danielcugler

This was my issue and my solution: I have a mqtt server and a client in a Raspberry Pi and another client in a ESP8266. I had one or two reconnections per week, until I changed my router. I had the described issue, with 3 or 4 reconnections every 10 minutes, getting rc=-2 during attemps.

I solved it checking free channels in the surrounding wifis and changing the channel of my wifi to a free channel. A lot of routers have an "Auto" to find free channels as well.

Hope this helps :)

manuelfgm avatar Feb 19 '20 22:02 manuelfgm

I encouter this problem too. I want to briefly explain what got wrong. Firstly, I tried this library and it worked perfectly. Although, I worked with ThingSpeak.h module github/thingspeak-arduino where it worked. Thus I tested again the MQTT, implementing this module and I get this error. Any ideas will be valuable.

SakisHous avatar Mar 10 '20 11:03 SakisHous

I had the same issues and it was driving me mad because I had it working and after delivering a few working devices (and of course updating my code) it stopped working. I finally figured it out. I changed the mqttServer from a DNS name to an IP. This is what broke it. I just changed it back to a server name that can resolve on DNS without changing anything else and the error disappeared.

Pi-And-More avatar Mar 20 '20 21:03 Pi-And-More

Since a few days I have the same problems with a esp32 ttgo epaper board. I use the deep sleep mode. Therefore the board wents to sleep for 30 seconds, wakes up and tries to reconnect to the MQTT-server. That works fine for the probably first 5 times and then stucks. In the meantime I set the deep sleep time to 120 seconds and now it works fine.

BeatArnet avatar Mar 25 '20 12:03 BeatArnet

Hi,

in my case it was the usesage of int ldrPin = A0;

when i uncomment these my mqtt connection stays up.

is there a solution for these on esp8266 (Wemos d1)?

regards maik

maik-dmxc avatar Mar 29 '20 07:03 maik-dmxc

Hi,

in my case it was the usesage of int ldrPin = A0;

when i uncomment these my mqtt connection stays up.

is there a solution for these on esp8266 (Wemos d1)?

regards maik

found another solution for my code only show a part off loop! original:

void loop() {

digitalWrite(statusLed,!digitalRead(pirPin));

fbright = (analogRead(ldrPin));  
//Serial.println(fbright);  

  now = millis();
    if (now - lastMsg > send_timer) {
    mqtt_send();
  
    }

  if (!client.connected()) {
    Serial.print(" : ");
    Serial.print("mqtt_reconnect");
    reconnect();
  }
  client.loop();

changed to:

void loop() {



  if (!client.connected()) {
    Serial.print(" : ");
    Serial.print("mqtt_reconnect");
    reconnect();
  }
  client.loop();

  digitalWrite(statusLed,!digitalRead(pirPin));

fbright = (analogRead(ldrPin));  
Serial.println(fbright);  

  now = millis();
    if (now - lastMsg > send_timer) {
    mqtt_send();
  
    }

when i put

 if (!client.connected()) {
    Serial.print(" : ");
    Serial.print("mqtt_reconnect");
    reconnect();
  }
  client.loop();

in front off all the rest of the code, it works!?

found this solution in
#185

regards maik

maik-dmxc avatar Mar 29 '20 07:03 maik-dmxc

more

when i comment the line out, the mqtt errors comes back Serial.println(fbright);

that could be a timing problem, because when add the a delay between

if (!client.connected()) {
    Serial.print(" : ");
    Serial.print("mqtt_reconnect");
    reconnect();
  }
  client.loop();

delay(100);

now = millis();
    if (now - lastMsg > send_timer) {
    mqtt_send();
  
    }

the connection stays good so i think, if i send to early after client.connect the connection crashes?

regards maik

maik-dmxc avatar Mar 29 '20 07:03 maik-dmxc

I got the same error and the problem was in the certificate string was missing letter.

waged avatar Jun 08 '20 12:06 waged

If you use Platformio, try to change platform version platform = [email protected] Recompile and upload

easterism avatar Jun 10 '20 07:06 easterism

Use this solution https://github.com/knolleary/pubsubclient/issues/138#issuecomment-326113915

easterism avatar Jun 10 '20 07:06 easterism

i get the same problem too.. but now i can solved it. i get the problem when i used mega 2560 with ethernet shield. I am trying to test example PubSubClient library, mqtt_basic. I setting the ip server same with the ethernet and in the program

image

and this i configure in my program byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED }; byte ip[] = {192, 168, 0, 200}; byte server[] = {192, 168, 0, 180};

for the first time i get the serial monitor : 09:19:43.509 -> Attempting MQTT connection...failed, rc=-2 try again in 5 seconds 09:19:48.519 -> Attempting MQTT connection...failed, rc=-2 try again in 5 seconds 09:19:53.495 -> Attempting MQTT connection...failed, rc=-2 try again in 5 seconds

and i can solved it, when i turn on and turn off again my windown defender domain network and public network

and i get the server is connected

Namirafathani avatar Jun 29 '20 02:06 Namirafathani

I've faced the same problem. Then I checked using another wifi, the nodemcu v2 was connected to the MQTT broker successfully. Then I had checked the wifi router , here I found that the IP which was assigned to the nodemcu v2 by wifi router, was bound to ARP Binding ( Address Resolution Protocol). I deleted the ARP Binding, and my nodemcu v2 had been started to work perfectly, it was connected to the MQTT broker.

mnhpias avatar Sep 07 '20 22:09 mnhpias

Hi,

very similar issue. I added millis() to loop and then I was getting Attempting MQTT connection...failed, rc=-2 try again in 5 seconds ... the issue disapered after adding of delay(10) after millis()

svatos-jirka avatar Dec 25 '20 22:12 svatos-jirka

I found this video as a problem solver, This may help you. All MQTT Errors explained very well and Suggester Different trouble shooting techniques https://youtu.be/CbodTTk-D18

VeeruSubbuAmi avatar Jun 21 '21 04:06 VeeruSubbuAmi

Thanks for the useful link! Kind regardsBeat-------- Ursprüngliche Nachricht --------Von: Veera Subrahmanyam Mediboina @.>Datum: Mo., 21. Juni 2021, 06:51An: knolleary/pubsubclient @.>Cc: Beat Arnet @.>, Comment @.>Betreff: Re: [knolleary/pubsubclient] strange issue Attempting MQTT connection…failed, rc=-2 try again in 5 seconds (#604) I found this video as a problem solver, This may help you. All MQTT Errors explained very well and Suggester Different trouble shooting techniques https://youtu.be/CbodTTk-D18

—You are receiving this because you commented.Reply to this email directly, view it on GitHub, or unsubscribe.

BeatArnet avatar Jun 21 '21 06:06 BeatArnet

i had same problem , i spend two days for "connection…failed, rc=-2 " error . But today i find solution . the error doesnt mean connection error always .

i read server ip adres on SD card as char[] and set "client.setServer" as Parameter , but it is wrong , the funcstion request "IPAddress" object , and byte port . when i set values like this my problem solved .

IPAddress server(mqttAdres[0],mqttAdres[1],mqttAdres[2],mqttAdres[3]);

client.setServer(server, 1883);

yilmazt81 avatar Oct 18 '21 08:10 yilmazt81