diff --git a/tasmota/tasmota_xnrg_energy/xnrg_23_ade7880.ino b/tasmota/tasmota_xnrg_energy/xnrg_23_ade7880.ino index 7adacaa97..9312f4df5 100644 --- a/tasmota/tasmota_xnrg_energy/xnrg_23_ade7880.ino +++ b/tasmota/tasmota_xnrg_energy/xnrg_23_ade7880.ino @@ -41,6 +41,8 @@ //#define ADE7880_DEBUG //#define ADE7880_PROFILING +#define ADE7880_HEARTBEAT 5 // Allow x seconds of missed interrupts before reinit + // Default calibration parameters can be overridden by a rule as documented above. #define ADE7880_FREQ_INIT 0 // Connected to networks with fundamental frequencies between 55 Hz and 66 Hz (1). Default 45 Hz to 55 Hz (0). #define ADE7880_AIGAIN_INIT 3166385 // rms, current_a @@ -262,6 +264,7 @@ struct Ade7880 { bool calib_frequency; bool irq0_state; uint8_t cycle_count; + uint8_t heartbeat; } Ade7880; /*********************************************************************************************/ @@ -460,6 +463,7 @@ bool Ade7880SetCalibrate(void) { #endif // ADE7880_PROFILING if (Ade7880Init()) { + Ade7880.heartbeat = 0; return true; } } @@ -534,6 +538,7 @@ void Ade7880Service0(void) { // Poll sequence SkipSleep(false); Ade7880Cycle(); + Ade7880.heartbeat = 0; Ade7880.irq0_state = 0; } @@ -545,6 +550,15 @@ void IRAM_ATTR Ade7880Isr0(void) { } } +void Ade7880Heartbeat(void) { + // Reinit if interrupt stopped + Ade7880.heartbeat++; + if (Ade7880.heartbeat > ADE7880_HEARTBEAT) { + AddLog(LOG_LEVEL_DEBUG, PSTR("A78: Reinit")); + Ade7880SetCalibrate(); + } +} + /*********************************************************************************************/ bool Ade7880SetDefaults(const char* json) { @@ -747,6 +761,9 @@ bool Xnrg23(uint8_t function) { case FUNC_LOOP: if (Ade7880.irq0_state) { Ade7880Service0(); } break; + case FUNC_EVERY_SECOND: + Ade7880Heartbeat(); + break; #ifdef ADE7880_MORE_REGS case FUNC_JSON_APPEND: Ade7880Show(1);