Fix possible overflow situation

Fix possible overflow situation
This commit is contained in:
Theo Arends 2018-11-28 14:06:02 +01:00
parent 9738f640cf
commit e3bff56092
3 changed files with 14 additions and 8 deletions

View File

@ -181,8 +181,10 @@ void PzemEvery200ms(void)
break;
case 4: // Total energy as 99999Wh
if (!energy_start || (value < energy_start)) energy_start = value; // Init after restart and hanlde roll-over if any
energy_kWhtoday += (value - energy_start) * 100;
energy_start = value;
if (value != energy_start) {
energy_kWhtoday += (unsigned long)((value - energy_start) * 100);
energy_start = value;
}
EnergyUpdateToday();
break;
}

View File

@ -62,9 +62,11 @@ void PzemAcEverySecond(void)
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
if (!energy_start || (energy < energy_start)) { energy_start = energy; } // Init after restart and hanlde roll-over if any
energy_kWhtoday += (energy - energy_start) * 100;
energy_start = energy;
if (!energy_start || (energy < energy_start)) { energy_start = energy; } // Init after restart and handle roll-over if any
if (energy != energy_start) {
energy_kWhtoday += (unsigned long)((energy - energy_start) * 100);
energy_start = energy;
}
EnergyUpdateToday();
}
}

View File

@ -60,9 +60,11 @@ void PzemDcEverySecond(void)
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
if (!energy_start || (energy < energy_start)) { energy_start = energy; } // Init after restart and hanlde roll-over if any
energy_kWhtoday += (energy - energy_start) * 100;
energy_start = energy;
if (!energy_start || (energy < energy_start)) { energy_start = energy; } // Init after restart and handle roll-over if any
if (energy != energy_start) {
energy_kWhtoday += (unsigned long)((energy - energy_start) * 100);
energy_start = energy;
}
EnergyUpdateToday();
}
}