diff --git a/defrost_v3.ino b/defrost_v3.ino index 9b28534..2a7f178 100644 --- a/defrost_v3.ino +++ b/defrost_v3.ino @@ -3,6 +3,10 @@ #include "config.h" // network +// After how much time without connection we will +// reboot (in millis) +#define NON_CONNECTED_REBOOT_TIMEOUT 60000 +#define RECONNECT_TIMEOUT 10000 IPAddress my_ip(MY_IP); IPAddress gateway(MY_GW); @@ -47,19 +51,13 @@ void setup() WiFi.setAutoReconnect(true); WiFi.begin(WIFI_NAME, WIFI_PASS); Serial.print("Connecting"); - while (WiFi.status() != WL_CONNECTED) - { - delay(500); - Serial.print("."); - } + delay(500); Serial.println(); - Serial.print("Connected, IP address: "); - Serial.println(WiFi.localIP()); uint8_t macAddr[6]; WiFi.macAddress(macAddr); sprintf(id, "%02x:%02x:%02x:%02x:%02x:%02x", macAddr[0], macAddr[1], macAddr[2], macAddr[3], macAddr[4], macAddr[5]); - Serial.printf("Connected, mac address: %s\n", id); + Serial.printf("mac address: %s\n", id); last_poll = millis(); mb.server(15000); @@ -101,6 +99,9 @@ void UpdateCurrent() last_update = millis(); } +unsigned long last_connected = 0; +unsigned long last_connecting = 0; + void loop() { unsigned long current = millis(); @@ -138,8 +139,18 @@ void loop() mb.Hreg(REGISTER_SECONDARY_TEMP, cur_t); if (WiFi.status() != WL_CONNECTED) { - // no wifi - Serial.println("No WiFi connection"); + if (current < last_connecting || current - last_connecting > RECONNECT_TIMEOUT) + { + WiFi.reconnect(); + last_connecting = current; + } + } + else + { + last_connected = current; + } + if (current < last_connected || current - last_connected > NON_CONNECTED_REBOOT_TIMEOUT) + { ESP.restart(); } }