Skip to content
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

First run mqttclient.loop() takes 15+ seconds #308

Closed
gjt211 opened this issue Jul 24, 2017 · 5 comments
Closed

First run mqttclient.loop() takes 15+ seconds #308

gjt211 opened this issue Jul 24, 2017 · 5 comments

Comments

@gjt211
Copy link

gjt211 commented Jul 24, 2017

I am using this library with a WiFi secure connection to my own dedicated server running Mosquitto.

After Wi-Fi is connected and secure connection made to my server, In my main loop, I test if mqtt is connected.
if (!mqttclient.connected()) { reconnect(); }
Then in my reconnect function (which is really both a connect & reconnect function), I have the following code

while (!mqttclient.connected()) {
Serial.print("State:");
Serial.println(mqttclient.state());
    Serial.print("Attempting MQTT connection...");
    // Create the client ID
    String clientId = "SH-";
    clientId += String(s);
    // Attempt to connect
    if (mqttclient.connect(clientId.c_str())) {
      Serial.println("connected");
      Serial.print("Subscribe");
      mqttclient.subscribe (topicDO1);
      Serial.println(", Done");

      Serial.print("MQTT Loop");
      mqttclient.loop();
      Serial.println(", Done");
      Serial.print("State:");
      Serial.println(mqttclient.state());
    } else {
      Serial.print("failed, rc=");
      Serial.print(mqttclient.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      for (uint16_t i = 0;i<500;i++){
        delay(10);
      }
      
    }
  }
  Serial.print("F: done in ");
  Serial.print(millis()-start_time);
  Serial.println(" mS");

When this code is run, the mqttclient.loop(); it takes around 15 seconds to complete.
Previously this function did NOT have the mqttclient.loop() and was relying on the mqttclient.loop() inside the main program loop. I added it here as it was also taking 15 seconds in the main loop the first time the loop ran. As the mqttclient.loop() is blocking, at least performing the first call after an mqttclient.connect I have a little better control over it.

It is typically between 15100mS - 15800mS, however sometimes I have seen it as high as 40000mS.
Any ideas why this takes 15+ seconds?

@gjt211
Copy link
Author

gjt211 commented Jul 24, 2017

I have found that changing two lines in PubSubClient.h has varying effects as follows...
If I change both MQTT_KEEPALIVE and MQTT_SOCKET_TIMEOUT to 20, then the first time I run mqttclient.loop() it takes a little over 20 seconds. (Remember sometimes, maybe 1 in 20 or more it connects within a few hundred milliseconds).

Next I tried with KEEPALIVE 10, and SOCKET_TIMEOUT 5.
And it connected in 5441mS.

I then tried with KEEPALIVE 10, and SOCKET_TIMEOUT 15.
And it just keeps trying to reconnect.

So it seems for some reason the socket is timing out. But what does this mean? After the timeout it is connected and I can publish and subscribe and they work. But the sensor might reconnect again once or 20 times each day. This is really bugging me.
Any help is appreciated.

@gjt211
Copy link
Author

gjt211 commented Jul 29, 2017

Anyone?
Why does the first time I run mqtt.loop() not complete until the SOCKET_TIMEOUT occurs, but subsequent calls are fine?

@jorgecis
Copy link

jorgecis commented Aug 3, 2017

gjt211 Please try fix, at let met know if works you you

@stefanbode
Copy link

Hi, I'm using your library with the TASMOTO project here on github to work with the SONOFF devices. I can confirm, that the first .loop() take forever ~20 seconds. I just downloaded the version today Master branch and recompile everything. I do not see a change or better behavior. I will look into the change. Maybe I have an idea. I can confirm that after the first hang everything is working fine.

@stefanbode
Copy link

OK. after manual moving the change to my codebase it works.

@gjt211 gjt211 closed this as completed Nov 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants