pubsubclient: KeepaliveTimeout error with different brokers

When I connect to the MQTT server it works, but after 10 - 30 seconds the broker gives a keepalive timeout and reconnects rightaway. The loop is continuously executed. I tried different brokers but still the same problem. Mosquitto and Aedes (Node-Red) both are set to 60 seconds by default. So the broker expects every 60 seconds a PINGREQ. The esp8266 are default set to 15 seconds in the PubSubClient Library. So every 15 seconds it sends a PINGREQ. The code is without any delay so client.loop() is constantly being updated. Why do I still get all these disconnections?

void loop(){
 reconnectWifi();
 client.loop();
}
void reconnectWifi() { //(re)connect to WiFi and MQTT Server.
  if (WiFi.status() != WL_CONNECTED && currentMillis - wifiLostTimer >= 500UL ) {
    wifiLostTimer = currentMillis;
    delay(0); //to keep wifi connection alive same als yield()
    Serial.println("Wifi connecting");
    wifiLost = true;
    mqttLost = true;
    reconnectTried++;
    if (reconnectTried == 60 && digitalRead(relaySwitch) == relayOffOutput) {
      ESP.restart();
    }
  }

  else if (WiFi.status() == WL_CONNECTED && wifiLost == true) {
    reconnectTried = 0;
    wifiLost = false;
    Serial.println("Wifi Connected!!!");
  }
  // Loop until we're reconnected
  else if (client.connected() == false && currentMillis - timeOut >= 10000UL && WiFi.status() == WL_CONNECTED) {
    mqttLost = true;
    delay(0); //to keep wifi-connection alive
    timeOut = currentMillis;
    Serial.println("Attempting MQTT connection...");
    // Attempt to connect
    Serial.println(deviceID.c_str());
    client.connect(deviceID.c_str());
  }
  else if (client.connected() == true && wifiLost == false && mqttLost == true) {
    mqttLost = false;
    Serial.println("MQTT Connected!!!");
    String rxTopic = rxTopicFirst + deviceID;
    client.subscribe(rxTopic.c_str());
    Serial.println("Subscribed to: " + rxTopic);
  }
}

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Comments: 18 (1 by maintainers)

Commits related to this issue

Most upvoted comments

I have got this keepalive problem too, it seems that the client gets stucks waiting for a pringresponse, it doesnt get one and the server terminates connection. I changed PubSubClient.cpp like this:

Line 255: lastInActivity = lastOutActivity = millis();

And added the following line right after it: pingOutstanding = false;

This solved the issues for me.

This client library is unlike to support QoS 1 publishing any time soon for all the reasons I’ve given every time someone asks about it.

The keepalive handling in the client should keep the connection going regardless of the qos level being published at.

We have seen some issues with client.loop being called too frequently. Putting it in a tight loop like you have means the hardware spends all of its time checking the connection which gets in the way of proper handling of the data.

Have you tried adding a small delay in your loop function - delay(100)?