 const char mqtt_host[] = "space.vijn.org";
 const unsigned short mqtt_port = 1883;
 
+extern void callback(char* topic, byte * payload, unsigned int length);
+
+bool caching = false;
+
 #define PREFIX "test/"
 
 const char rfid_topic[] = PREFIX "deur/space2/rfid";
 const char door_topic[] = PREFIX "deur/space2/open";
-const char log_topic[] = PREFIX "log";
+const char log_topic[] =  PREFIX "log";
 
 WiFiClient wifiClient;
 PubSubClient client(wifiClient);

    case OPEN:
      if (millis() - last_doorstatechange > DOOR_OPEN_DELAY) {
        Serial.println("Closing door.");
        stepper.moveTo(DOOR_CLOSED);
        doorstate = CLOSING;
      };
    case CLOSING:
      if (!is_moving) {
        char msg[256];
        snprintf(msg, sizeof(msg), "[%s] Door is closed.", pname);
        Serial.println(msg);

  doorstate = CLOSED;
  last_doorstatechange = millis();

  Serial.println("SPIFF setup");
  if (!SPIFFS.begin()) {
    Serial.println("SPIFFS Mount Failed - reformatting");
    wipeCache();
  } else {
    caching = true;
    listDir(SPIFFS, "/", "");
  };

  Serial.println("setup() done.\n\n");
}

// This function is taken from the SPIFFS_Test example. Under the expressif ESP32 license.
//
void listDir(fs::FS &fs, const char * dirname, String prefix) {
  Serial.print(prefix);
  Serial.println(dirname);

  File root = fs.open(dirname);
  if (!root) {
    Serial.println("Failed to open directory");
    return;
  }
  if (!root.isDirectory()) {
    Serial.println("Not a directory");
    return;
  }

  File file = root.openNextFile();
  while (file) {
    if (file.isDirectory()) {
      listDir(fs, file.name(), prefix + " ");
    } else {
      Serial.print(prefix + " ");
      Serial.println(file.name());
    }
    file = root.openNextFile();
  }
  root.close();
}

void wipeCache() {
  caching = false;

  if (!SPIFFS.format()) {
    Serial.println("SPIFFS formatting failed.");
    return;
  }

  Serial.println("Formatted.");
  if (!SPIFFS.begin()) {
    Serial.println("SPIFFS mount after formatting failed.");
    return;
  };

  for (int i = 0; i < 255; i++)
    SPIFFS.mkdir(String(i));

  Serial.println("Directory structure created.");
  listDir(SPIFFS, "/", "");
  caching = true;
};

String uid2path(MFRC522::Uid uid) {
  String path = "/uid-";
  for (int i = 0; i < uid.size; i++) {
    path += String(uid.uidByte[i], DEC);
    if (i == 0)
      path += "/";
    else
      path += ".";
  };
  return path;
}

bool checkCache(MFRC522::Uid uid) {
  String path = uid2path(uid) + ".lastOK";
  return SPIFFS.exists(path);
}

void setCache(MFRC522::Uid uid, bool ok) {
  String path = uid2path(uid) + ".lastOK";
  if (ok) {
    File f = SPIFFS.open(path, "w");
    f.println(cnt_minutes);
    f.close();
  } else {
    SPIFFS.remove(path);
  }
}

void openDoor() {
  doorstate = OPENING;
  stepper.moveTo(DOOR_OPEN);
};

bool isOpen() {
  return stepper.currentPosition() == DOOR_OPEN; // no sensors.
}

void closeDoor() {
  stepper.moveTo(DOOR_CLOSED);
  doorstate = CLOSING;
}

bool isClosed() {
  return stepper.currentPosition() == DOOR_CLOSED; // no sensors.
}

MFRC522::Uid uid;

void callback(char* topic, byte * payload, unsigned int length) {
  char buff[256];
