   }
 }
 
-void connectClient() {
-  while (!mqttClient.connected()) {
+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());

      delay(5000);
    }
  }

}
void publish_heartbeat() {
  char payload[8];
  sprintf(payload, "%d", proxim_nominal);
//  connectClient();
  if ( mqttClient.connected() ) {
    mqttClient.publish("/catdoor/avgproxim", payload);
  }
  else {

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

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