diff --git a/soft/main_pilot_wire_control/main_pilot_wire_control.ino b/soft/main_pilot_wire_control/main_pilot_wire_control.ino index e4f4606..abd4caa 100644 --- a/soft/main_pilot_wire_control/main_pilot_wire_control.ino +++ b/soft/main_pilot_wire_control/main_pilot_wire_control.ino @@ -3,8 +3,12 @@ Switches on or off several heaters. */ +#include + #include "EspMQTTClient.h" +#define LWD_TIMEOUT_MS 15000 + EspMQTTClient client( "rico2", "xxxxx", @@ -33,6 +37,12 @@ struct heater { static struct heater heaters[NB_HEATERS]; +const int led_pin = 2; + +#define led_on(state) do {\ + digitalWrite(led_pin, (state) ? LOW : HIGH); \ +} while (0) + #define heater_on(pin) do {\ digitalWrite(pin, LOW); \ } while(0) @@ -41,6 +51,10 @@ static struct heater heaters[NB_HEATERS]; digitalWrite(pin, HIGH); \ } while(0) +// loop watchdog variables +unsigned long lwdTime = 0; +unsigned long lwdTimeout = LWD_TIMEOUT_MS; +Ticker lwdTicker; void reset_timer(enum rooms room) { @@ -51,6 +65,19 @@ void reset_timer(enum rooms room) os_timer_arm(timer, 3600000, false); } +void ICACHE_RAM_ATTR lwdtcb(void) +{ + if (((millis() - lwdTime) > LWD_TIMEOUT_MS) || ((lwdTimeout - lwdTime) != LWD_TIMEOUT_MS)) + { + ESP.restart(); + } +} + +void lwdtFeed(void) { + lwdTime = millis(); + lwdTimeout = lwdTime + LWD_TIMEOUT_MS; +} + void handle_message(enum rooms room, const String &message) { Serial.println(message); @@ -146,6 +173,9 @@ void onConnectionEstablished() void loop() { + static unsigned int counter; + static bool led_state = false; + client.loop(); if (!client.isConnected()) { // if something wrong happens, switch off the heater @@ -153,4 +183,12 @@ void loop() heater_off(heaters[i].pin); } } + + // blink ESP blue led to show that we are alive + if (((led_state) && (counter % 1000 == 0)) || (!led_state && (counter % 30000 == 0))) { + led_state = !led_state; + led_on(led_state); + } + counter++; + lwdtFeed(); }