[SOLVED] WLAN router and MQTT server connection glitches

First not so significant part solved.

With the below code, in loop(), I first establish a WLAN router and then an MQTT broker connection. The CoolTerm serial output shows the first two lines repeated. Why is this?

23:10:39.096 ->
23:10:39.096 -> Trying to connect to WLAN router
23:10:40.757 ->
23:10:41.536 -> Trying to connect to WLAN router
23:10:51.635 -> 3
23:10:51.742 -> Connection to WLAN router successful
23:10:51.843 -> Trying to connect to Adafruit IO
23:10:55.303 -> Connection to Adafruit IO successful

The somewhat more concerning problem is that when I switch the WLAN router off and on again a few minutes later, I sometimes do get a WLAN re-connection, but sometimes not. I am publishing every 30 seconds, not subscribing. What should I change to achieve better reliability?

include <SPI.h>
#include <WiFiNINA.h> // Adafruit's WiFiNINA fork, use version 1.4.0
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"


// Variables...

int status = WL_IDLE_STATUS; // Necessary at start


// Instanced objects...


void setup()
{
  // CoolTerm output only needed for debugging
  Serial.begin(9600);
  while (!Serial); Serial.println();

  // Override default pins with the AirLift's breakout board pins
  WiFi.setPins(SPIWIFI_SS, SPIWIFI_ACK, ESP32_RESET, ESP32_GPIO0, &SPIWIFI);

  // Indicate there is no WLAN router connection yet; GRB colour order!
  WiFi.setLEDs(0, 255, 0);
  delay(2000);
}


void loop()
{
  connectToWLAN();

  connectToMQTT();
 
  // Sensor reading and MQTT publishing...
}


void connectToWLAN()
{
  if (WiFi.status() == WL_NO_MODULE)
  {
    Serial.println("Connection to AirLift FeatherWing WLAN module failed");

    // Indicate there is a WLAN module malfunction
    WiFi.setLEDs(0, 255, 255);

    while (true);
  }

  // Return to loop() if already connected to WLAN router
  if (WiFi.status() == WL_CONNECTED)
  {
    return;
  }

  Serial.println("Trying to connect to WLAN router");

  // Indicate there is no WLAN router connection
  WiFi.setLEDs(96, 255, 0);

  do
  {
    status = WiFi.begin(WLAN_SSID, WLAN_PASS);
    // WL_IDLE_STATUS     = 0
    // WL_NO_SSID_AVAIL   = 1
    // WL_SCAN_COMPLETED  = 2
    // WL_CONNECTED       = 3
    // WL_CONNECT_FAILED  = 4
    // WL_CONNECTION_LOST = 5
    // WL_DISCONNECTED    = 6
    Serial.println(WiFi.status());
    delay(100);
  }
  while (status != WL_CONNECTED);

  Serial.println("Connection to WLAN router successful");

  // Indicate that the WLAN router connection was established
  WiFi.setLEDs(192, 255, 0);
  delay(1000);
}


void connectToMQTT()
{
  // Return to loop() if already connected to MQTT broker
  if (mqtt.connected())
  {
    return;
  }

  Serial.println("Trying to connect to Adafruit IO");

  // Stores a printable string version of the error code returned by
  // connect()
  int8_t MQTTerrorString;

  // In case the error code is not 0, meaning successful connection
  while ((MQTTerrorString = mqtt.connect()) != 0)
  {
    // Print an error message matching the error code
    switch (MQTTerrorString)
    {
      case 1: Serial.println("Wrong protocol"); break;
      case 2: Serial.println("ID rejected"); break;
      case 3: Serial.println("Server unavailable"); break;
      case 4: Serial.println("Bad username/password"); break;
      case 5: Serial.println("Not authenticated"); break;
      case 6: Serial.println("Failed to subscribe"); break;
      default: Serial.println("Connection failed"); break;
    }

    // And in case of an error
    if (MQTTerrorString >= 0)
    {
      // Send a MQTT disconnect packet and break the connection
      mqtt.disconnect();

      // And indicate that the connection failed
      WiFi.setLEDs(255, 0, 255);

      Serial.println("Retry to connect");

      // Wait n seconds before returning to loop() and trying again
      delay(3000);
    }
  }
  // Indicate that the MQTT broker connection was established
  WiFi.setLEDs(255, 0, 0);
  delay(1000);

  Serial.println("Connection to Adafruit IO successful");
}

The problem appears to be solved; I tried it out a few times now. With the above code, I can walk out of range or unplug the router, and WLAN and then MQTT re-connect themselves. Hopefully this works permanently.