Skip to content

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

Open
@mdelain

Description

@mdelain

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions