Add reset of Energy values when connection to sensor is lost for over 4 seconds

Add reset of Energy values when connection to sensor is lost for over 4 seconds (#5874, #5881)
This commit is contained in:
Theo Arends 2019-05-30 12:45:02 +02:00
parent 60871b37b7
commit 4e95417db4
8 changed files with 68 additions and 25 deletions

View File

@ -1,6 +1,7 @@
/* 6.5.0.13 20190527 /* 6.5.0.13 20190527
* Add command SetOption38 6..255 to set IRReceive protocol detection sensitivity mimizing UNKNOWN protocols (#5853) * Add command SetOption38 6..255 to set IRReceive protocol detection sensitivity mimizing UNKNOWN protocols (#5853)
* Fix missing white channel for WS2812 (#5869) * Fix missing white channel for WS2812 (#5869)
* Add reset of Energy values when connection to sensor is lost for over 4 seconds (#5874, #5881)
* *
* 6.5.0.12 20190521 * 6.5.0.12 20190521
* Add AriLux RF control GPIO option "ALux IrSel" (159) replacing "Led4i" (59) for full LED control (#5709) * Add AriLux RF control GPIO option "ALux IrSel" (159) replacing "Led4i" (59) for full LED control (#5709)

View File

@ -28,6 +28,7 @@
#define ENERGY_NONE 0 #define ENERGY_NONE 0
#define ENERGY_OVERTEMP 73.0 // Industry standard lowest overtemp in Celsius #define ENERGY_OVERTEMP 73.0 // Industry standard lowest overtemp in Celsius
#define ENERGY_WATCHDOG 4 // Allow up to 4 seconds before deciding no valid data present
#define FEATURE_POWER_LIMIT true #define FEATURE_POWER_LIMIT true
@ -71,6 +72,7 @@ unsigned long energy_period = 0; // 12312312 Wh * 10^-2 (deca milli Watt hour
float energy_power_last[3] = { 0 }; float energy_power_last[3] = { 0 };
uint8_t energy_power_delta = 0; uint8_t energy_power_delta = 0;
uint8_t energy_data_valid = 0;
bool energy_voltage_available = true; // Enable if voltage is measured bool energy_voltage_available = true; // Enable if voltage is measured
bool energy_current_available = true; // Enable if current is measured bool energy_current_available = true; // Enable if current is measured
@ -327,6 +329,18 @@ void EnergyOverTempCheck()
SetAllPower(POWER_ALL_OFF, SRC_OVERTEMP); SetAllPower(POWER_ALL_OFF, SRC_OVERTEMP);
} }
} }
if (energy_data_valid <= ENERGY_WATCHDOG) {
energy_data_valid++;
if (energy_data_valid > ENERGY_WATCHDOG) {
// Reset energy registers
energy_voltage = 0;
energy_current = 0;
energy_active_power = 0;
if (!isnan(energy_frequency)) { energy_frequency = 0; }
if (!isnan(energy_power_factor)) { energy_power_factor = 0; }
energy_start = 0;
}
}
} }
/*********************************************************************************************\ /*********************************************************************************************\

View File

@ -86,6 +86,7 @@ void HlwCfInterrupt(void) // Service Power
hlw_cf_pulse_last_time = us; hlw_cf_pulse_last_time = us;
hlw_energy_period_counter++; hlw_energy_period_counter++;
} }
energy_data_valid = 0;
} }
void HlwCf1Interrupt(void) // Service Voltage and Current void HlwCf1Interrupt(void) // Service Voltage and Current
@ -104,6 +105,7 @@ void HlwCf1Interrupt(void) // Service Voltage and Current
hlw_cf1_timer = 8; // We need up to HLW_SAMPLE_COUNT samples within 1 second (low current could take up to 0.3 second) hlw_cf1_timer = 8; // We need up to HLW_SAMPLE_COUNT samples within 1 second (low current could take up to 0.3 second)
} }
} }
energy_data_valid = 0;
} }
/********************************************************************************************/ /********************************************************************************************/
@ -189,14 +191,20 @@ void HlwEvery200ms(void)
void HlwEverySecond(void) void HlwEverySecond(void)
{ {
unsigned long hlw_len; if (energy_data_valid > ENERGY_WATCHDOG) {
hlw_cf1_voltage_pulse_length = 0;
hlw_cf1_current_pulse_length = 0;
hlw_cf_power_pulse_length = 0;
} else {
unsigned long hlw_len;
if (hlw_energy_period_counter) { if (hlw_energy_period_counter) {
hlw_len = 10000 / hlw_energy_period_counter; hlw_len = 10000 / hlw_energy_period_counter;
hlw_energy_period_counter = 0; hlw_energy_period_counter = 0;
if (hlw_len) { if (hlw_len) {
energy_kWhtoday_delta += ((hlw_power_ratio * Settings.energy_power_calibration) / hlw_len) / 36; energy_kWhtoday_delta += ((hlw_power_ratio * Settings.energy_power_calibration) / hlw_len) / 36;
EnergyUpdateToday(); EnergyUpdateToday();
}
} }
} }
} }

View File

@ -139,6 +139,7 @@ bool CseSerialInput(void)
uint8_t checksum = 0; uint8_t checksum = 0;
for (uint8_t i = 2; i < 23; i++) { checksum += serial_in_buffer[i]; } for (uint8_t i = 2; i < 23; i++) { checksum += serial_in_buffer[i]; }
if (checksum == serial_in_buffer[23]) { if (checksum == serial_in_buffer[23]) {
energy_data_valid = 0;
CseReceived(); CseReceived();
cse_receive_flag = 0; cse_receive_flag = 0;
return 1; return 1;
@ -170,28 +171,34 @@ bool CseSerialInput(void)
void CseEverySecond(void) void CseEverySecond(void)
{ {
long cf_frequency = 0; if (energy_data_valid > ENERGY_WATCHDOG) {
voltage_cycle = 0;
if (CSE_PULSES_NOT_INITIALIZED == cf_pulses_last_time) { current_cycle = 0;
cf_pulses_last_time = cf_pulses; // Init after restart power_cycle = 0;
} else { } else {
if (cf_pulses < cf_pulses_last_time) { // Rolled over after 65535 pulses long cf_frequency = 0;
cf_frequency = (65536 - cf_pulses_last_time) + cf_pulses;
if (CSE_PULSES_NOT_INITIALIZED == cf_pulses_last_time) {
cf_pulses_last_time = cf_pulses; // Init after restart
} else { } else {
cf_frequency = cf_pulses - cf_pulses_last_time; if (cf_pulses < cf_pulses_last_time) { // Rolled over after 65535 pulses
} cf_frequency = (65536 - cf_pulses_last_time) + cf_pulses;
if (cf_frequency && energy_active_power) { } else {
unsigned long delta = (cf_frequency * Settings.energy_power_calibration) / 36; cf_frequency = cf_pulses - cf_pulses_last_time;
// prevent invalid load delta steps even checksum is valid (issue #5789):
if (delta <= (3680*100/36) * 10 ) { // max load for S31/Pow R2: 3.68kW
cf_pulses_last_time = cf_pulses;
energy_kWhtoday_delta += delta;
} }
else { if (cf_frequency && energy_active_power) {
AddLog_P(LOG_LEVEL_DEBUG, PSTR("CSE: Load overflow")); unsigned long delta = (cf_frequency * Settings.energy_power_calibration) / 36;
cf_pulses_last_time = CSE_PULSES_NOT_INITIALIZED; // prevent invalid load delta steps even checksum is valid (issue #5789):
if (delta <= (3680*100/36) * 10 ) { // max load for S31/Pow R2: 3.68kW
cf_pulses_last_time = cf_pulses;
energy_kWhtoday_delta += delta;
}
else {
AddLog_P(LOG_LEVEL_DEBUG, PSTR("CSE: Load overflow"));
cf_pulses_last_time = CSE_PULSES_NOT_INITIALIZED;
}
EnergyUpdateToday();
} }
EnergyUpdateToday();
} }
} }
} }

View File

@ -169,6 +169,7 @@ void PzemEvery200ms(void)
if (data_ready) { if (data_ready) {
float value = 0; float value = 0;
if (PzemRecieve(pzem_responses[pzem_read_state], &value)) { if (PzemRecieve(pzem_responses[pzem_read_state], &value)) {
energy_data_valid = 0;
switch (pzem_read_state) { switch (pzem_read_state) {
case 1: // Voltage as 230.2V case 1: // Voltage as 230.2V
energy_voltage = value; energy_voltage = value;

View File

@ -469,6 +469,7 @@ void McpParseData(void)
energy_active_power = 0; energy_active_power = 0;
energy_current = 0; energy_current = 0;
} }
energy_data_valid = 0;
} }
/********************************************************************************************/ /********************************************************************************************/
@ -526,6 +527,13 @@ void McpSerialInput(void)
void McpEverySecond(void) void McpEverySecond(void)
{ {
if (energy_data_valid > ENERGY_WATCHDOG) {
mcp_voltage_rms = 0;
mcp_current_rms = 0;
mcp_active_power = 0;
mcp_line_frequency = 0;
}
if (mcp_active_power) { if (mcp_active_power) {
energy_kWhtoday_delta += ((mcp_active_power * 10) / 36); energy_kWhtoday_delta += ((mcp_active_power * 10) / 36);
EnergyUpdateToday(); EnergyUpdateToday();

View File

@ -51,6 +51,8 @@ void PzemAcEverySecond(void)
if (error) { if (error) {
AddLog_P2(LOG_LEVEL_DEBUG, PSTR(D_LOG_DEBUG "PzemAc response error %d"), error); AddLog_P2(LOG_LEVEL_DEBUG, PSTR(D_LOG_DEBUG "PzemAc response error %d"), error);
} else { } else {
energy_data_valid = 0;
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
// 01 04 14 08 D1 00 6C 00 00 00 F4 00 00 00 26 00 00 01 F4 00 64 00 00 51 34 // 01 04 14 08 D1 00 6C 00 00 00 F4 00 00 00 26 00 00 01 F4 00 64 00 00 51 34
// Id Cc Sz Volt- Current---- Power------ Energy----- Frequ PFact Alarm Crc-- // Id Cc Sz Volt- Current---- Power------ Energy----- Frequ PFact Alarm Crc--

View File

@ -51,6 +51,8 @@ void PzemDcEverySecond(void)
if (error) { if (error) {
AddLog_P2(LOG_LEVEL_DEBUG, PSTR(D_LOG_DEBUG "PzemDc response error %d"), error); AddLog_P2(LOG_LEVEL_DEBUG, PSTR(D_LOG_DEBUG "PzemDc response error %d"), error);
} else { } else {
energy_data_valid = 0;
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
// 01 04 10 05 40 00 0A 00 0D 00 00 00 02 00 00 00 00 00 00 D6 29 // 01 04 10 05 40 00 0A 00 0D 00 00 00 02 00 00 00 00 00 00 D6 29
// Id Cc Sz Volt- Curre Power------ Energy----- HiAlm LoAlm Crc-- // Id Cc Sz Volt- Curre Power------ Energy----- HiAlm LoAlm Crc--