Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[HLW8012] Fix setting calibration via command #4895

Open
wants to merge 2 commits into
base: mega
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 26 additions & 6 deletions lib/HLW8012_1.1.1/src/HLW8012.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,24 +186,44 @@ void HLW8012::resetEnergy() {
_cf_pulse_count_total = 0;
}

void HLW8012::expectedCurrent(float value) {
void HLW8012::expectedCurrent(float expected) {
bool valid = false;
if (static_cast<int>(_current) == 0) getCurrent(valid);
if (valid && static_cast<int>(_current) > 0) _current_multiplier *= (value / _current);
if (valid && static_cast<int>(_current) > 0) _current_multiplier *= (expected / _current);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will valid ever be set to true, if _current is already greater than 0, when calling this method? If not, then the multiplier won't be set, AFAICS...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only when _power != 0, or when there is an interrupt while executing this function.

But with this PR I will no longer just call getCurrent() when updating the calibration.

}

void HLW8012::expectedVoltage(float value) {
void HLW8012::expectedVoltage(float expected) {
bool valid = false;
if (static_cast<int>(_voltage) == 0) getVoltage(valid);
if (valid && static_cast<int>(_voltage) > 0) _voltage_multiplier *= (value / _voltage);
if (valid && static_cast<int>(_voltage) > 0) _voltage_multiplier *= (expected / _voltage);
}

void HLW8012::expectedActivePower(float value) {
void HLW8012::expectedActivePower(float expected) {
bool valid = false;
if (static_cast<int>(_power) == 0) getActivePower(valid);
if (valid && static_cast<int>(_power) > 0) _power_multiplier *= (value / _power);
if (valid && static_cast<int>(_power) > 0) _power_multiplier *= (expected / _power);
}

void HLW8012::expectedCurrent(float expected, float measured) {
if (static_cast<int>(expected) == 0 || static_cast<int>(measured) == 0)
return;
_current_multiplier *= (expected / measured);
}

void HLW8012::expectedVoltage(float expected, float measured) {
if (static_cast<int>(expected) == 0 || static_cast<int>(measured) == 0)
return;
_voltage_multiplier *= (expected / measured);
}

void HLW8012::expectedActivePower(float expected, float measured) {
if (static_cast<int>(expected) == 0 || static_cast<int>(measured) == 0)
return;
_power_multiplier *= (expected / measured);
}



void HLW8012::resetMultipliers() {
_calculateDefaultMultipliers();
}
Expand Down
11 changes: 8 additions & 3 deletions lib/HLW8012_1.1.1/src/HLW8012.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,14 @@ class HLW8012 {

void setResistors(float current, float voltage_upstream, float voltage_downstream);

void expectedCurrent(float current);
void expectedVoltage(float current);
void expectedActivePower(float power);
void expectedCurrent(float expected);
void expectedVoltage(float expected);
void expectedActivePower(float expected);

void expectedCurrent(float expected, float measured);
void expectedVoltage(float expected, float measured);
void expectedActivePower(float expected, float measured);


float getCurrentMultiplier() { return _current_multiplier; };
float getVoltageMultiplier() { return _voltage_multiplier; };
Expand Down
6 changes: 3 additions & 3 deletions src/_P076_HLW8012.ino
Original file line number Diff line number Diff line change
Expand Up @@ -511,17 +511,17 @@ boolean Plugin_076(uint8_t function, struct EventStruct *event, String& string)
bool changed = false;

if (CalibVolt != 0) {
Plugin_076_hlw->expectedVoltage(CalibVolt);
Plugin_076_hlw->expectedVoltage(CalibVolt, UserVar[event->BaseVarIndex]);
changed = true;
}

if (definitelyGreaterThan(CalibCurr, 0.0f)) {
Plugin_076_hlw->expectedCurrent(CalibCurr);
Plugin_076_hlw->expectedCurrent(CalibCurr, UserVar[event->BaseVarIndex + 1]);
changed = true;
}

if (!essentiallyEqual(CalibAcPwr, 0.0f)) {
Plugin_076_hlw->expectedActivePower(CalibAcPwr);
Plugin_076_hlw->expectedActivePower(CalibAcPwr, UserVar[event->BaseVarIndex + 2]);
changed = true;
}

Expand Down