From c86a9f8e5321f86dc60a2370f6ecc59c431afb30 Mon Sep 17 00:00:00 2001 From: Theo Arends Date: Sun, 29 Apr 2018 15:29:43 +0200 Subject: [PATCH] Fix Energy Today and Yesterday overflow 5.12.0o * Fix Energy Today and Yesterday overflow (#2543) --- README.md | 2 +- sonoff/_releasenotes.ino | 5 ++++- sonoff/settings.ino | 6 +++++- sonoff/sonoff.ino | 2 +- sonoff/xdrv_03_energy.ino | 35 ++++++++++++++++++----------------- 5 files changed, 29 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 8d55383d2ae6..f4b8697518f5 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ If you like **Sonoff-Tasmota**, give it a star, or fork it and contribute! ### Development: [![Build Status](https://img.shields.io/travis/arendst/Sonoff-Tasmota.svg)](https://travis-ci.org/arendst/Sonoff-Tasmota) -Current version is **5.12.0n** - See [sonoff/_releasenotes.ino](https://github.com/arendst/Sonoff-Tasmota/blob/development/sonoff/_releasenotes.ino) for change information. +Current version is **5.12.0o** - See [sonoff/_releasenotes.ino](https://github.com/arendst/Sonoff-Tasmota/blob/development/sonoff/_releasenotes.ino) for change information. ### Quick install Download one of the released binaries from https://github.com/arendst/Sonoff-Tasmota/releases and flash it to your hardware as documented in the wiki. diff --git a/sonoff/_releasenotes.ino b/sonoff/_releasenotes.ino index d7bed584e7d5..804fc4068e3f 100644 --- a/sonoff/_releasenotes.ino +++ b/sonoff/_releasenotes.ino @@ -1,4 +1,7 @@ -/* 5.12.0n +/* 5.12.0o + * Fix Energy Today and Yesterday overflow (#2543) + * + * 5.12.0n * Change ESP8266 Analog JSON message from {"Analog0:123"} to {"ANALOG":{"A0:123"}} to accomodate rules (#2560) * Change Counter JSON message from {"Counter1":0,"Counter3":0} to {"COUNTER":{"C1":0,"C3":0}} to accomodate rules * Change ADS1115 JSON message from {"ADS1115":{"Analog0":123,"Analog1":123}} to {"ADS1115":{"A0":123,"A1":123}} diff --git a/sonoff/settings.ino b/sonoff/settings.ino index 579862457c29..059383d72f40 100644 --- a/sonoff/settings.ino +++ b/sonoff/settings.ino @@ -851,7 +851,11 @@ void SettingsDelta() Settings.knx_CB_registered = 0; memset(&Settings.knx_physsical_addr, 0x00, 0x800 - 0x6b8); // Reset until 0x800 for future use } - + if (Settings.version < 0x050C000F) { + Settings.energy_kWhtoday /= 1000; + Settings.energy_kWhyesterday /= 1000; + RtcSettings.energy_kWhtoday /= 1000; + } Settings.version = VERSION; SettingsSave(1); } diff --git a/sonoff/sonoff.ino b/sonoff/sonoff.ino index 2da5379830f8..ef8708617ce7 100644 --- a/sonoff/sonoff.ino +++ b/sonoff/sonoff.ino @@ -25,7 +25,7 @@ - Select IDE Tools - Flash Size: "1M (no SPIFFS)" ====================================================*/ -#define VERSION 0x050C000E // 5.12.0n +#define VERSION 0x050C000F // 5.12.0o // Location specific includes #include // Arduino_Esp8266 version information (ARDUINO_ESP8266_RELEASE and ARDUINO_ESP8266_RELEASE_2_3_0) diff --git a/sonoff/xdrv_03_energy.ino b/sonoff/xdrv_03_energy.ino index fc70751bfd78..db43cc670588 100644 --- a/sonoff/xdrv_03_energy.ino +++ b/sonoff/xdrv_03_energy.ino @@ -47,11 +47,12 @@ float energy_voltage = 0; // 123.1 V float energy_current = 0; // 123.123 A float energy_power = 0; // 123.1 W float energy_power_factor = 0; // 0.12 -float energy_daily = 0; // 12.123 kWh +float energy_daily = 0; // 123.123 kWh float energy_total = 0; // 12345.12345 kWh float energy_start = 0; // 12345.12345 kWh total previous -unsigned long energy_kWhtoday; // 1212312345 Wh * 10^-5 (deca micro Watt hours) - 5763924 = 0.05763924 kWh = 0.058 kWh = energy_daily -unsigned long energy_period = 0; // 1212312345 Wh * 10^-5 (deca micro Watt hours) - 5763924 = 0.05763924 kWh = 0.058 kWh = energy_daily +unsigned long energy_kWhtoday; // 12312312 Wh * 10^-2 (deca milli Watt hours) - 5764 = 0.05764 kWh = 0.058 kWh = energy_daily +unsigned long energy_period = 0; // 12312312 Wh * 10^-2 (deca milli Watt hours) - 5764 = 0.05764 kWh = 0.058 kWh = energy_daily + float energy_power_last[3] = { 0 }; uint8_t energy_power_delta = 0; @@ -82,8 +83,8 @@ Ticker ticker_energy; void EnergyUpdateToday() { RtcSettings.energy_kWhtoday = energy_kWhtoday; - energy_daily = (float)energy_kWhtoday / 100000000; - energy_total = (float)(RtcSettings.energy_kWhtotal + (energy_kWhtoday / 1000)) / 100000; + energy_daily = (float)energy_kWhtoday / 100000; + energy_total = (float)(RtcSettings.energy_kWhtotal + energy_kWhtoday) / 100000; } /*********************************************************************************************\ @@ -156,7 +157,7 @@ void HlwEverySecond() hlw_len = 10000 / hlw_energy_period_counter; hlw_energy_period_counter = 0; if (hlw_len) { - energy_kWhtoday += ((HLW_PREF * Settings.energy_power_calibration) / hlw_len) / 36; + energy_kWhtoday += ((HLW_PREF * Settings.energy_power_calibration) / hlw_len) / 36000; EnergyUpdateToday(); } } @@ -390,7 +391,7 @@ void CseEverySecond() } if (cf_frequency && energy_power) { cf_pulses_last_time = cf_pulses; - energy_kWhtoday += (cf_frequency * Settings.energy_power_calibration) / 36; + energy_kWhtoday += (cf_frequency * Settings.energy_power_calibration) / 36000; EnergyUpdateToday(); } } @@ -539,7 +540,7 @@ void PzemEvery200ms() 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) * 100000; + energy_kWhtoday += (value - energy_start) * 100; energy_start = value; EnergyUpdateToday(); break; @@ -579,7 +580,7 @@ void Energy200ms() if (RtcTime.valid) { if (LocalTime() == Midnight()) { Settings.energy_kWhyesterday = energy_kWhtoday; - Settings.energy_kWhtotal += (energy_kWhtoday / 1000); + Settings.energy_kWhtotal += energy_kWhtoday; RtcSettings.energy_kWhtotal = Settings.energy_kWhtotal; energy_kWhtoday = 0; energy_period = energy_kWhtoday; @@ -851,14 +852,14 @@ boolean EnergyCommand() if (p != XdrvMailbox.data) { switch (XdrvMailbox.index) { case 1: - energy_kWhtoday = lnum *100000; + energy_kWhtoday = lnum *100; energy_period = energy_kWhtoday; Settings.energy_kWhtoday = energy_kWhtoday; RtcSettings.energy_kWhtoday = energy_kWhtoday; - energy_daily = (float)energy_kWhtoday / 100000000; + energy_daily = (float)energy_kWhtoday / 100000; break; case 2: - Settings.energy_kWhyesterday = lnum *100000; + Settings.energy_kWhyesterday = lnum *100; break; case 3: RtcSettings.energy_kWhtotal = lnum *100; @@ -869,9 +870,9 @@ boolean EnergyCommand() char energy_yesterday_chr[10]; char stoday_energy[10]; char energy_total_chr[10]; - dtostrfd((float)Settings.energy_kWhyesterday / 100000000, Settings.flag2.energy_resolution, energy_yesterday_chr); - dtostrfd((float)RtcSettings.energy_kWhtoday / 100000000, Settings.flag2.energy_resolution, stoday_energy); - dtostrfd((float)(RtcSettings.energy_kWhtotal + (energy_kWhtoday / 1000)) / 100000, Settings.flag2.energy_resolution, energy_total_chr); + dtostrfd((float)Settings.energy_kWhyesterday / 100000, Settings.flag2.energy_resolution, energy_yesterday_chr); + dtostrfd((float)RtcSettings.energy_kWhtoday / 100000, Settings.flag2.energy_resolution, stoday_energy); + dtostrfd((float)(RtcSettings.energy_kWhtotal + energy_kWhtoday) / 100000, Settings.flag2.energy_resolution, energy_total_chr); snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"%s\":{\"" D_JSON_TOTAL "\":%s,\"" D_JSON_YESTERDAY "\":%s,\"" D_JSON_TODAY "\":%s}}"), command, energy_total_chr, energy_yesterday_chr, stoday_energy); status_flag = 1; @@ -1073,7 +1074,7 @@ void EnergyShow(boolean json) float energy = 0; if (show_energy_period) { - if (energy_period) energy = (float)(energy_kWhtoday - energy_period) / 100000; + if (energy_period) energy = (float)(energy_kWhtoday - energy_period) / 100; energy_period = energy_kWhtoday; } @@ -1084,7 +1085,7 @@ void EnergyShow(boolean json) dtostrfd(energy_voltage, Settings.flag2.voltage_resolution, energy_voltage_chr); dtostrfd(energy_current, Settings.flag2.current_resolution, energy_current_chr); dtostrfd(energy_power_factor, 2, energy_power_factor_chr); - dtostrfd((float)Settings.energy_kWhyesterday / 100000000, Settings.flag2.energy_resolution, energy_yesterday_chr); + dtostrfd((float)Settings.energy_kWhyesterday / 100000, Settings.flag2.energy_resolution, energy_yesterday_chr); if (json) { snprintf_P(speriod, sizeof(speriod), PSTR(",\"" D_JSON_PERIOD "\":%s"), energy_period_chr);