   return NULL;
 }
 
-unsigned long last_mqtt_connect_try = 0;
 void reconnectMQTT() {
-  last_mqtt_connect_try = millis();
 
   Debug.print("Attempting MQTT connection (State : ");
   Debug.print(state2str(client.state()));

}

int checkTagReader() {
  MFRC522::Uid uid = { 0, 0, 0 };

#if 0
  static unsigned long test = 0;
  if (millis() - test > 15000) {
    uid = { 4, { 1, 2, 3, 4}, 0 };
    test = millis();
  }
#endif

  // Look for new cards
  if ( ! mfrc522.PICC_IsNewCardPresent()) {
    return 0;
  }

  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial()) {
    return 0;
  }

  uid = mfrc522.uid;
  // Dump debug info about the card; PICC_HaltA() is automatically called
  // mfrc522.PICC_DumpToSerial(&(mfrc522.uid));

  if (uid.size) {
    lasttag[0] = 0;
    for (int i = 0; i < uid.size; i++) {
      char buff[5];
      snprintf(buff, sizeof(buff), "%s%d", i ? "-" : "", uid.uidByte[i]);
      strcat(lasttag, buff);
    }
    lasttagbeat = beatCounter;

    char beatAsString[ MAX_BEAT ];
    snprintf(beatAsString, sizeof(beatAsString), BEATFORMAT, beatCounter);
    Sha256.initHmac((const uint8_t*)passwd, strlen(passwd));
    Sha256.print(beatAsString);
    Sha256.write(uid.uidByte, uid.size);
    const char * tag_encoded = hmacToHex(Sha256.resultHmac());

    char buff[250];
    snprintf(buff, sizeof(buff), "energize % s % s % s", moi, machine, tag_encoded);
    send(buff);

    return 1;
  }
}

void loop() {

  tock();

  // Keepting time is a bit messy; the millis() wrap around and
  // the SPI access to the reader seems to mess with the millis().
  //
  static unsigned long last_loop = 0;
  if (millis() - last_loop >= 1000) {
    unsigned long secs = (millis() - last_loop + 500) / 1000;
    beatCounter += secs;
    last_loop = millis();
  }

  static unsigned long last_wifi_ok = 0;
  if (WiFi.status() != WL_CONNECTED) {
    if (machinestate <= WAITINGFORCARD)
      machinestate = NOCONN;
    if ( millis() - last_wifi_ok > 10000) {
      Log.println("Connection dead for 10 seconds now -- Rebooting...");
      ESP.restart();
    }
  } else { 
    if (machinestate == NOCONN)
      machinestate == WAITINGFORCARD;
    last_wifi_ok = millis();
  };
  
#ifdef OTA
  ArduinoOTA.handle();
#endif

  client.loop();

  static unsigned long last_mqtt_connect_try = 0;
  if (!client.connected()) {
    if (machinestate <= WAITINGFORCARD)
      machinestate = NOCONN;
    if (millis() - last_mqtt_connect_try > 5000) {
      reconnectMQTT();
      last_mqtt_connect_try = millis();
    } 
  } else {
    if (machinestate == NOCONN)
      machinestate == WAITINGFORCARD;
  };

#ifdef DEBUG3
  static unsigned long last_beat = 0;
