-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Attempting MQTT connection...failed, rc=-2 try again in 5 seconds #185
Comments
Have you checked one of the basic http examples to verify you have the network configuration and setup correct? -2 means it's failing to create a network connection to the broker. |
I too get the same error |
I get the same error |
It actually worked once for me a whole day just like it is suppose to. Powered all off at night and the next day it will not work, get the same message as above, and has not worked since, no matter what machine I try it on. |
Issue #[203] is related |
You can have one more client in the network the same MQTT Client ID. I have the same problem but I found that I have two clients with the same Client ID. |
Same Problem here. `#include <ESP8266WiFi.h> #include "DHT.h" DHT dht(2, DHT22); const char* ssid = "Funkzelle1212"; WiFiClient espClient; void setup() { } void setup_wifi() { delay(10); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { Serial.println(""); void callback(char* topic, byte* payload, unsigned int length) { // Switch on the LED if an 1 was received as first character } void reconnect() { if (!client.connected()) { client.loop(); snprintf (msg, 75, "T: %.1f" , t); // snprintf (msg, 75, "Nr %d", value); }` |
try adding |
it was the host, it works with ip adress: |
The connection to my MQTT broker on a raspberry worked for months... probably after an upgrade it started to fail, but not always... just most of the time. The solution for me was to move to broker from wireless to wired. Now using the wired-ip it works, on the wireless-ip most of time not! |
Any recent comments on this? I've got two boards, running the same code with some local changes (like client/host name, etc). Both publishing to the same wired MQTT broker. For some reason I don't understand, one (or the other) will start reporting a MQTT connection failure that it doesn't recover from. The other board won't exhibit the condition. Very infrequent, resetting the chip clears the issue. Durring the failure condition, other wifi functions are working, for instance I can still get weather over a different WiFiClient. I've not yet tried either the items suggested here:
Thought I'd ask before taking shots. Thanks, |
Same problem i am facing now. it is not able to reconnect again. when MQTT Broker is running that time its working fine. When i restarted the MQTT Broker is not able to reconnect. Attempting MQTT connection...failed, rc=-2 |
Hey I've got the same Problem here. Looking for a Solution for three days, today nothing changed, it works. I know this is the wrong location for this Question, but I found no solution for this: My Mosquitto MQTT Broker (running at a Ubuntu Mashine) is on Protocol Version 3.1. How can I change it to Version 3.1.1? |
Same problem here... |
When this happens to me, I reload 0x00000 bin file using esp_flasher and presto, problem goes away. |
I had a similar issue (connecting and reconnecting in 8-10 sec), the solution was changing the place of some variables used inside loop() function. |
I solved this issue this way (similar thread): #604 (comment) |
My raspberry pi worked as a broker and esp8266 as client. whenever raspberry pi disconnected from wifi network and reconnected, i had this issue of esp disconnecting and could not reconnect. I tried to reconnect esp to the raspberry wifi by disconnecting WiFi.disconnect(), and it worked. |
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. I just changed it back without changing anything else and the error disappeared. |
My ¢2, in case it helps:
One thing that nobody mentioned above is VLAN, I'm running this on a tightly locked network. UPDATE: VLAN matters. |
Changing from IP to server name like I mentioned before was not the answer unfortunately. I do however have something stable working now. I found that frequently the code does not connect after initial upload but does connect after a restart. What I have done is count the number of connect attempts and if there are more than 3 in succession I restart the ESP. If it connects, I reset the counter. Don’t know why but it works quite well. I have 15 ESPs connected this way know to my Domoticz MQTT and can see they report in every 2 minutes. |
I figured it out - the connection fails on |
@climategadgets VLAN should be transparent at IP level. |
@eku That would be my expectation, too. Here's the facts: several MQTT brokers, different platforms, some of them on VLAN, some not. Independently, several WiFi networks, some on VLAN, some not. The only case when |
I don’t think it’s just a VLAN problem as I have seen it on public internet addresses as well |
Hola bueno por si le sirve a alguien, el problema es cuando se pasa al bróker a través de un repetidor Wifi, extensor Wifi, o si es un bróker local se esta utilizando el Wifi, solo sucede con el esp8266, si utilizan el ESP32 no tendrán ese problema, saludos |
@ingeltro , I think it may not be the ESP8266 per se, but the particular TCP/IP stack implementation for it on this platform. I almost gave up on ESP8266 as well, but then tried https://esphome.io/components/mqtt.html and, surprisingly, it worked. |
You should modify this line in the example code if you are using a secure account on your MQTT broker |
For those of you who have this issue and using WiFiClientSecure at the same time: I ran into this issue too and basically I had an issue with the certificates I provided to WiFiClientSecure. |
I found this video as a problem solver, This may help you. |
Would you please share the code you used and worked? I am using: mqtt: But I can't connect. Thanks for your help |
@Xesphome , https://esphome.io/components/mqtt.html. I eventually just ported all the sensors and actuators to it, and there was no longer a need for custom code. |
I had this problem. It turned that the reason for this -2 connection status was the broker; It was down. I turned the mosquitto broker on and the issue was resolved. |
Hopefully this will be of use to some: The not-quite-underlying but mid-layer reason in a case I had run into was that the Arduino framework, in my case its core, referenced by Espressif platform libraries I was using, had been updated to a new major version, including an array of breaking changes. Both, Arduino Core (implicitly referenced by the Espressif platform) and the Espressif platform (context: PlatformIO) were bumped to Symptoms were as described numerous times above: WiFi connected, acquired IP through DHCP just fine but PubSubClient kept failing to connect (rc: I'd be happy to test if it might help in finding the root cause. I'm fairly certain my setup is as described in the examples (at least hasn't changed since in the last ~1.5 years) but I haven't tried to isolate the setup as complexity would make it a bigger effort. |
Hi I have a Teensy 4.1 and im using the Nativeethernet library and Pubsubclient library to connect to MQTT. // Update these with values suitable for your network. EthernetClient ethClient; void reconnect() { void setup() client.setServer(server, 1883); Ethernet.begin(mac, ip); void loop() void teensyMAC(uint8_t *mac) |
If there is someone facing this problem, i found the solution. By default, Mosquitto only listen to local connections (127.0.0.1)
And more important: If you're using the examples from PubSubClient, you might notice that they doesn't use client authentication, so this line must also be in yout config file:
Hope it helps! |
I'm getting this error on a new ESP8266 node. I've allowed anonymous connections, and am listening on 0.0.0.0. The other two nodes connect, but this last one fails with rc=-2 continuously. From /var/log/mosquitto/mosquitto.log I don't even see an attempt to connect. |
The fix from Kyuugeki worked for me. Thank-you |
I tried the fix off Kyuugeki but unfortunately the problem remains the same. Weirdly I found a way to force the conncection by just starting a terminal conncection to the raspberry from a computer in the same network. (just opening putty conncection is enough, password wasn't provided yet for the terminal but this somehow makes the ESP8266 connect suddenly to the MQTT server) Has somebody an idea what could be the reason behind that? |
// Control ESP8266 anywhere
// Import required libraries
include <ESP8266WiFi.h>
include <PubSubClient.h>
include <aREST.h>
// Clients
WiFiClient espClient;
PubSubClient client(espClient);
// Create aREST instance
aREST rest = aREST(client);
// Unique ID to identify the device for cloud.arest.io
char* device_id = "e49n2di";
// WiFi parameters
const char* ssid = "Quasar";
const char* password = "quablu49476";
// Functions
void callback(char* topic, byte* payload, unsigned int length);
void setup(void)
{
// Start Serial
Serial.begin(115200);
// Set callback
client.setCallback(callback);
// Give name and ID to device
rest.set_id(device_id);
rest.set_name("relay_anywhere");
// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Set output topic
char* out_topic = rest.get_topic();
}
void loop() {
// Connect to the cloud
rest.loop(client);
}
// Handles message arrived on subscribed topic(s)
void callback(char* topic, byte* payload, unsigned int length) {
rest.handle_callback(client, topic, payload, length);
}
this is my code and when i opened serial monitor in arduino ide then continuously its shows like--
..
WiFi connected
Attempting MQTT connection...failed, rc=-2 try again in 5 seconds
Attempting MQTT connection...Connected to aREST.io
Attempting MQTT connection...failed, rc=-2 try again in 5 seconds
Attempting MQTT connection...Connected to aREST.io
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...failed, rc=-2 try again in 5 seconds
Attempting MQTT connection...Connected to aREST.io
Attempting MQTT connection...failed, rc=-2 try again in 5 seconds
The text was updated successfully, but these errors were encountered: