From e3bff56092d2a3e0a30a49d696e7d951b6a86602 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Wed, 28 Nov 2018 14:06:02 +0100 Subject: [PATCH] Fix possible overflow situation Fix possible overflow situation --- sonoff/xnrg_03_pzem004t.ino | 6 ++++-- sonoff/xnrg_05_pzem_ac.ino | 8 +++++--- sonoff/xnrg_06_pzem_dc.ino | 8 +++++--- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/sonoff/xnrg_03_pzem004t.ino b/sonoff/xnrg_03_pzem004t.ino index d30b769c2a56..850131762cf9 100644 --- a/sonoff/xnrg_03_pzem004t.ino +++ b/sonoff/xnrg_03_pzem004t.ino @@ -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; } diff --git a/sonoff/xnrg_05_pzem_ac.ino b/sonoff/xnrg_05_pzem_ac.ino index 6bbabdaa9520..7c145384ae89 100644 --- a/sonoff/xnrg_05_pzem_ac.ino +++ b/sonoff/xnrg_05_pzem_ac.ino @@ -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(); } } diff --git a/sonoff/xnrg_06_pzem_dc.ino b/sonoff/xnrg_06_pzem_dc.ino index 3dcaf8332a12..c12f401d4571 100644 --- a/sonoff/xnrg_06_pzem_dc.ino +++ b/sonoff/xnrg_06_pzem_dc.ino @@ -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(); } }