Open smarta1980 opened 5 years ago
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.
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);
} }
Hello, i've got very similar issue but with D11. Have you found any link for solution?
regards, 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.
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
.........
ESP8266 (Wemos d1 mini) Arduino 1.8.9 PubSubClient 2.7.0
mosquitto version 1.6.1 debian 8.11
/*
* 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
Maybe it`s power issue - try another port, cable, computer (or powerbank or phone charger).
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
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?
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.
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
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 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.
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.
jkjk
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!!
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 :)
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.
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.
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.
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
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
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
I got the same error and the problem was in the certificate string was missing letter.
If you use Platformio, try to change platform version
platform = espressif8266@2.0.4
Recompile and upload
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
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
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.
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()
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
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.
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);
Hey, hopefully this helps someone. There was an update to Mosquitto recently, and it will block all connections (connection refused) that are not localhost. That's why it may work via RPI itself where the Broker is hosted, but not from the NodeMCU or arduino or whatever is connecting via an IP address or Domain name. It's not local host. You will need to add these two lines to \etc\mosquitto\mosquitto.conf
then restart mosquitto service.
sudo systemctl restart mosquitto
Also make sure you test with your WIFI IP as well as the Ethernet connected IP. The mosquitto broker binds to one of them, and I can't figure out how to print that once it's running. An IP address must be formatted as the following: IPAddress mqtt_server(192, 168, 1, 213);//MQTT server IP
Hey, hopefully this helps someone. There was an update to Mosquitto recently, and it will block all connections (connection refused) that are not localhost. That's why it may work via RPI itself where the Broker is hosted, but not from the NodeMCU or arduino or whatever is connecting via an IP address or Domain name. It's not local host. You will need to add these two lines to \etc\mosquitto\mosquitto.conf
- listener 1883
- allow_anonymous true
then restart mosquitto service.
sudo systemctl restart mosquitto
Also make sure you test with your WIFI IP as well as the Ethernet connected IP. The mosquitto broker binds to one of them, and I can't figure out how to print that once it's running. An IP address must be formatted as the following: IPAddress mqtt_server(192, 168, 1, 213);//MQTT server IP
Winner winner chicken dinner! Thank you CarterMoody, that solved the problem for me.
Thanks CarterMoody! (even though I got to the same answer by installing Wireshark, learning Wireshark, watching arduino make a gesture but PC just didn't respond, as though..... as though it wasn't even listening. Hmmmm.) Line 1. was critical for me!
- listener 1883
- allow_anonymous true
then restart mosquitto service.
I used allow_anonymous false
but I also provided a username and password for anyone talking to this broker.
I also messed up my Win 10 Defender firewall rules by having 1883 as Local instead of Remote at the outbound rule. Create that port rule with the 'wizard' and it will set the respective direction for the in and out rules.
Thanks for the direction.
I ended up using this to check if the server name was an IP or an actual address.
IPAddress mqtt_server_ip;
if (mqtt_server_ip.fromString(mqtt_server.c_str())) {
Serial.println("Got IP from server name");
mqtt_client.setServer(mqtt_server_ip, 1883);
} else {
Debug.println("Server name not an IP so just using it");
mqtt_client.setServer(mqtt_server.c_str(), 1883);
}
Hi guys, if you still face the below MQTT issue and you are using raspberry pi as a server, 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 .........
I started facing this issue two days ago after my first Node-Red & MQTT crashed due to a power issue, I tried to make a fresh setup and this issue started my ESP32 refused to connect, but today I found a way to resolve this, follow the steps and copy commands without the quotes:
1) uninstall MQTT "using sudo apt-get purge --remove mosquitto*" 2) you will have to manually delete mosquitto folder "sudo rm -R /etc/mosquitto" 3) enter "cd /etc" and find and delete mosquitto.conf and mosquitto.conf.saved 4) make a fresh installation using "sudo apt install -y mosquitto mosquitto-clients" 5) follow the instruction on this link to setup your mosquitto.conf file "https://randomnerdtutorials.com/how-to-install-mosquitto-broker-on-raspberry-pi/" 6) if you want to setup username and password for MQTT connection use "sudo mosquitto_passwd -c /etc/mosquitto/passwd YOUR_USERNAME" 7) Run the following command to edit the configuration file "sudo nano /etc/mosquitto/mosquitto.conf" 7b) For your mosquitto.conf file parameters please check attached file. (https://github.com/knolleary/pubsubclient/files/12737815/msoquitto.conf.file.sample.odt)
Please let me know if anyone finds this useful or encounters any issues while following this process.
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?