From 99e0192374a6f4db571b9b6706631e46e20b7d9c Mon Sep 17 00:00:00 2001 From: Denis Denisov Date: Sun, 25 Feb 2024 12:23:04 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A3=D0=BB=D1=83=D1=87=D1=88=D0=B8=D0=BB=20?= =?UTF-8?q?=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D1=83=20=D0=B2=20=D1=81=D0=BB?= =?UTF-8?q?=D1=83=D1=87=D0=B0=D0=B5=20=D0=BE=D1=82=D1=81=D1=83=D1=82=D1=81?= =?UTF-8?q?=D1=82=D0=B2=D0=B8=D1=8F=20WiFi?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- defrost_v3.ino | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) 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(); } }