 bool activateRelay = false;
 bool inAPMode = false;
 bool isWifiConnected = false;
+int autoRestartIntervalSeconds = 0;
 
 bool wifiDisabled = true;
 bool doDisableWifi = false;
 bool doEnableWifi = false;
-unsigned long const defaultWifiTimeout = 240000;
-long wifiTimeout = defaultWifiTimeout;
-
-char * our_hostname = 0;
+int wifiTimeout = -1;
+unsigned long wiFiUptimeMillis = 0;
+char * deviceHostname = 0;
 
 int relayPin;
 int relayType;


// Try to connect Wi-Fi
bool connectSTA(const char* ssid, const char* password, byte bssid[6]) {
  WiFi.mode(WIFI_STA);
  // First connect to a wi-fi network
  WiFi.begin(ssid, password, 0, bssid);
  // Inform user we are trying to connect
  Serial.print(F("[ INFO ] Trying to connect WiFi: "));
  Serial.print(ssid);


// Main Loop
void loop() {

  unsigned long currentMillis = millis();
  unsigned long deltaTime = currentMillis-previousLoopMillis;
  unsigned long uptime = NTP.getUptime();
  previousLoopMillis = currentMillis;

  if (autoRestartIntervalSeconds > 0 && uptime > autoRestartIntervalSeconds*1000)
  {
    Serial.println(F("[ UPDT ] Auto restarting..."));
    shouldReboot = true;
  }

  // check for a new update and restart
  if (shouldReboot) {
    Serial.println(F("[ UPDT ] Rebooting..."));
    delay(100);
    ESP.restart();
  }
  if (currentMillis - previousMillis >= activateTime && activateRelay) {
    activateRelay = false;
    digitalWrite(relayPin, relayType);
