From e273e5714eed3335e54dfd1b8b25bcd3d91a11c2 Mon Sep 17 00:00:00 2001 From: Denis Denisov Date: Thu, 8 Sep 2022 23:38:41 +0300 Subject: [PATCH] Trying to reconnect when there's no network connection. Reboots if connection is not established for a while --- thermo_sensor_v2.ino | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/thermo_sensor_v2.ino b/thermo_sensor_v2.ino index b3df161..60ad6ce 100644 --- a/thermo_sensor_v2.ino +++ b/thermo_sensor_v2.ino @@ -3,6 +3,10 @@ #include #include "config.h" +// After how much time without connection we will +// reboot (in millis) +#define NON_CONNECTED_REBOOT_TIMEOUT 60000 + #define DHTPIN 5 #define DHTTYPE DHT22 #define READ_INTERVAL 100 @@ -66,6 +70,7 @@ void UpdateDHTData() } unsigned long last_read = 0; +unsigned long last_connected = 0; void loop() { @@ -78,4 +83,16 @@ void loop() mb.task(); mb.Hreg(REGISTER_CURRENT_TEMP, cur_t); mb.Hreg(REGISTER_CURRENT_HUM, cur_hum); + if (WiFi.status() != WL_CONNECTED) + { + WiFi.reconnect(); + } + else + { + last_connected = current; + } + if (current < last_connected || current - last_connected > NON_CONNECTED_REBOOT_TIMEOUT) + { + ESP.restart(); + } }