From 11934d88ae9d90c04c3b9270202208c4db780ddb Mon Sep 17 00:00:00 2001 From: Bruno Hoffmeister Date: Tue, 16 Apr 2024 23:28:23 -0300 Subject: [PATCH 1/4] Added Generation Energy Support --- src/cmnds/cmd_public.h | 1 + src/driver/drv_bl_shared.c | 33 +++++++++++++++++++++++++++------ src/driver/drv_public.h | 1 + src/hal/hal_flashVars.h | 3 ++- src/httpserver/json_interface.c | 3 +++ 5 files changed, 34 insertions(+), 7 deletions(-) diff --git a/src/cmnds/cmd_public.h b/src/cmnds/cmd_public.h index d95032f99..ef1d8bb5a 100644 --- a/src/cmnds/cmd_public.h +++ b/src/cmnds/cmd_public.h @@ -67,6 +67,7 @@ enum EventCode { CMD_EVENT_CHANGE_POWER, CMD_EVENT_CHANGE_CONSUMPTION_TOTAL, CMD_EVENT_CHANGE_CONSUMPTION_LAST_HOUR, + CMD_EVENT_CHANGE_GENERATION_TOTAL, // this is for ToggleChannelOnToggle CMD_EVENT_PIN_ONTOGGLE, diff --git a/src/driver/drv_bl_shared.c b/src/driver/drv_bl_shared.c index f711ed7ab..c1f53205f 100644 --- a/src/driver/drv_bl_shared.c +++ b/src/driver/drv_bl_shared.c @@ -43,6 +43,7 @@ struct { {{"apparent_power", "VA", "Apparent Power", "power_apparent", "9", }, 2, 0.25, }, // OBK_POWER_APPARENT {{"reactive_power", "var", "Reactive Power", "power_reactive", "10", }, 2, 0.25, }, // OBK_POWER_REACTIVE {{"power_factor", "", "Power Factor", "power_factor", "11", }, 2, 0.05, }, // OBK_POWER_FACTOR + {{"energy", UNIT_WH, "Total Generation", "energycounter_generation", "14", }, 3, 0.1, }, // OBK_GENERATION_TOTAL {{"energy", UNIT_WH, "Energy Total", "energycounter", "3", }, 3, 0.1, }, // OBK_CONSUMPTION_TOTAL {{"energy", UNIT_WH, "Energy Last Hour", "energycounter_last_hour", "4", }, 3, 0.1, }, // OBK_CONSUMPTION_LAST_HOUR //{{"", "", "Consumption Stats", "consumption_stats", "5", }, 0, 0, }, // OBK_CONSUMPTION_STATS @@ -105,13 +106,15 @@ void BL09XX_AppendInformationToHTTPIndexPage(http_request_t *request) } for (int i = OBK__FIRST; i <= OBK_CONSUMPTION__DAILY_LAST; i++) { + if (i == OBK_GENERATION_TOTAL && (!CFG_HasFlag(OBK_FLAG_POWER_ALLOW_NEGATIVE))){i++;} + if (i <= OBK__NUM_MEASUREMENTS || NTP_IsTimeSynced()) { poststr(request, ""); poststr(request, sensors[i].names.name_friendly); poststr(request, ""); hprintf255(request, "%.*f%s", sensors[i].rounding_decimals, - (i == OBK_CONSUMPTION_TOTAL ? 0.001 : 1) * sensors[i].lastReading, //always display OBK_CONSUMPTION_TOTAL in kwh - i == OBK_CONSUMPTION_TOTAL ? "kWh": sensors[i].names.units); + ((i == OBK_CONSUMPTION_TOTAL || i == OBK_GENERATION_TOTAL) ? 0.001 : 1) * sensors[i].lastReading, //always display OBK_CONSUMPTION_TOTAL and OBK_GENERATION_TOTAL in kwh + (i == OBK_CONSUMPTION_TOTAL || i == OBK_GENERATION_TOTAL) ? "kWh": sensors[i].names.units); } }; @@ -181,6 +184,7 @@ void BL09XX_SaveEmeteringStatistics() memset(&data, 0, sizeof(ENERGY_METERING_DATA)); data.TotalConsumption = sensors[OBK_CONSUMPTION_TOTAL].lastReading; + data.TotalGeneration = sensors[OBK_GENERATION_TOTAL].lastReading; data.TodayConsumpion = sensors[OBK_CONSUMPTION_TODAY].lastReading; data.YesterdayConsumption = sensors[OBK_CONSUMPTION_YESTERDAY].lastReading; data.actual_mday = actual_mday; @@ -201,6 +205,7 @@ commandResult_t BL09XX_ResetEnergyCounter(const void *context, const char *cmd, if(args==0||*args==0) { sensors[OBK_CONSUMPTION_TOTAL].lastReading = 0.0; + sensors[OBK_GENERATION_TOTAL].lastReading = 0.0; energyCounterStamp = xTaskGetTickCount(); if (energyCounterStatsEnable == true) { @@ -507,21 +512,34 @@ void BL_ProcessUpdate(float voltage, float current, float power, lastReadingFrequency = frequency; float energy = 0; + float generation = 0; if (isnan(energyWh)) { xPassedTicks = (int)(xTaskGetTickCount() - energyCounterStamp); // FIXME: Wrong calculation if tick count overflows if (xPassedTicks <= 0) xPassedTicks = 1; energy = xPassedTicks * power / (3600000.0f / portTICK_PERIOD_MS); - } else + } else { energy = energyWh; - - if (energy < 0) - energy = 0.0; + } + + if(energy < 0) { + if(CFG_HasFlag(OBK_FLAG_POWER_ALLOW_NEGATIVE)) { + // If energy is negative, account as generation + generation = (energy*-1); + } else { + // If negative flag is disable, make sure that energy is positive + energy = 0.0; + } + } sensors[OBK_CONSUMPTION_TOTAL].lastReading += (double)energy; + sensors[OBK_GENERATION_TOTAL].lastReading += (double)generation; + + energyCounterStamp = xTaskGetTickCount(); HAL_FlashVars_SaveTotalConsumption(sensors[OBK_CONSUMPTION_TOTAL].lastReading); + HAL_FlashVars_SaveTotalConsumption(sensors[OBK_GENERATION_TOTAL].lastReading); sensors[OBK_CONSUMPTION_TODAY].lastReading += energy; if (NTP_IsTimeSynced()) { @@ -577,6 +595,7 @@ void BL_ProcessUpdate(float voltage, float current, float power, root = cJSON_CreateObject(); cJSON_AddNumberToObject(root, "uptime", g_secondsElapsed); cJSON_AddNumberToObject(root, "consumption_total", BL_ChangeEnergyUnitIfNeeded(DRV_GetReading(OBK_CONSUMPTION_TOTAL))); + cJSON_AddNumberToObject(root, "consumption_generation", BL_ChangeEnergyUnitIfNeeded(DRV_GetReading(OBK_GENERATION_TOTAL))); cJSON_AddNumberToObject(root, "consumption_last_hour", BL_ChangeEnergyUnitIfNeeded(DRV_GetReading(OBK_CONSUMPTION_LAST_HOUR))); cJSON_AddNumberToObject(root, "consumption_stat_index", energyCounterMinutesIndex); cJSON_AddNumberToObject(root, "consumption_sample_count", energyCounterSampleCount); @@ -673,6 +692,7 @@ void BL_ProcessUpdate(float voltage, float current, float power, case OBK_CURRENT: eventChangeCode = CMD_EVENT_CHANGE_CURRENT; break; case OBK_POWER: eventChangeCode = CMD_EVENT_CHANGE_POWER; break; case OBK_CONSUMPTION_TOTAL: eventChangeCode = CMD_EVENT_CHANGE_CONSUMPTION_TOTAL; break; + case OBK_GENERATION_TOTAL: eventChangeCode = CMD_EVENT_CHANGE_GENERATION_TOTAL; break; case OBK_CONSUMPTION_LAST_HOUR: eventChangeCode = CMD_EVENT_CHANGE_CONSUMPTION_LAST_HOUR; break; default: eventChangeCode = CMD_EVENT_NONE; break; } @@ -772,6 +792,7 @@ void BL_Shared_Init(void) HAL_GetEnergyMeterStatus(&data); sensors[OBK_CONSUMPTION_TOTAL].lastReading = data.TotalConsumption; + sensors[OBK_GENERATION_TOTAL].lastReading = data.TotalGeneration; sensors[OBK_CONSUMPTION_TODAY].lastReading = data.TodayConsumpion; sensors[OBK_CONSUMPTION_YESTERDAY].lastReading = data.YesterdayConsumption; actual_mday = data.actual_mday; diff --git a/src/driver/drv_public.h b/src/driver/drv_public.h index 8f6d82d4e..75bfadb32 100644 --- a/src/driver/drv_public.h +++ b/src/driver/drv_public.h @@ -11,6 +11,7 @@ typedef enum energySensor_e { OBK_POWER_APPARENT, OBK_POWER_REACTIVE, OBK_POWER_FACTOR, + OBK_GENERATION_TOTAL, OBK_CONSUMPTION_TOTAL, OBK__NUM_MEASUREMENTS = OBK_CONSUMPTION_TOTAL, diff --git a/src/hal/hal_flashVars.h b/src/hal/hal_flashVars.h index 8c64e7b40..3b6808b70 100644 --- a/src/hal/hal_flashVars.h +++ b/src/hal/hal_flashVars.h @@ -5,10 +5,11 @@ #include "../new_common.h" #define BOOT_COMPLETE_SECONDS 30 -#define MAX_RETAIN_CHANNELS 12 +#define MAX_RETAIN_CHANNELS 13 /* Fixed size 32 bytes */ typedef struct ENERGY_METERING_DATA { + float TotalGeneration; float TotalConsumption; float TodayConsumpion; float YesterdayConsumption; diff --git a/src/httpserver/json_interface.c b/src/httpserver/json_interface.c index 4d8513fb8..38b0746ca 100644 --- a/src/httpserver/json_interface.c +++ b/src/httpserver/json_interface.c @@ -224,6 +224,9 @@ static int http_tasmota_json_ENERGY(void* request, jsonCb_t printer) { printer(request, "\"ConsumptionTotal\":%f,", _getReading_NanToZero(OBK_CONSUMPTION_TOTAL)); printer(request, "\"Yesterday\": %f,", _getReading_NanToZero(OBK_CONSUMPTION_YESTERDAY)); printer(request, "\"ConsumptionLastHour\":%f", _getReading_NanToZero(OBK_CONSUMPTION_LAST_HOUR)); + if(CFG_HasFlag(OBK_FLAG_POWER_ALLOW_NEGATIVE)) { + printer(request, "\"GenerationtionTotal\":%f,", _getReading_NanToZero(OBK_GENERATION_TOTAL)); + } // close ENERGY block printer(request, "}"); } From 32804577d8e1eaa9127a65fc4655565743b5d5e9 Mon Sep 17 00:00:00 2001 From: Bruno Hoffmeister Date: Wed, 17 Apr 2024 17:22:21 -0300 Subject: [PATCH 2/4] Fix MAX_RETAIN_CHANNELS var value --- src/hal/hal_flashVars.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hal/hal_flashVars.h b/src/hal/hal_flashVars.h index 3b6808b70..d65e1cd2f 100644 --- a/src/hal/hal_flashVars.h +++ b/src/hal/hal_flashVars.h @@ -5,7 +5,7 @@ #include "../new_common.h" #define BOOT_COMPLETE_SECONDS 30 -#define MAX_RETAIN_CHANNELS 13 +#define MAX_RETAIN_CHANNELS 12 /* Fixed size 32 bytes */ typedef struct ENERGY_METERING_DATA { From c246e797823db72d6ebf0ef8b7360bf8805f91b9 Mon Sep 17 00:00:00 2001 From: Bruno Hoffmeister Date: Thu, 18 Apr 2024 10:30:07 -0300 Subject: [PATCH 3/4] Fix reading negative values --- src/driver/drv_bl_shared.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/driver/drv_bl_shared.c b/src/driver/drv_bl_shared.c index c1f53205f..55f4c97de 100644 --- a/src/driver/drv_bl_shared.c +++ b/src/driver/drv_bl_shared.c @@ -523,10 +523,11 @@ void BL_ProcessUpdate(float voltage, float current, float power, energy = energyWh; } - if(energy < 0) { + if(power < 0) { if(CFG_HasFlag(OBK_FLAG_POWER_ALLOW_NEGATIVE)) { // If energy is negative, account as generation - generation = (energy*-1); + generation = energy; + energy = 0.0; } else { // If negative flag is disable, make sure that energy is positive energy = 0.0; From 9e97227509015c2336f5a641fb6a49e99ac61df1 Mon Sep 17 00:00:00 2001 From: Bruno Hoffmeister Date: Thu, 18 Apr 2024 17:14:31 -0300 Subject: [PATCH 4/4] Fix ENERGY_METERING_DATA order to minimize breaking compatibility --- src/hal/hal_flashVars.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hal/hal_flashVars.h b/src/hal/hal_flashVars.h index d65e1cd2f..d74227161 100644 --- a/src/hal/hal_flashVars.h +++ b/src/hal/hal_flashVars.h @@ -9,7 +9,6 @@ /* Fixed size 32 bytes */ typedef struct ENERGY_METERING_DATA { - float TotalGeneration; float TotalConsumption; float TodayConsumpion; float YesterdayConsumption; @@ -18,6 +17,7 @@ typedef struct ENERGY_METERING_DATA { time_t ConsumptionResetTime; unsigned char reseved[3]; char actual_mday; + float TotalGeneration; } ENERGY_METERING_DATA; typedef struct flash_vars_structure