       delay(5000);
     }
   }
-
-}
+  if ( nbtry == MAX_CLIENT_RETRIES ) {
+    Serial.println("Give up on mqtt client connect!");
+    Serial.println("It would be a good time to reboot the board now...");
+    // system_restart();
+  }
+  return connected;
+}
+
 void publish_heartbeat() {
   char payload[8];
   sprintf(payload, "%d", proxim_nominal);
-//  connectClient();
-  if ( mqttClient.connected() ) {
+  if ( connectClient() ) {
     mqttClient.publish("/catdoor/avgproxim", payload);
   }
   else {

  setup_wifi();
  mqttClient.setServer(MQTT_SERVER, MQTT_PORT);

  // Prepare client
  connectClient();
  
  // Schedulers
  // tickerHeartbeat.attach(10, heartbeat);
  // tickerUpdate.attach_ms(100, update);

  }
}

bool connectClient() {
  uint8_t nbtry = 0;
  bool connected = mqttClient.connected();
  while ( !connected && nbtry < MAX_CLIENT_RETRIES ) {
    Serial.print("Attempting MQTT connection...");
    nbtry++;
    if (mqttClient.connect("MistyDoor")) {
      Serial.println("connected");
      connected = true;
    } else {
      Serial.print("failed, rc=");
      Serial.print(mqttClient.state());
