arduino-libraries / ArduinoMqttClient

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

mqttClient.connected() is freezing on VIDOR when MQTT connection drops #43

Open mdelain opened 4 years ago

mdelain commented 4 years ago

Hey there,

I'm facing an issue with the mqttClient.connected() command on MKR VIDOR 4000: it seems to freeze when the MQTT connection is broken.

I've been successfully using this command before on MKR1000, MKR GSM 1400, MKR NB 1500.

I've set up the code below so that you can reproduce. I've simulated a broken MQTT connection by turning my Wi-Fi access point off and on again.

#include <ArduinoMqttClient.h>
#include <WiFiNINA.h>
#include "arduino_secrets.h"

/////// Enter your sensitive data in arduino_secrets.h
const char wifi_ssid[] = SECRET_WIFI_SSID;
const char wifi_pass[] = SECRET_WIFI_PASS;

const char mqtt_broker[] = "test.mosquitto.org";
const int mqtt_port = 1883;

WiFiClient networkClient;
MqttClient mqttClient(networkClient);

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

  mqttClient.setId("TestVIDOR");
}

void loop() {
  if (WiFi.status() != WL_CONNECTED) {
    connectWiFi();
  }

  if (!mqttClient.connected()) {
    connectMQTT();
  }

  mqttClient.poll();
}

void connectWiFi() {
  Serial.print("Attempting to connect to SSID: ");
  Serial.print(wifi_ssid);

  while (WiFi.begin(wifi_ssid, wifi_pass) != WL_CONNECTED) {
    // failed, retry
    Serial.print(".");
    delay(5000);
  }

  Serial.println();
  Serial.println("You're connected to the network");
}

void connectMQTT() {
  Serial.print("Attempting to connect to MQTT broker: ");
  Serial.print(mqtt_broker);

  while (!mqttClient.connected()) {
    if (!mqttClient.connect(mqtt_broker, mqtt_port)) {
      // failed, retry
      Serial.print(".");
      delay(5000);
    }
  }

  Serial.println();
  Serial.println("You're connected to the MQTT broker");
}

The VIDOR board reconnects to Wi-Fi and then freezes when it comes to MQTT:

22:29:20.066 -> Attempting to connect to SSID: Logan
22:29:25.711 -> You're connected to the network
22:29:25.711 -> Attempting to connect to MQTT broker: test.mosquitto.org
22:29:27.911 -> You're connected to the MQTT broker
// Wi-Fi interruption //
22:29:48.061 -> Attempting to connect to SSID: Logan.
22:29:59.894 -> You're connected to the network
// nothing else after //

On the other boards, the code reconnects instantly to the Wi-Fi and to the broker. This is the output for MKR1000 (just changed the Wi-Fi library to WiFi101.h):

22:35:11.458 -> Attempting to connect to SSID: Logan
22:35:13.013 -> You're connected to the network
22:35:13.013 -> Attempting to connect to MQTT broker: test.mosquitto.org
22:35:17.924 -> You're connected to the MQTT broker
// Wi-Fi interruption //
22:35:29.854 -> Attempting to connect to SSID: Logan..
22:35:42.733 -> You're connected to the network
22:35:42.733 -> Attempting to connect to MQTT broker: test.mosquitto.org
22:35:45.101 -> You're connected to the MQTT broker
Webbeh commented 3 years ago

It seems this issue happens on MKR WIFI 1010 as well, most specifically if the MQTT server restarts.

My arduinos were all frozen for several days and I had no choice but to restart them. What I do notice as well is that the ".connected()" method seems to slow down the arduino in a random interval as well. The loop checks it every ~2 seconds and sometimes it just hangs for 10-20 seconds before resuming.

I don't believe power supply is an issue there as it seems to do it whether they're on a lab supply or on a solar panel's battery output.

Webbeh commented 3 years ago

Any new details?

Nuddel69 commented 1 year ago

I'm noticing the same issue on an Arduino Portenta H7 using GSM. I'm connecting and disconnecting the MQTTClient before/after each transmission, and after ~24 hours it freezes on MQTT.connect().