From 92b9b7bf717959a78a346017bb0c61ad3b13ceea Mon Sep 17 00:00:00 2001 From: Daniel Siegmanski Date: Sun, 24 Nov 2024 14:38:15 +0100 Subject: [PATCH 01/17] Added a missing comma so that the plugin can also be built with activated PLUGIN_076_DEBUG. --- src/_P076_HLW8012.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/_P076_HLW8012.ino b/src/_P076_HLW8012.ino index eacb3bd6d4..da1b0be41f 100644 --- a/src/_P076_HLW8012.ino +++ b/src/_P076_HLW8012.ino @@ -305,7 +305,7 @@ boolean Plugin_076(uint8_t function, struct EventStruct *event, String& string) } # if PLUGIN_076_DEBUG - addLogMove(LOG_LEVEL_INFO, strformat(F("P076: PIN Settings curr_read: %d cf_edge: %d cf1_edge: %d") + addLogMove(LOG_LEVEL_INFO, strformat(F("P076: PIN Settings curr_read: %d cf_edge: %d cf1_edge: %d"), PCONFIG(4), PCONFIG(5), PCONFIG(6))); # endif // if PLUGIN_076_DEBUG From 10d6981c9861a3fc9b381396b84644f8ff0d652d Mon Sep 17 00:00:00 2001 From: Daniel Siegmanski Date: Sun, 24 Nov 2024 14:39:49 +0100 Subject: [PATCH 02/17] Added the ability to read reactive power, apparent power and energy. --- src/_P076_HLW8012.ino | 150 +++++++++++++++++++++++++++++++++++------- 1 file changed, 128 insertions(+), 22 deletions(-) diff --git a/src/_P076_HLW8012.ino b/src/_P076_HLW8012.ino index da1b0be41f..9d1e928070 100644 --- a/src/_P076_HLW8012.ino +++ b/src/_P076_HLW8012.ino @@ -31,10 +31,6 @@ HLW8012 *Plugin_076_hlw = nullptr; # define PLUGIN_ID_076 76 # define PLUGIN_076_DEBUG false // activate extra log info in the debug # define PLUGIN_NAME_076 "Energy (AC) - HLW8012/BL0937" -# define PLUGIN_VALUENAME1_076 "Voltage" -# define PLUGIN_VALUENAME2_076 "Current" -# define PLUGIN_VALUENAME3_076 "Power" -# define PLUGIN_VALUENAME4_076 "PowerFactor" # define HLW_DELAYREADING 500 @@ -49,8 +45,23 @@ unsigned long p076_timer{}; float p076_hcurrent{}; float p076_hvoltage{}; -float p076_hpower{}; +float p076_hpowerActive{}; +float p076_hpowerReactive{}; +float p076_hpowerApparent{}; float p076_hpowfact{}; +float p076_henergy{}; + +# define P076_NR_OUTPUT_VALUES 4 +# define P076_NR_OUTPUT_OPTIONS 7 +# define P076_QUERY1_CONFIG_POS 1 +# define P076_QUERY1 PCONFIG(1) +# define P076_QUERY2 PCONFIG(2) +# define P076_QUERY3 PCONFIG(3) +# define P076_QUERY4 PCONFIG(4) +# define P076_QUERY1_DFLT 0 // Voltage (V) +# define P076_QUERY2_DFLT 1 // Current (A) +# define P076_QUERY3_DFLT 2 // Active Power (W) +# define P076_QUERY4_DFLT 6 // Energy (Ws) # define P076_Custom 0 @@ -68,6 +79,9 @@ float p076_hpowfact{}; # define P076_Gosund 9 # define P076_Shelly_PLUG_S 10 +// Forward declaration helper function +const __FlashStringHelper* p076_getQueryString(uint8_t query); + # if ESP_IDF_VERSION_MAJOR >= 5 // FIXME TD-er: Must check if older (and ESP8266) envs need IRAM_ATTR in the function declaration. @@ -119,6 +133,7 @@ boolean Plugin_076(uint8_t function, struct EventStruct *event, String& string) Device[deviceCount].TimerOption = true; Device[deviceCount].PluginStats = true; Device[deviceCount].setPin1Direction(gpio_direction::gpio_output); + Device[deviceCount].OutputDataType = Output_Data_type_t::Simple; break; } @@ -128,17 +143,28 @@ boolean Plugin_076(uint8_t function, struct EventStruct *event, String& string) } case PLUGIN_GET_DEVICEVALUENAMES: { - strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[0], - PSTR(PLUGIN_VALUENAME1_076)); - strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[1], - PSTR(PLUGIN_VALUENAME2_076)); - strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[2], - PSTR(PLUGIN_VALUENAME3_076)); - strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[3], - PSTR(PLUGIN_VALUENAME4_076)); + for (uint8_t i = 0; i < VARS_PER_TASK; ++i) { + if (i < P076_NR_OUTPUT_VALUES) { + uint8_t choice = PCONFIG(i + P076_QUERY1_CONFIG_POS); + ExtraTaskSettings.setTaskDeviceValueName(i, p076_getQueryString(choice)); + } else { + ExtraTaskSettings.clearTaskDeviceValueName(i); + } + } break; } + case PLUGIN_SET_DEFAULTS: { + // Load some defaults + P076_QUERY1 = P076_QUERY1_DFLT; + P076_QUERY2 = P076_QUERY2_DFLT; + P076_QUERY3 = P076_QUERY3_DFLT; + P076_QUERY4 = P076_QUERY4_DFLT; + + success = true; + break; + } + case PLUGIN_GET_DEVICEGPIONAMES: { event->String1 = formatGpioName_output(F("SEL")); event->String2 = formatGpioName_input(F("CF1")); @@ -146,6 +172,24 @@ boolean Plugin_076(uint8_t function, struct EventStruct *event, String& string) break; } + case PLUGIN_WEBFORM_LOAD_OUTPUT_SELECTOR: { + // To select the data in the 4 fields. + const __FlashStringHelper *options[P076_NR_OUTPUT_OPTIONS]; + + for (uint8_t i = 0; i < P076_NR_OUTPUT_OPTIONS; ++i) { + options[i] = p076_getQueryString(i); + } + + for (uint8_t i = 0; i < P076_NR_OUTPUT_VALUES; ++i) { + const uint8_t pconfigIndex = i + P076_QUERY1_CONFIG_POS; + sensorTypeHelper_loadOutputSelector(event, pconfigIndex, i, P076_NR_OUTPUT_OPTIONS, options); + } + + success = true; + + break; + } + case PLUGIN_WEBFORM_LOAD: { uint8_t devicePinSettings = PCONFIG(7); @@ -309,6 +353,13 @@ boolean Plugin_076(uint8_t function, struct EventStruct *event, String& string) PCONFIG(4), PCONFIG(5), PCONFIG(6))); # endif // if PLUGIN_076_DEBUG + // Save output selector parameters. + for (uint8_t i = 0; i < P076_NR_OUTPUT_VALUES; ++i) { + const uint8_t pconfigIndex = i + P076_QUERY1_CONFIG_POS; + const uint8_t choice = PCONFIG(pconfigIndex); + sensorTypeHelper_saveOutputSelector(event, pconfigIndex, i, p076_getQueryString(choice)); + } + success = true; break; } @@ -340,8 +391,11 @@ boolean Plugin_076(uint8_t function, struct EventStruct *event, String& string) if (timeOutReached(p076_timer)) { p076_hvoltage = Plugin_076_hlw->getVoltage(valid); - p076_hpower = Plugin_076_hlw->getActivePower(valid); + p076_hpowerActive = Plugin_076_hlw->getActivePower(valid); + p076_hpowerReactive = Plugin_076_hlw->getReactivePower(valid); + p076_hpowerApparent = Plugin_076_hlw->getApparentPower(valid); p076_hpowfact = static_cast(100 * Plugin_076_hlw->getPowerFactor(valid)); + p076_henergy = Plugin_076_hlw->getEnergy(); ++p076_read_stage; // Measurement is done, schedule a new PLUGIN_READ call @@ -364,7 +418,7 @@ boolean Plugin_076(uint8_t function, struct EventStruct *event, String& string) // ++p076_read_stage; // } else if (p076_read_stage > 3) { bool valid = false; - p076_hpower = Plugin_076_hlw->getActivePower(valid); + p076_hpowerActive = Plugin_076_hlw->getActivePower(valid); if (valid) { success = true; @@ -386,18 +440,41 @@ boolean Plugin_076(uint8_t function, struct EventStruct *event, String& string) success = true; } - UserVar.setFloat(event->TaskIndex, 0, p076_hvoltage); - UserVar.setFloat(event->TaskIndex, 1, p076_hcurrent); - UserVar.setFloat(event->TaskIndex, 2, p076_hpower); - UserVar.setFloat(event->TaskIndex, 3, p076_hpowfact); + p076_hpowerReactive = Plugin_076_hlw->getReactivePower(valid); + + if (valid) { + success = true; + } + + p076_hpowerApparent = Plugin_076_hlw->getApparentPower(valid); + + if (valid) { + success = true; + } + + p076_henergy = Plugin_076_hlw->getEnergy(); + + float HLW[7]; + HLW[0] = p076_hvoltage; + HLW[1] = p076_hcurrent; + HLW[2] = p076_hpowerActive; + HLW[3] = p076_hpowerReactive; + HLW[4] = p076_hpowerApparent; + HLW[5] = p076_hpowfact; + HLW[6] = p076_henergy; + + UserVar.setFloat(event->TaskIndex, 0, HLW[P076_QUERY1]); + UserVar.setFloat(event->TaskIndex, 1, HLW[P076_QUERY2]); + UserVar.setFloat(event->TaskIndex, 2, HLW[P076_QUERY3]); + UserVar.setFloat(event->TaskIndex, 3, HLW[P076_QUERY4]); // Measurement is complete. p076_read_stage = 0; # if PLUGIN_076_DEBUG addLogMove(LOG_LEVEL_INFO, - strformat(F("P076: Read values - V=%.2f - A=%.2f - W=%.2f - Pf%%=%.2f"), - p076_hvoltage, p076_hcurrent, p076_hpower, p076_hpowfact)); + strformat(F("P076: Read values - V=%.2f - A=%.2f - W=%.2f - VAR=%.2f - VA=%.2f - Pf%%=%.2f - Ws=%.2f"), + p076_hvoltage, p076_hcurrent, p076_hpowerActive, p076_hpowerReactive, p076_hpowerApparent, p076_hpowfact, p076_henergy)); # endif // if PLUGIN_076_DEBUG // Plugin_076_hlw->toggleMode(); @@ -522,12 +599,24 @@ boolean Plugin_076(uint8_t function, struct EventStruct *event, String& string) } success = true; } + + if (equals(command, F("hwlresetenergy"))) { + Plugin076_ResetEnergy(); + success = true; + } } break; } return success; } +void Plugin076_ResetEnergy() { + Plugin_076_hlw -> resetEnergy(); + # if PLUGIN_076_DEBUG + addLog(LOG_LEVEL_INFO, F("P076: Reset Energy counter to zero")); + # endif // if PLUGIN_076_DEBUG +} + void Plugin076_ResetMultipliers() { if (Plugin_076_hlw) { Plugin_076_hlw->resetMultipliers(); @@ -617,8 +706,11 @@ void Plugin076_Reset(taskIndex_t TaskIndex) { p076_hcurrent = 0.0f; p076_hvoltage = 0.0f; - p076_hpower = 0.0f; + p076_hpowerActive = 0.0f; + p076_hpowerReactive = 0.0f; + p076_hpowerApparent = 0.0f; p076_hpowfact = 0.0f; + p076_henergy = 0.0f; } // When using interrupts we have to call the library entry point @@ -635,4 +727,18 @@ void IRAM_ATTR p076_hlw8012_cf_interrupt() { } } +const __FlashStringHelper* p076_getQueryString(uint8_t query) { + switch (query) + { + case 0: return F("Voltage_V"); + case 1: return F("Current_A"); + case 2: return F("Active Power_W"); + case 3: return F("Reactive Power_VAR"); + case 4: return F("Apparent Power_VA"); + case 5: return F("Power_Factor_cosphi"); + case 6: return F("Energy_Ws"); + } + return F(""); +} + #endif // USES_P076 From 0c7cdb747b44da51c5a959d5bb5a8a1651260ff3 Mon Sep 17 00:00:00 2001 From: Daniel Siegmanski Date: Sun, 24 Nov 2024 14:44:17 +0100 Subject: [PATCH 03/17] Fixed typo in reset command. --- src/_P076_HLW8012.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/_P076_HLW8012.ino b/src/_P076_HLW8012.ino index 9d1e928070..f02dbe6b76 100644 --- a/src/_P076_HLW8012.ino +++ b/src/_P076_HLW8012.ino @@ -600,7 +600,7 @@ boolean Plugin_076(uint8_t function, struct EventStruct *event, String& string) success = true; } - if (equals(command, F("hwlresetenergy"))) { + if (equals(command, F("hlwresetenergy"))) { Plugin076_ResetEnergy(); success = true; } From 48785f05156f60b65a9318debdc24cf709e2a608 Mon Sep 17 00:00:00 2001 From: Daniel Siegmanski Date: Sun, 24 Nov 2024 15:20:32 +0100 Subject: [PATCH 04/17] Changed the PCONIFG values from 1,2,3,4 to 8,9,10,11 --- src/_P076_HLW8012.ino | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/_P076_HLW8012.ino b/src/_P076_HLW8012.ino index f02dbe6b76..4a5ecdc101 100644 --- a/src/_P076_HLW8012.ino +++ b/src/_P076_HLW8012.ino @@ -54,10 +54,10 @@ float p076_henergy{}; # define P076_NR_OUTPUT_VALUES 4 # define P076_NR_OUTPUT_OPTIONS 7 # define P076_QUERY1_CONFIG_POS 1 -# define P076_QUERY1 PCONFIG(1) -# define P076_QUERY2 PCONFIG(2) -# define P076_QUERY3 PCONFIG(3) -# define P076_QUERY4 PCONFIG(4) +# define P076_QUERY1 PCONFIG(8) +# define P076_QUERY2 PCONFIG(9) +# define P076_QUERY3 PCONFIG(10) +# define P076_QUERY4 PCONFIG(11) # define P076_QUERY1_DFLT 0 // Voltage (V) # define P076_QUERY2_DFLT 1 // Current (A) # define P076_QUERY3_DFLT 2 // Active Power (W) From d336bc33673444da839092525b8b28ca61b7b5b6 Mon Sep 17 00:00:00 2001 From: Daniel Siegmanski Date: Sun, 24 Nov 2024 15:27:36 +0100 Subject: [PATCH 05/17] Set default querys in PLUGIN_INIT if not already set. --- src/_P076_HLW8012.ino | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/_P076_HLW8012.ino b/src/_P076_HLW8012.ino index 4a5ecdc101..7323afda8a 100644 --- a/src/_P076_HLW8012.ino +++ b/src/_P076_HLW8012.ino @@ -544,6 +544,20 @@ boolean Plugin_076(uint8_t function, struct EventStruct *event, String& string) // Need a few seconds to read the first sample, so trigger a new read a few seconds after init. Scheduler.schedule_task_device_timer(event->TaskIndex, millis() + 5000); + + // Set default querys if not set + if ( P076_QUERY1 = 0 ) { + P076_QUERY1 = P076_QUERY1_DFLT; + } + if ( P076_QUERY2 = 0 ) { + P076_QUERY2 = P076_QUERY2_DFLT; + } + if ( P076_QUERY3 = 0 ) { + P076_QUERY3 = P076_QUERY3_DFLT; + } + if ( P076_QUERY4 = 0 ) { + P076_QUERY4 = P076_QUERY4_DFLT; + } success = true; } } From 1631e1d77d79d5d296ad508fdb84acd24a577c55 Mon Sep 17 00:00:00 2001 From: Daniel Siegmanski Date: Sun, 24 Nov 2024 15:29:38 +0100 Subject: [PATCH 06/17] Change the PCONFIG values from 8, 9, 10, 11 to 0, 1, 2, 3 --- src/_P076_HLW8012.ino | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/_P076_HLW8012.ino b/src/_P076_HLW8012.ino index 7323afda8a..90358cd79d 100644 --- a/src/_P076_HLW8012.ino +++ b/src/_P076_HLW8012.ino @@ -54,10 +54,10 @@ float p076_henergy{}; # define P076_NR_OUTPUT_VALUES 4 # define P076_NR_OUTPUT_OPTIONS 7 # define P076_QUERY1_CONFIG_POS 1 -# define P076_QUERY1 PCONFIG(8) -# define P076_QUERY2 PCONFIG(9) -# define P076_QUERY3 PCONFIG(10) -# define P076_QUERY4 PCONFIG(11) +# define P076_QUERY1 PCONFIG(0) +# define P076_QUERY2 PCONFIG(1) +# define P076_QUERY3 PCONFIG(2) +# define P076_QUERY4 PCONFIG(3) # define P076_QUERY1_DFLT 0 // Voltage (V) # define P076_QUERY2_DFLT 1 // Current (A) # define P076_QUERY3_DFLT 2 // Active Power (W) From a4df22faa8858e09967852f1d17a29e61d5b010a Mon Sep 17 00:00:00 2001 From: Daniel Siegmanski Date: Sun, 24 Nov 2024 15:31:05 +0100 Subject: [PATCH 07/17] Changed init value from P076_QUERY1_CONFIG_POS --- src/_P076_HLW8012.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/_P076_HLW8012.ino b/src/_P076_HLW8012.ino index 90358cd79d..630a5eb89c 100644 --- a/src/_P076_HLW8012.ino +++ b/src/_P076_HLW8012.ino @@ -53,7 +53,7 @@ float p076_henergy{}; # define P076_NR_OUTPUT_VALUES 4 # define P076_NR_OUTPUT_OPTIONS 7 -# define P076_QUERY1_CONFIG_POS 1 +# define P076_QUERY1_CONFIG_POS 0 # define P076_QUERY1 PCONFIG(0) # define P076_QUERY2 PCONFIG(1) # define P076_QUERY3 PCONFIG(2) From 1ebde0cacb62f86aea2db6ce031ff3f809217423 Mon Sep 17 00:00:00 2001 From: Daniel Siegmanski Date: Sun, 24 Nov 2024 15:34:19 +0100 Subject: [PATCH 08/17] Changed single = to a double == --- src/_P076_HLW8012.ino | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/_P076_HLW8012.ino b/src/_P076_HLW8012.ino index 630a5eb89c..3effefb17b 100644 --- a/src/_P076_HLW8012.ino +++ b/src/_P076_HLW8012.ino @@ -546,16 +546,16 @@ boolean Plugin_076(uint8_t function, struct EventStruct *event, String& string) Scheduler.schedule_task_device_timer(event->TaskIndex, millis() + 5000); // Set default querys if not set - if ( P076_QUERY1 = 0 ) { + if ( P076_QUERY1 == 0) { P076_QUERY1 = P076_QUERY1_DFLT; } - if ( P076_QUERY2 = 0 ) { + if ( P076_QUERY2 == 0 ) { P076_QUERY2 = P076_QUERY2_DFLT; } - if ( P076_QUERY3 = 0 ) { + if ( P076_QUERY3 == 0 ) { P076_QUERY3 = P076_QUERY3_DFLT; } - if ( P076_QUERY4 = 0 ) { + if ( P076_QUERY4 == 0 ) { P076_QUERY4 = P076_QUERY4_DFLT; } success = true; From 1a5d0aca900b3e192a806b93b369e1401480128d Mon Sep 17 00:00:00 2001 From: Daniel Siegmanski Date: Sun, 24 Nov 2024 15:48:29 +0100 Subject: [PATCH 09/17] Set default quersy only if no one is set. --- src/_P076_HLW8012.ino | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/_P076_HLW8012.ino b/src/_P076_HLW8012.ino index 3effefb17b..052fa61a0b 100644 --- a/src/_P076_HLW8012.ino +++ b/src/_P076_HLW8012.ino @@ -546,16 +546,10 @@ boolean Plugin_076(uint8_t function, struct EventStruct *event, String& string) Scheduler.schedule_task_device_timer(event->TaskIndex, millis() + 5000); // Set default querys if not set - if ( P076_QUERY1 == 0) { + if ( P076_QUERY1 == 0 && P076_QUERY2 == 0 && P076_QUERY3 == 0 && P076_QUERY4 == 0 ) { P076_QUERY1 = P076_QUERY1_DFLT; - } - if ( P076_QUERY2 == 0 ) { P076_QUERY2 = P076_QUERY2_DFLT; - } - if ( P076_QUERY3 == 0 ) { P076_QUERY3 = P076_QUERY3_DFLT; - } - if ( P076_QUERY4 == 0 ) { P076_QUERY4 = P076_QUERY4_DFLT; } success = true; From fc3bf81cad712e3351548cd777dc2d573f627463 Mon Sep 17 00:00:00 2001 From: Daniel Siegmanski Date: Sun, 24 Nov 2024 15:52:07 +0100 Subject: [PATCH 10/17] Update the values only if valid. --- src/_P076_HLW8012.ino | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/_P076_HLW8012.ino b/src/_P076_HLW8012.ino index 052fa61a0b..42df07fd07 100644 --- a/src/_P076_HLW8012.ino +++ b/src/_P076_HLW8012.ino @@ -418,49 +418,50 @@ boolean Plugin_076(uint8_t function, struct EventStruct *event, String& string) // ++p076_read_stage; // } else if (p076_read_stage > 3) { bool valid = false; + float HLW[7]; p076_hpowerActive = Plugin_076_hlw->getActivePower(valid); if (valid) { + HLW[2] = p076_hpowerActive; success = true; } p076_hvoltage = Plugin_076_hlw->getVoltage(valid); if (valid) { + HLW[0] = p076_hvoltage; success = true; } + p076_hcurrent = Plugin_076_hlw->getCurrent(valid); if (valid) { + HLW[1] = p076_hcurrent; success = true; } + p076_hpowfact = static_cast(100 * Plugin_076_hlw->getPowerFactor(valid)); if (valid) { + HLW[5] = p076_hpowfact; success = true; } p076_hpowerReactive = Plugin_076_hlw->getReactivePower(valid); if (valid) { + HLW[3] = p076_hpowerReactive; success = true; } p076_hpowerApparent = Plugin_076_hlw->getApparentPower(valid); if (valid) { + HLW[4] = p076_hpowerApparent; success = true; } p076_henergy = Plugin_076_hlw->getEnergy(); - - float HLW[7]; - HLW[0] = p076_hvoltage; - HLW[1] = p076_hcurrent; - HLW[2] = p076_hpowerActive; - HLW[3] = p076_hpowerReactive; - HLW[4] = p076_hpowerApparent; - HLW[5] = p076_hpowfact; HLW[6] = p076_henergy; UserVar.setFloat(event->TaskIndex, 0, HLW[P076_QUERY1]); From cec66f4e772baf1bc6c1f081214cadabec611c21 Mon Sep 17 00:00:00 2001 From: Daniel Siegmanski Date: Sun, 24 Nov 2024 15:53:00 +0100 Subject: [PATCH 11/17] Check if the pointer is valid. --- src/_P076_HLW8012.ino | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/_P076_HLW8012.ino b/src/_P076_HLW8012.ino index 42df07fd07..5e797b441b 100644 --- a/src/_P076_HLW8012.ino +++ b/src/_P076_HLW8012.ino @@ -620,10 +620,12 @@ boolean Plugin_076(uint8_t function, struct EventStruct *event, String& string) } void Plugin076_ResetEnergy() { - Plugin_076_hlw -> resetEnergy(); - # if PLUGIN_076_DEBUG - addLog(LOG_LEVEL_INFO, F("P076: Reset Energy counter to zero")); - # endif // if PLUGIN_076_DEBUG + if (Plugin_076_hlw) { + Plugin_076_hlw -> resetEnergy(); + # if PLUGIN_076_DEBUG + addLog(LOG_LEVEL_INFO, F("P076: Reset Energy counter to zero")); + # endif // if PLUGIN_076_DEBUG + } } void Plugin076_ResetMultipliers() { From 31ef47d450ed447ab2206768e914e793dc2ce689 Mon Sep 17 00:00:00 2001 From: Daniel Siegmanski Date: Sun, 1 Dec 2024 09:35:31 +0100 Subject: [PATCH 12/17] Replaced spaces through underlines. --- src/_P076_HLW8012.ino | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/_P076_HLW8012.ino b/src/_P076_HLW8012.ino index 5e797b441b..a7c1740b62 100644 --- a/src/_P076_HLW8012.ino +++ b/src/_P076_HLW8012.ino @@ -743,9 +743,9 @@ const __FlashStringHelper* p076_getQueryString(uint8_t query) { { case 0: return F("Voltage_V"); case 1: return F("Current_A"); - case 2: return F("Active Power_W"); - case 3: return F("Reactive Power_VAR"); - case 4: return F("Apparent Power_VA"); + case 2: return F("Active_Power_W"); + case 3: return F("Reactive_Power_VAR"); + case 4: return F("Apparent_Power_VA"); case 5: return F("Power_Factor_cosphi"); case 6: return F("Energy_Ws"); } From 0112399c1f967be8a6d187c93af7bcd06fd4aa32 Mon Sep 17 00:00:00 2001 From: Daniel Siegmanski Date: Sun, 1 Dec 2024 09:47:10 +0100 Subject: [PATCH 13/17] The old meassurement values (voltage, current, active power & power factor) are now defaults again. For compatibility with old setups. --- src/_P076_HLW8012.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/_P076_HLW8012.ino b/src/_P076_HLW8012.ino index a7c1740b62..20680cd86b 100644 --- a/src/_P076_HLW8012.ino +++ b/src/_P076_HLW8012.ino @@ -61,7 +61,7 @@ float p076_henergy{}; # define P076_QUERY1_DFLT 0 // Voltage (V) # define P076_QUERY2_DFLT 1 // Current (A) # define P076_QUERY3_DFLT 2 // Active Power (W) -# define P076_QUERY4_DFLT 6 // Energy (Ws) +# define P076_QUERY4_DFLT 5 // Power Factor (cosphi) # define P076_Custom 0 From 8fcadfab242915c902539eed3d3a0415e53b493c Mon Sep 17 00:00:00 2001 From: Daniel Siegmanski Date: Sun, 1 Dec 2024 11:13:55 +0100 Subject: [PATCH 14/17] Added changelog --- src/_P076_HLW8012.ino | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/_P076_HLW8012.ino b/src/_P076_HLW8012.ino index 20680cd86b..3f40a0efb2 100644 --- a/src/_P076_HLW8012.ino +++ b/src/_P076_HLW8012.ino @@ -19,6 +19,11 @@ // /** Changelog: + * 2024-11-24 dsiggi: - Added the ability to read reactive power, apparent power and energy + * - Added ability to reset the energy counter with console command hlwresetenergy + * 2024-12-01 dsiggi: - The old measurement values (voltage, current and power factor) are now again the default to support old setups. + * - Removed spaces from value names + * - Uncrustify * 2023-01-03 tonhuisman: Uncrustify source, apply some code improvements * Older changelog not registered. */ @@ -368,7 +373,6 @@ boolean Plugin_076(uint8_t function, struct EventStruct *event, String& string) if (Plugin_076_hlw) { bool valid = false; - switch (p076_read_stage) { case 0: // The stage where we have to wait for a measurement to be started. From 0f00c66e4bb0dccfcdfa0c6af9bc6f737714338c Mon Sep 17 00:00:00 2001 From: Daniel Siegmanski Date: Sun, 1 Dec 2024 11:15:44 +0100 Subject: [PATCH 15/17] Uncrustify --- src/_P076_HLW8012.ino | 105 ++++++++++++++++++++++-------------------- 1 file changed, 56 insertions(+), 49 deletions(-) diff --git a/src/_P076_HLW8012.ino b/src/_P076_HLW8012.ino index 3f40a0efb2..5ba5321158 100644 --- a/src/_P076_HLW8012.ino +++ b/src/_P076_HLW8012.ino @@ -45,16 +45,16 @@ HLW8012 *Plugin_076_hlw = nullptr; # define HLW_VOLTAGE_RESISTOR_DOWN (1000) // Real 1.009k // ----------------------------------------------------------------------------------------------- int StoredTaskIndex = -1; -uint8_t p076_read_stage{}; -unsigned long p076_timer{}; +uint8_t p076_read_stage {}; +unsigned long p076_timer {}; -float p076_hcurrent{}; -float p076_hvoltage{}; -float p076_hpowerActive{}; -float p076_hpowerReactive{}; -float p076_hpowerApparent{}; -float p076_hpowfact{}; -float p076_henergy{}; +float p076_hcurrent {}; +float p076_hvoltage {}; +float p076_hpowerActive {}; +float p076_hpowerReactive {}; +float p076_hpowerApparent {}; +float p076_hpowfact {}; +float p076_henergy {}; # define P076_NR_OUTPUT_VALUES 4 # define P076_NR_OUTPUT_OPTIONS 7 @@ -138,7 +138,7 @@ boolean Plugin_076(uint8_t function, struct EventStruct *event, String& string) Device[deviceCount].TimerOption = true; Device[deviceCount].PluginStats = true; Device[deviceCount].setPin1Direction(gpio_direction::gpio_output); - Device[deviceCount].OutputDataType = Output_Data_type_t::Simple; + Device[deviceCount].OutputDataType = Output_Data_type_t::Simple; break; } @@ -161,15 +161,15 @@ boolean Plugin_076(uint8_t function, struct EventStruct *event, String& string) case PLUGIN_SET_DEFAULTS: { // Load some defaults - P076_QUERY1 = P076_QUERY1_DFLT; - P076_QUERY2 = P076_QUERY2_DFLT; - P076_QUERY3 = P076_QUERY3_DFLT; - P076_QUERY4 = P076_QUERY4_DFLT; + P076_QUERY1 = P076_QUERY1_DFLT; + P076_QUERY2 = P076_QUERY2_DFLT; + P076_QUERY3 = P076_QUERY3_DFLT; + P076_QUERY4 = P076_QUERY4_DFLT; success = true; break; } - + case PLUGIN_GET_DEVICEGPIONAMES: { event->String1 = formatGpioName_output(F("SEL")); event->String2 = formatGpioName_input(F("CF1")); @@ -336,7 +336,7 @@ boolean Plugin_076(uint8_t function, struct EventStruct *event, String& string) hlwMultipliers[2] = getFormItemFloat(F("powmult")); if ((hlwMultipliers[0] > 1.0) && (hlwMultipliers[1] > 1.0) && (hlwMultipliers[2] > 1.0)) { - SaveCustomTaskSettings(event->TaskIndex, reinterpret_cast(&hlwMultipliers), + SaveCustomTaskSettings(event->TaskIndex, reinterpret_cast < const uint8_t * > (&hlwMultipliers), sizeof(hlwMultipliers)); # if PLUGIN_076_DEBUG addLog(LOG_LEVEL_INFO, F("P076: Saved Calibration from Config Page")); @@ -373,6 +373,7 @@ boolean Plugin_076(uint8_t function, struct EventStruct *event, String& string) if (Plugin_076_hlw) { bool valid = false; + switch (p076_read_stage) { case 0: // The stage where we have to wait for a measurement to be started. @@ -394,12 +395,12 @@ boolean Plugin_076(uint8_t function, struct EventStruct *event, String& string) case 3: // Read voltage + active power + power factor if (timeOutReached(p076_timer)) { - p076_hvoltage = Plugin_076_hlw->getVoltage(valid); + p076_hvoltage = Plugin_076_hlw->getVoltage(valid); p076_hpowerActive = Plugin_076_hlw->getActivePower(valid); - p076_hpowerReactive = Plugin_076_hlw->getReactivePower(valid); - p076_hpowerApparent = Plugin_076_hlw->getApparentPower(valid); - p076_hpowfact = static_cast(100 * Plugin_076_hlw->getPowerFactor(valid)); - p076_henergy = Plugin_076_hlw->getEnergy(); + p076_hpowerReactive = Plugin_076_hlw->getReactivePower(valid); + p076_hpowerApparent = Plugin_076_hlw->getApparentPower(valid); + p076_hpowfact = static_cast < int > (100 * Plugin_076_hlw->getPowerFactor(valid)); + p076_henergy = Plugin_076_hlw->getEnergy(); ++p076_read_stage; // Measurement is done, schedule a new PLUGIN_READ call @@ -421,52 +422,52 @@ boolean Plugin_076(uint8_t function, struct EventStruct *event, String& string) // Force a measurement start. // ++p076_read_stage; // } else if (p076_read_stage > 3) { - bool valid = false; + bool valid = false; float HLW[7]; p076_hpowerActive = Plugin_076_hlw->getActivePower(valid); if (valid) { - HLW[2] = p076_hpowerActive; + HLW[2] = p076_hpowerActive; success = true; } p076_hvoltage = Plugin_076_hlw->getVoltage(valid); if (valid) { - HLW[0] = p076_hvoltage; + HLW[0] = p076_hvoltage; success = true; } p076_hcurrent = Plugin_076_hlw->getCurrent(valid); if (valid) { - HLW[1] = p076_hcurrent; + HLW[1] = p076_hcurrent; success = true; } - p076_hpowfact = static_cast(100 * Plugin_076_hlw->getPowerFactor(valid)); + p076_hpowfact = static_cast < int > (100 * Plugin_076_hlw->getPowerFactor(valid)); if (valid) { - HLW[5] = p076_hpowfact; + HLW[5] = p076_hpowfact; success = true; } p076_hpowerReactive = Plugin_076_hlw->getReactivePower(valid); if (valid) { - HLW[3] = p076_hpowerReactive; + HLW[3] = p076_hpowerReactive; success = true; } p076_hpowerApparent = Plugin_076_hlw->getApparentPower(valid); if (valid) { - HLW[4] = p076_hpowerApparent; + HLW[4] = p076_hpowerApparent; success = true; } p076_henergy = Plugin_076_hlw->getEnergy(); - HLW[6] = p076_henergy; + HLW[6] = p076_henergy; UserVar.setFloat(event->TaskIndex, 0, HLW[P076_QUERY1]); UserVar.setFloat(event->TaskIndex, 1, HLW[P076_QUERY2]); @@ -479,7 +480,13 @@ boolean Plugin_076(uint8_t function, struct EventStruct *event, String& string) # if PLUGIN_076_DEBUG addLogMove(LOG_LEVEL_INFO, strformat(F("P076: Read values - V=%.2f - A=%.2f - W=%.2f - VAR=%.2f - VA=%.2f - Pf%%=%.2f - Ws=%.2f"), - p076_hvoltage, p076_hcurrent, p076_hpowerActive, p076_hpowerReactive, p076_hpowerApparent, p076_hpowfact, p076_henergy)); + p076_hvoltage, + p076_hcurrent, + p076_hpowerActive, + p076_hpowerReactive, + p076_hpowerApparent, + p076_hpowfact, + p076_henergy)); # endif // if PLUGIN_076_DEBUG // Plugin_076_hlw->toggleMode(); @@ -502,7 +509,7 @@ boolean Plugin_076(uint8_t function, struct EventStruct *event, String& string) const uint8_t SEL_PIN = CONFIG_PIN1; if (validGpio(CF_PIN) && validGpio(CF1_PIN) && validGpio(SEL_PIN)) { - Plugin_076_hlw = new (std::nothrow) HLW8012; + Plugin_076_hlw = new(std::nothrow) HLW8012; if (Plugin_076_hlw) { const uint8_t currentRead = PCONFIG(4); @@ -551,11 +558,11 @@ boolean Plugin_076(uint8_t function, struct EventStruct *event, String& string) Scheduler.schedule_task_device_timer(event->TaskIndex, millis() + 5000); // Set default querys if not set - if ( P076_QUERY1 == 0 && P076_QUERY2 == 0 && P076_QUERY3 == 0 && P076_QUERY4 == 0 ) { - P076_QUERY1 = P076_QUERY1_DFLT; - P076_QUERY2 = P076_QUERY2_DFLT; - P076_QUERY3 = P076_QUERY3_DFLT; - P076_QUERY4 = P076_QUERY4_DFLT; + if ((P076_QUERY1 == 0) && (P076_QUERY2 == 0) && (P076_QUERY3 == 0) && (P076_QUERY4 == 0)) { + P076_QUERY1 = P076_QUERY1_DFLT; + P076_QUERY2 = P076_QUERY2_DFLT; + P076_QUERY3 = P076_QUERY3_DFLT; + P076_QUERY4 = P076_QUERY4_DFLT; } success = true; } @@ -625,7 +632,7 @@ boolean Plugin_076(uint8_t function, struct EventStruct *event, String& string) void Plugin076_ResetEnergy() { if (Plugin_076_hlw) { - Plugin_076_hlw -> resetEnergy(); + Plugin_076_hlw->resetEnergy(); # if PLUGIN_076_DEBUG addLog(LOG_LEVEL_INFO, F("P076: Reset Energy counter to zero")); # endif // if PLUGIN_076_DEBUG @@ -646,19 +653,19 @@ void Plugin076_SaveMultipliers() { if (StoredTaskIndex < 0) { return; // Not yet initialized. } - ESPEASY_RULES_FLOAT_TYPE hlwMultipliers[3]{}; + ESPEASY_RULES_FLOAT_TYPE hlwMultipliers[3] {}; if (Plugin076_ReadMultipliers(hlwMultipliers[0], hlwMultipliers[1], hlwMultipliers[2])) { # if FEATURE_USE_DOUBLE_AS_ESPEASY_RULES_FLOAT_TYPE - SaveCustomTaskSettings(StoredTaskIndex, reinterpret_cast(&hlwMultipliers), + SaveCustomTaskSettings(StoredTaskIndex, reinterpret_cast < const uint8_t * > (&hlwMultipliers), sizeof(hlwMultipliers)); # else // if FEATURE_USE_DOUBLE_AS_ESPEASY_RULES_FLOAT_TYPE - double hlwMultipliers_d[3]{}; + double hlwMultipliers_d[3] {}; hlwMultipliers_d[0] = hlwMultipliers[0]; hlwMultipliers_d[1] = hlwMultipliers[1]; hlwMultipliers_d[2] = hlwMultipliers[2]; - SaveCustomTaskSettings(StoredTaskIndex, reinterpret_cast(&hlwMultipliers_d), + SaveCustomTaskSettings(StoredTaskIndex, reinterpret_cast < const uint8_t * > (&hlwMultipliers_d), sizeof(hlwMultipliers_d)); # endif // if FEATURE_USE_DOUBLE_AS_ESPEASY_RULES_FLOAT_TYPE } @@ -689,7 +696,7 @@ bool Plugin076_LoadMultipliers(taskIndex_t TaskIndex, } double hlwMultipliers[3]; - LoadCustomTaskSettings(TaskIndex, reinterpret_cast(&hlwMultipliers), + LoadCustomTaskSettings(TaskIndex, reinterpret_cast < uint8_t * > (&hlwMultipliers), sizeof(hlwMultipliers)); if (hlwMultipliers[0] > 1.0) { @@ -719,13 +726,13 @@ void Plugin076_Reset(taskIndex_t TaskIndex) { p076_read_stage = 0; p076_timer = 0; - p076_hcurrent = 0.0f; - p076_hvoltage = 0.0f; + p076_hcurrent = 0.0f; + p076_hvoltage = 0.0f; p076_hpowerActive = 0.0f; - p076_hpowerReactive = 0.0f; - p076_hpowerApparent = 0.0f; - p076_hpowfact = 0.0f; - p076_henergy = 0.0f; + p076_hpowerReactive = 0.0f; + p076_hpowerApparent = 0.0f; + p076_hpowfact = 0.0f; + p076_henergy = 0.0f; } // When using interrupts we have to call the library entry point From 5e55670ef232e6f6b8320afbc56aa1f617753afe Mon Sep 17 00:00:00 2001 From: Daniel Siegmanski Date: Sun, 1 Dec 2024 18:23:58 +0100 Subject: [PATCH 16/17] Added different strings for output selector and value description. --- src/_P076_HLW8012.ino | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/src/_P076_HLW8012.ino b/src/_P076_HLW8012.ino index 5ba5321158..51d4c66f8f 100644 --- a/src/_P076_HLW8012.ino +++ b/src/_P076_HLW8012.ino @@ -19,11 +19,12 @@ // /** Changelog: - * 2024-11-24 dsiggi: - Added the ability to read reactive power, apparent power and energy - * - Added ability to reset the energy counter with console command hlwresetenergy * 2024-12-01 dsiggi: - The old measurement values (voltage, current and power factor) are now again the default to support old setups. * - Removed spaces from value names + * - Seperated Output selector string an value string * - Uncrustify + * 2024-11-24 dsiggi: - Added the ability to read reactive power, apparent power and energy + * - Added ability to reset the energy counter with console command hlwresetenergy * 2023-01-03 tonhuisman: Uncrustify source, apply some code improvements * Older changelog not registered. */ @@ -85,7 +86,8 @@ float p076_henergy {}; # define P076_Shelly_PLUG_S 10 // Forward declaration helper function -const __FlashStringHelper* p076_getQueryString(uint8_t query); +const __FlashStringHelper* p076_getQueryString(uint8_t value_nr, + bool displayString); # if ESP_IDF_VERSION_MAJOR >= 5 @@ -151,7 +153,7 @@ boolean Plugin_076(uint8_t function, struct EventStruct *event, String& string) for (uint8_t i = 0; i < VARS_PER_TASK; ++i) { if (i < P076_NR_OUTPUT_VALUES) { uint8_t choice = PCONFIG(i + P076_QUERY1_CONFIG_POS); - ExtraTaskSettings.setTaskDeviceValueName(i, p076_getQueryString(choice)); + ExtraTaskSettings.setTaskDeviceValueName(i, p076_getQueryString(choice, false)); } else { ExtraTaskSettings.clearTaskDeviceValueName(i); } @@ -182,7 +184,7 @@ boolean Plugin_076(uint8_t function, struct EventStruct *event, String& string) const __FlashStringHelper *options[P076_NR_OUTPUT_OPTIONS]; for (uint8_t i = 0; i < P076_NR_OUTPUT_OPTIONS; ++i) { - options[i] = p076_getQueryString(i); + options[i] = p076_getQueryString(i, true); } for (uint8_t i = 0; i < P076_NR_OUTPUT_VALUES; ++i) { @@ -362,7 +364,7 @@ boolean Plugin_076(uint8_t function, struct EventStruct *event, String& string) for (uint8_t i = 0; i < P076_NR_OUTPUT_VALUES; ++i) { const uint8_t pconfigIndex = i + P076_QUERY1_CONFIG_POS; const uint8_t choice = PCONFIG(pconfigIndex); - sensorTypeHelper_saveOutputSelector(event, pconfigIndex, i, p076_getQueryString(choice)); + sensorTypeHelper_saveOutputSelector(event, pconfigIndex, i, p076_getQueryString(choice, false)); } success = true; @@ -749,16 +751,15 @@ void IRAM_ATTR p076_hlw8012_cf_interrupt() { } } -const __FlashStringHelper* p076_getQueryString(uint8_t query) { - switch (query) - { - case 0: return F("Voltage_V"); - case 1: return F("Current_A"); - case 2: return F("Active_Power_W"); - case 3: return F("Reactive_Power_VAR"); - case 4: return F("Apparent_Power_VA"); - case 5: return F("Power_Factor_cosphi"); - case 6: return F("Energy_Ws"); +const __FlashStringHelper* p076_getQueryString(uint8_t value_nr, bool displayString) { + switch (value_nr) { + case 0: return displayString ? F("Voltage") : F("V"); + case 1: return displayString ? F("Current") : F("A"); + case 2: return displayString ? F("Active Power") : F("W"); + case 3: return displayString ? F("Reactive Power") : F("VAR"); + case 4: return displayString ? F("Apparent Power") : F("VA"); + case 5: return displayString ? F("Power Factor") : F("cosphi"); + case 6: return displayString ? F("Energy") : F("Ws"); } return F(""); } From cb24f26c8dbba1fa4901057dae496f4bb3c272ed Mon Sep 17 00:00:00 2001 From: Daniel Siegmanski Date: Sun, 1 Dec 2024 20:45:38 +0100 Subject: [PATCH 17/17] Resored the old value names for backward compatibility. --- src/_P076_HLW8012.ino | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/_P076_HLW8012.ino b/src/_P076_HLW8012.ino index 51d4c66f8f..84daace7ec 100644 --- a/src/_P076_HLW8012.ino +++ b/src/_P076_HLW8012.ino @@ -753,12 +753,12 @@ void IRAM_ATTR p076_hlw8012_cf_interrupt() { const __FlashStringHelper* p076_getQueryString(uint8_t value_nr, bool displayString) { switch (value_nr) { - case 0: return displayString ? F("Voltage") : F("V"); - case 1: return displayString ? F("Current") : F("A"); - case 2: return displayString ? F("Active Power") : F("W"); + case 0: return displayString ? F("Voltage") : F("Voltage"); + case 1: return displayString ? F("Current") : F("Current"); + case 2: return displayString ? F("Active Power") : F("Power"); case 3: return displayString ? F("Reactive Power") : F("VAR"); case 4: return displayString ? F("Apparent Power") : F("VA"); - case 5: return displayString ? F("Power Factor") : F("cosphi"); + case 5: return displayString ? F("Power Factor") : F("PowerFactor"); case 6: return displayString ? F("Energy") : F("Ws"); } return F("");