arduino-libraries / ArduinoMqttClient

ArduinoMqttClient Library for Arduino
GNU Lesser General Public License v2.1
192 stars 75 forks source link

Connection handling delays #19

Closed mdelain closed 4 years ago

mdelain commented 5 years ago

Hello, I've been playing with the lib with MKR NB 1500 and I encounter delays that seem to be linked to the use of MQTT.

I'm trying to close MQTT and cellular connections before putting the device to sleep. I'm using the following sketch:

#include <MKRNB.h>
#include <ArduinoMqttClient.h>

NB nbAccess;
//GPRS gprs;
NBClient nbClient;
MqttClient mqttClient(nbClient);

const char mqtt_user[] = "none";
const char mqtt_pass[] = "none";
const char mqtt_id[] = "MKRNB1500";
const char mqtt_broker[] = "broker.hivemq.com";

void connectionManager(bool _way);

void setup() {
  Serial.begin(9600);
  while (!Serial);

  mqttClient.setId(mqtt_id);
  mqttClient.setUsernamePassword(mqtt_user, mqtt_pass);

  connectionManager(1);
  connectionManager(0);
}

void loop() {
}

void connectionManager(bool _way = 1) {
  switch (_way) {
    case 1:
      Serial.print("Connecting to cellular network");
      // Discarding the GPRS client, seems working without it
      //while ((nbAccess.begin() != NB_READY) || (gprs.attachGPRS() != GPRS_READY))
      while (nbAccess.begin() != NB_READY)
        Serial.print(".");

      Serial.println("\nYou're connected to the network");

      //-- Comment this part to avoid establishing the MQTT connection
          Serial.print("Connecting to MQTT broker '");
          Serial.print(mqtt_broker);
          Serial.print("'");

          while (!mqttClient.connect(mqtt_broker))
            Serial.print(".");

          Serial.println("\nYou're connected to the MQTT broker");
      //--

      break;

    case 0:
      //-- Comment this part when the MQTT connection is not established
          Serial.println("\nClosing MQTT connection...");
          mqttClient.stop();
      //--

      Serial.println("Disconnecting from cellular network...");
      nbAccess.shutdown();
      Serial.println("Offline.\n");
      break;
  }
}

When I establish the MQTT connection with the broker, it takes ~10 seconds to execute the mqttClient.stop() call. Then it takes another ~40sec to execute the nbAccess.shutdown() call:

18:35:25.250 -> Connecting to cellular network
18:35:28.893 -> You're connected to the network
18:35:28.928 -> Connecting to MQTT broker 'broker.hivemq.com'
18:35:29.605 -> You're connected to the MQTT broker
18:35:29.605 -> 
18:35:29.605 -> Closing MQTT connection...
18:35:39.667 -> Disconnecting from cellular network...
18:36:19.672 -> Offline.
18:36:19.672 -> 

When I don't establish the MQTT connection, the nbAccess.shutdown() call is executed instantaneously:

18:37:22.351 -> Connecting to cellular network
18:37:29.172 -> You're connected to the network
18:37:29.172 -> Disconnecting from cellular network...
18:37:29.206 -> Offline.
18:37:29.206 -> 

So it seems I'm getting some delays here depending if the MQTT connections was open or not. Any thoughts about it?

Thanks! ;)

mdelain commented 4 years ago

While searching, I came across a 40s delay in the NB::shutdown() function of the MKRNB library. So I guess it's not related to the MQTT library ;)

sandeepmistry commented 4 years ago

Hi @mdelain,

I guess it's not related to the MQTT library ;)

Ok, great detective work! I'm closing this issue accordingly then :)