knolleary / pubsubclient

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

Attempting MQTT connection...connected #556

Open grandfx opened 5 years ago

grandfx commented 5 years ago

I am having mqtt connection issues with my NodeMCU. First off, I'm new to all of this, so I am sure I am missing something obvious. After numerous connection issues with other 'flashes', I went back to the PubSubClient example "mqtt_esp8266.ino". I seem to have the same connection issue with the example as well. I also thought I might be having an issue with my NodeMCU, so I tried the same process with a new NodeMCU with the same results.

As noted in previous issues, I do NOT have other clients connected with the same client ID

Side Note: I have a Sonoff-Tasmota connected to the same Home Assistant (Hassio) Mosquitto server without this issue, so it leads me to think it is an issue with the client and not the server.

Here is the NodeMCU output: image image

Here is my MQTT log image

knolleary commented 5 years ago

How often are you publishing? Are you calling client.loop() regularly? The log shows the client is being timed out, which means it hasn't sent anything for over 30 seconds.

grandfx commented 5 years ago

I am using the default parameters in the "mqtt_esp8266.ino" file, so the loop is running every 2 seconds:

  client.loop();

  long now = millis();
  if (now - lastMsg > 2000) {
    lastMsg = now;
    ++value;
    snprintf (msg, 50, "hello world #%ld", value);
    Serial.print("Publish message: ");
    Serial.println(msg);
    client.publish("outTopic", msg);
knolleary commented 5 years ago

You have the client.loop() outside of the if statement. That means it is being called on every loop. There have been cases where calling loop too quickly can cause issues with certain hardware.

If you are only publishing from the client, try moving the client.loop inside the if statement, so it isn't called so frequently. Alternatively, add a delay(100) before the client.loop.

grandfx commented 5 years ago

Thanks Nick

So far the solution mostly works. I still have a slight stability/reconnect issue, but it seems to be 1 or 2 magnitudes better than it was before.

I'm not a programmer, so hopefully this was done correctly and can help anyone else with similar issues:

The primary project code was to setup an LED Light published on GitHub by bruhautomation: ESP-MQTT-JSON-Digital-LEDs

I found the following code "client.loop();" in the “ESP_MQTT_Digital_LEDs.ino” file and added "delay(100);" before it.

delay(100);  // Added to code to increase MQTT connection stability
client.loop();  // existing code

As I said, I am still getting some reconnects, but this has increased stability some.

Thanks for your help Nick. Do you have a site to accept donations? I see you are very active in helping people. I'd like to support you back if I can?

papperone commented 5 years ago

I have the same problem with same errors in mosquitto logs but my issue is worst as the client cannot neither pulish or subscribe at all! I've two identical mosquitto brokers (on 2 different Raspberry Pi3) one plain without authentication and one with SSL+user/pw authentication. The exact same sketch can connect and publish/subscribe on the first broker while on the second I've continous disconnections and no publish/subscribe at all! I'm banging my head since few days but no solution so far.... any clue???

ronnyandre commented 5 years ago

Same issue here. I've tested code from different developers, and they all connect, publish a packet and disconnect. In a loop.

From the serial monitor:

Attempting MQTT connection...connected
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
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

The reconnect functionality:

void reconnect()
{
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect(mqtt_client_name, mqtt_user, mqtt_pass)) {
      Serial.println("connected");
      client.publish("checkIn/muntisensor", "Hello"); // just for debugging purposes
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

And every time the MQTT client connects, I receive "Hello" in Node-RED where I am subscribing to the MQTT topic:

21.1.2019, 22:00:02node: 16b096ce.7dc009
checkIn/multisensor : msg.payload : string[5]
"Hello"

I am referencing the reconnect() method at the very end of setup().

JacoFourie commented 5 years ago

I am having the same issues now. It used to work fine. Then I upgraded to the core 1.1 and then I started to get mqtt timeouts of the mosquitto side. I had the same issue using Tasmota on the Sonoff devices an they stated you have to use the 2.3 core as the newer Arduino cores have issues. So now I am wondering if the ESP32 does not have the same issues as I am seeing the same errors now on the ESP32 side after upgrading to 1.0.1

https://github.com/arendst/Sonoff-Tasmota/issues/5263