   if (activateRelay) {
     digitalWrite(relayPin, !relayType);
   }
+
+  if (isWifiConnected)
+    wiFiUptimeMillis += deltaTime;
   
-  if (wifiTimeout > 0)
-    wifiTimeout -= deltaTime;
-
-  if (wifiTimeout <= 0 && isWifiConnected == true)
+  if (wifiTimeout>0 && wiFiUptimeMillis>(wifiTimeout*1000) && isWifiConnected == true)
   {
     doDisableWifi = true;
   }
-  
+
   if (doDisableWifi==true)
   {
     doDisableWifi=false;
+    wiFiUptimeMillis = 0;
     disableWifi();
   }
   else if (doEnableWifi==true)


// 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);

  inAPMode = true;
  WiFi.mode(WIFI_AP);
  Serial.print(F("[ INFO ] Configuring access point... "));
  Serial.println(WiFi.softAP(deviceHostname) ? "Ready" : "Failed!");
  // Access Point IP
  IPAddress myIP = WiFi.softAPIP();
  Serial.print(F("[ INFO ] AP IP address: "));
