Init Energy Total on energy monitoring devices with Energy Total Register

Init Energy Total on energy monitoring devices with Energy Total Register (#6282)
This commit is contained in:
Theo Arends 2019-09-03 21:53:20 +02:00
parent 3ee83ae147
commit b3562e030e
5 changed files with 21 additions and 27 deletions

View File

@ -146,6 +146,22 @@ void EnergyUpdateToday(void)
} }
} }
void EnergyUpdateTotal(float value, bool kwh)
{
uint32_t multiplier = (kwh) ? 100000 : 100; // kWh or Wh to deca milli Wh
if (0 == Energy.start_energy || (value < Energy.start_energy)) {
Energy.start_energy = value; // Init after restart and handle roll-over if any
RtcSettings.energy_kWhtotal = (unsigned long)(value * multiplier);
Energy.kWhtoday = 0;
}
else if (value != Energy.start_energy) {
Energy.kWhtoday += (unsigned long)((value - Energy.start_energy) * multiplier);
Energy.start_energy = value;
}
EnergyUpdateToday();
}
/*********************************************************************************************/ /*********************************************************************************************/
void Energy200ms(void) void Energy200ms(void)

View File

@ -181,12 +181,7 @@ void PzemEvery200ms(void)
Energy.active_power = value; Energy.active_power = value;
break; break;
case 4: // Total energy as 99999Wh case 4: // Total energy as 99999Wh
if (!Energy.start_energy || (value < Energy.start_energy)) Energy.start_energy = value; // Init after restart and hanlde roll-over if any EnergyUpdateTotal(value, false);
if (value != Energy.start_energy) {
Energy.kWhtoday += (unsigned long)((value - Energy.start_energy) * 100);
Energy.start_energy = value;
}
EnergyUpdateToday();
break; break;
} }
pzem_read_state++; pzem_read_state++;

View File

@ -82,12 +82,7 @@ void PzemAcEverySecond(void)
Energy.power_factor = (float)((buffer[19] << 8) + buffer[20]) / 100.0; // 1.00 Energy.power_factor = (float)((buffer[19] << 8) + buffer[20]) / 100.0; // 1.00
float energy = (float)((buffer[15] << 24) + (buffer[16] << 16) + (buffer[13] << 8) + buffer[14]); // 4294967295 Wh float energy = (float)((buffer[15] << 24) + (buffer[16] << 16) + (buffer[13] << 8) + buffer[14]); // 4294967295 Wh
if (!Energy.start_energy || (energy < Energy.start_energy)) { Energy.start_energy = energy; } // Init after restart and handle roll-over if any EnergyUpdateTotal(energy, false);
if (energy != Energy.start_energy) {
Energy.kWhtoday += (unsigned long)((energy - Energy.start_energy) * 100);
Energy.start_energy = energy;
}
EnergyUpdateToday();
// } // }
} }
} }

View File

@ -61,12 +61,7 @@ void PzemDcEverySecond(void)
Energy.active_power = (float)((buffer[9] << 24) + (buffer[10] << 16) + (buffer[7] << 8) + buffer[8]) / 10.0; // 429496729.0 W Energy.active_power = (float)((buffer[9] << 24) + (buffer[10] << 16) + (buffer[7] << 8) + buffer[8]) / 10.0; // 429496729.0 W
float energy = (float)((buffer[13] << 24) + (buffer[14] << 16) + (buffer[11] << 8) + buffer[12]); // 4294967295 Wh float energy = (float)((buffer[13] << 24) + (buffer[14] << 16) + (buffer[11] << 8) + buffer[12]); // 4294967295 Wh
if (!Energy.start_energy || (energy < Energy.start_energy)) { Energy.start_energy = energy; } // Init after restart and handle roll-over if any EnergyUpdateTotal(energy, false);
if (energy != Energy.start_energy) {
Energy.kWhtoday += (unsigned long)((energy - Energy.start_energy) * 100);
Energy.start_energy = energy;
}
EnergyUpdateToday();
} }
} }

View File

@ -94,7 +94,7 @@ void SDM120Every200ms(void)
Energy.data_valid = 0; Energy.data_valid = 0;
float value; float value;
((uint8_t*)&value)[3] = buffer[3]; // Get float values ((uint8_t*)&value)[3] = buffer[3]; // Get float values
((uint8_t*)&value)[2] = buffer[4]; ((uint8_t*)&value)[2] = buffer[4];
((uint8_t*)&value)[1] = buffer[5]; ((uint8_t*)&value)[1] = buffer[5];
((uint8_t*)&value)[0] = buffer[6]; ((uint8_t*)&value)[0] = buffer[6];
@ -129,14 +129,7 @@ void SDM120Every200ms(void)
break; break;
case 7: case 7:
if (!Energy.start_energy || (value < Energy.start_energy)) { // 484.708 kWh EnergyUpdateTotal(value, true); // 484.708 kWh
Energy.start_energy = value; // Init after restart and hanlde roll-over if any
}
if (value != Energy.start_energy) {
Energy.kWhtoday += (unsigned long)((value - Energy.start_energy) * 100000); // kWh to deca milli Wh
Energy.start_energy = value;
}
EnergyUpdateToday();
break; break;
#ifdef USE_SDM220 #ifdef USE_SDM220