Внятное логирование. Смотрим на наличие опросов через modbus, если их нет - считаем, что надо ребутнуться

This commit is contained in:
ftc
2025-12-24 10:04:31 +03:00
parent 75fb2e098e
commit 30504aedec
+29 -5
View File
@@ -6,6 +6,8 @@
// After how much time without connection we will // After how much time without connection we will
// reboot (in millis) // reboot (in millis)
#define NON_CONNECTED_REBOOT_TIMEOUT 60000 #define NON_CONNECTED_REBOOT_TIMEOUT 60000
// Will log current state with this interval
#define LOG_INTERVAL 1000
#define DHTPIN D5 #define DHTPIN D5
#define DHTTYPE DHT22 #define DHTTYPE DHT22
@@ -27,6 +29,14 @@ int cur_t = 0;
int cur_hum = 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() void setup()
{ {
Serial.begin(115200); Serial.begin(115200);
@@ -56,6 +66,7 @@ void setup()
Serial.printf("Connected, mac address: %s\n", id); Serial.printf("Connected, mac address: %s\n", id);
// Modbus init // Modbus init
mb.onRequest(OnModbusRequest);
mb.server(15000); mb.server(15000);
mb.addHreg(REGISTER_CURRENT_TEMP, cur_t); mb.addHreg(REGISTER_CURRENT_TEMP, cur_t);
mb.addHreg(REGISTER_CURRENT_HUM, cur_hum); mb.addHreg(REGISTER_CURRENT_HUM, cur_hum);
@@ -71,6 +82,7 @@ void UpdateDHTData()
unsigned long last_read = 0; unsigned long last_read = 0;
unsigned long last_connected = 0; unsigned long last_connected = 0;
unsigned long last_log = 0;
void loop() void loop()
{ {
@@ -79,11 +91,6 @@ void loop()
{ {
UpdateDHTData(); UpdateDHTData();
last_read = current; last_read = current;
Serial.print("cur_t = ");
Serial.print(cur_t);
Serial.print(" cur_h = ");
Serial.print(cur_hum);
Serial.println();
} }
mb.task(); mb.task();
mb.Hreg(REGISTER_CURRENT_TEMP, cur_t); mb.Hreg(REGISTER_CURRENT_TEMP, cur_t);
@@ -98,6 +105,23 @@ void loop()
} }
if (current < last_connected || current - last_connected > NON_CONNECTED_REBOOT_TIMEOUT) if (current < last_connected || current - last_connected > NON_CONNECTED_REBOOT_TIMEOUT)
{ {
Serial.println("Detected wifi disconnection, will reboot");
ESP.restart(); 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();
}
} }