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(); + } }