From 30504aedec4a9d83544a21b4ad8c048af4115787 Mon Sep 17 00:00:00 2001 From: Denis Denisov Date: Wed, 24 Dec 2025 10:04:31 +0300 Subject: [PATCH] =?UTF-8?q?=D0=92=D0=BD=D1=8F=D1=82=D0=BD=D0=BE=D0=B5=20?= =?UTF-8?q?=D0=BB=D0=BE=D0=B3=D0=B8=D1=80=D0=BE=D0=B2=D0=B0=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5.=20=D0=A1=D0=BC=D0=BE=D1=82=D1=80=D0=B8=D0=BC=20=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=BD=D0=B0=D0=BB=D0=B8=D1=87=D0=B8=D0=B5=20=D0=BE?= =?UTF-8?q?=D0=BF=D1=80=D0=BE=D1=81=D0=BE=D0=B2=20=D1=87=D0=B5=D1=80=D0=B5?= =?UTF-8?q?=D0=B7=20modbus,=20=D0=B5=D1=81=D0=BB=D0=B8=20=D0=B8=D1=85=20?= =?UTF-8?q?=D0=BD=D0=B5=D1=82=20-=20=D1=81=D1=87=D0=B8=D1=82=D0=B0=D0=B5?= =?UTF-8?q?=D0=BC,=20=D1=87=D1=82=D0=BE=20=D0=BD=D0=B0=D0=B4=D0=BE=20?= =?UTF-8?q?=D1=80=D0=B5=D0=B1=D1=83=D1=82=D0=BD=D1=83=D1=82=D1=8C=D1=81?= =?UTF-8?q?=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- thermo_sensor_v2.ino | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/thermo_sensor_v2.ino b/thermo_sensor_v2.ino index e74deb0..a3d5200 100644 --- a/thermo_sensor_v2.ino +++ b/thermo_sensor_v2.ino @@ -6,6 +6,8 @@ // After how much time without connection we will // reboot (in millis) #define NON_CONNECTED_REBOOT_TIMEOUT 60000 +// Will log current state with this interval +#define LOG_INTERVAL 1000 #define DHTPIN D5 #define DHTTYPE DHT22 @@ -27,6 +29,14 @@ int cur_t = 0; int cur_hum = 0; +unsigned long last_poll = 0; + +Modbus::ResultCode OnModbusRequest(Modbus::FunctionCode fc, const Modbus::RequestData data) +{ + last_poll = millis(); + return Modbus::EX_SUCCESS; +} + void setup() { Serial.begin(115200); @@ -56,6 +66,7 @@ void setup() Serial.printf("Connected, mac address: %s\n", id); // Modbus init + mb.onRequest(OnModbusRequest); mb.server(15000); mb.addHreg(REGISTER_CURRENT_TEMP, cur_t); mb.addHreg(REGISTER_CURRENT_HUM, cur_hum); @@ -71,6 +82,7 @@ void UpdateDHTData() unsigned long last_read = 0; unsigned long last_connected = 0; +unsigned long last_log = 0; void loop() { @@ -79,11 +91,6 @@ void loop() { UpdateDHTData(); last_read = current; - Serial.print("cur_t = "); - Serial.print(cur_t); - Serial.print(" cur_h = "); - Serial.print(cur_hum); - Serial.println(); } mb.task(); mb.Hreg(REGISTER_CURRENT_TEMP, cur_t); @@ -98,6 +105,23 @@ void loop() } if (current < last_connected || current - last_connected > NON_CONNECTED_REBOOT_TIMEOUT) { + Serial.println("Detected wifi disconnection, will reboot"); ESP.restart(); } + if (current < last_poll || current - last_poll > NON_CONNECTED_REBOOT_TIMEOUT) + { + Serial.println("Detected absense of polls, consider it as connection failure"); + ESP.restart(); + } + if (current < last_log || current - last_log > LOG_INTERVAL) + { + last_log = current; + Serial.print("t: "); + Serial.print(cur_t); + Serial.print(" h: "); + Serial.print(cur_hum); + Serial.print(" WiFi: "); + Serial.print(WiFi.status()); + Serial.println(); + } }