ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
hlw8032.cpp
Go to the documentation of this file.
1#include "hlw8032.h"
2#include "esphome/core/log.h"
3#include <cinttypes>
4
5namespace esphome::hlw8032 {
6
7ESPHOME_LOG_TAG(TAG, "hlw8032");
8
9static constexpr uint8_t STATE_REG_OFFSET = 0;
10static constexpr uint8_t VOLTAGE_PARAM_OFFSET = 2;
11static constexpr uint8_t VOLTAGE_REG_OFFSET = 5;
12static constexpr uint8_t CURRENT_PARAM_OFFSET = 8;
13static constexpr uint8_t CURRENT_REG_OFFSET = 11;
14static constexpr uint8_t POWER_PARAM_OFFSET = 14;
15static constexpr uint8_t POWER_REG_OFFSET = 17;
16static constexpr uint8_t DATA_UPDATE_REG_OFFSET = 20;
17static constexpr uint8_t CHECKSUM_REG_OFFSET = 23;
18static constexpr uint8_t PARAM_REG_USABLE_BIT = (1 << 0);
19static constexpr uint8_t POWER_OVERFLOW_BIT = (1 << 1);
20static constexpr uint8_t CURRENT_OVERFLOW_BIT = (1 << 2);
21static constexpr uint8_t VOLTAGE_OVERFLOW_BIT = (1 << 3);
22static constexpr uint8_t HAVE_POWER_BIT = (1 << 4);
23static constexpr uint8_t HAVE_CURRENT_BIT = (1 << 5);
24static constexpr uint8_t HAVE_VOLTAGE_BIT = (1 << 6);
25static constexpr uint8_t CHECK_REG = 0x5A;
26static constexpr uint8_t STATE_REG_CORRECTION_FUNC_NORMAL = 0x55;
27static constexpr uint8_t STATE_REG_CORRECTION_FUNC_FAIL = 0xAA;
28static constexpr uint8_t STATE_REG_CORRECTION_MASK = 0xF0;
29static constexpr uint8_t STATE_REG_OVERFLOW_MASK = 0xF;
30static constexpr uint8_t PACKET_LENGTH = 24;
31// Datasheet: the smallest current the chip measures
32static constexpr float MIN_CURRENT_A = 0.05f;
33
35 this->read_available([this](uint8_t data) {
36 if (!this->header_found_) {
37 if ((data == STATE_REG_CORRECTION_FUNC_NORMAL) || (data == STATE_REG_CORRECTION_FUNC_FAIL) ||
38 (data & STATE_REG_CORRECTION_MASK) == STATE_REG_CORRECTION_MASK) {
39 this->header_found_ = true;
40 this->raw_data_[0] = data;
41 }
42 } else if (data == CHECK_REG) {
43 this->raw_data_[1] = data;
44 this->raw_data_index_ = 2;
45 this->check_ = 0;
46 } else if (this->raw_data_index_ >= 2 && this->raw_data_index_ < PACKET_LENGTH) {
47 this->raw_data_[this->raw_data_index_++] = data;
48 if (this->raw_data_index_ < PACKET_LENGTH) {
49 this->check_ += data;
50 } else if (this->raw_data_index_ == PACKET_LENGTH) {
51 if (this->check_ == this->raw_data_[CHECKSUM_REG_OFFSET]) {
52 this->parse_data_();
53 } else {
54 ESP_LOGW(TAG, "Invalid checksum: 0x%02X != 0x%02X", this->check_, this->raw_data_[CHECKSUM_REG_OFFSET]);
55 }
56 this->raw_data_index_ = 0;
57 this->header_found_ = false;
58 memset(this->raw_data_, 0, PACKET_LENGTH);
59 }
60 }
61 });
62}
63
65 return encode_uint24(this->raw_data_[offset], this->raw_data_[offset + 1], this->raw_data_[offset + 2]);
66}
67
69 // Parse header
70 uint8_t state_reg = this->raw_data_[STATE_REG_OFFSET];
71
72 if (state_reg == STATE_REG_CORRECTION_FUNC_FAIL) {
73 ESP_LOGE(TAG, "The chip's function of error correction fails.");
74 return;
75 }
76
77 // Parse data frame
78 uint32_t voltage_parameter = this->read_uint24_(VOLTAGE_PARAM_OFFSET);
79 uint32_t voltage_reg = this->read_uint24_(VOLTAGE_REG_OFFSET);
80 uint32_t current_parameter = this->read_uint24_(CURRENT_PARAM_OFFSET);
81 uint32_t current_reg = this->read_uint24_(CURRENT_REG_OFFSET);
82 uint32_t power_parameter = this->read_uint24_(POWER_PARAM_OFFSET);
83 uint32_t power_reg = this->read_uint24_(POWER_REG_OFFSET);
84 uint8_t data_update_register = this->raw_data_[DATA_UPDATE_REG_OFFSET];
85
86 bool have_power = data_update_register & HAVE_POWER_BIT;
87 bool have_current = data_update_register & HAVE_CURRENT_BIT;
88 bool have_voltage = data_update_register & HAVE_VOLTAGE_BIT;
89
90 bool power_cycle_exceeds_range = false;
91 bool parameter_regs_usable = true;
92
93 if ((state_reg & STATE_REG_CORRECTION_MASK) == STATE_REG_CORRECTION_MASK) {
94 if (state_reg & STATE_REG_OVERFLOW_MASK) {
95 if (state_reg & VOLTAGE_OVERFLOW_BIT) {
96 have_voltage = false;
97 }
98 if (state_reg & CURRENT_OVERFLOW_BIT) {
99 have_current = false;
100 }
101 if (state_reg & POWER_OVERFLOW_BIT) {
102 have_power = false;
103 }
104 if (state_reg & PARAM_REG_USABLE_BIT) {
105 parameter_regs_usable = false;
106 }
107
108 ESP_LOGW(TAG,
109 "Reports: (0x%02X)\n"
110 " Voltage REG overflows: %s\n"
111 " Current REG overflows: %s\n"
112 " Power REG overflows: %s\n"
113 " Voltage/Current/Power Parameter REGs not usable: %s\n",
114 state_reg, YESNO(!have_voltage), YESNO(!have_current), YESNO(!have_power),
115 YESNO(!parameter_regs_usable));
116
117 if (!parameter_regs_usable) {
118 return;
119 }
120 }
121 power_cycle_exceeds_range = have_power;
122 }
123
124 ESP_LOGVV(TAG,
125 "Parsed data:\n"
126 " Voltage: Parameter REG 0x%06" PRIX32 ", REG 0x%06" PRIX32 "\n"
127 " Current: Parameter REG 0x%06" PRIX32 ", REG 0x%06" PRIX32 "\n"
128 " Power: Parameter REG 0x%06" PRIX32 ", REG 0x%06" PRIX32 "\n"
129 " Data Update: REG 0x%02" PRIX8 "\n",
130 voltage_parameter, voltage_reg, current_parameter, current_reg, power_parameter, power_reg,
131 data_update_register);
132
133 const float current_multiplier = 1 / (this->current_resistor_ * 1000);
134
135 float voltage = 0.0f;
136 if (have_voltage && voltage_reg) {
137 voltage = float(voltage_parameter) * this->voltage_divider_ / float(voltage_reg);
138 }
139 PUBLISH_STATE(this->voltage_sensor_, voltage);
140
141 float power = 0.0f;
142 if (have_power && power_reg && !power_cycle_exceeds_range) {
143 power = (float(power_parameter) / float(power_reg)) * this->voltage_divider_ * current_multiplier;
144 }
145 PUBLISH_STATE(this->power_sensor_, power);
146
147 // Below 50 mA the current register reads noise; gated on power like cse7766, so a low-PF load reads 0 A
148 const bool current_measurable = have_power && voltage > 1.0f && power > MIN_CURRENT_A * voltage;
149 float current = 0.0f;
150 if (have_current && current_reg && current_measurable) {
151 current = float(current_parameter) * current_multiplier / float(current_reg);
152 }
153 PUBLISH_STATE(this->current_sensor_, current);
154
155 float pf = NAN;
156 const float apparent_power = voltage * current;
157 if (have_voltage && have_current && have_power) {
158 if (apparent_power > 0) {
159 pf = power / apparent_power;
160 if (pf > 1) {
161 // Noise near the resolution limit pushes PF past 1 on most resistive-load frames
162 ESP_LOGV(TAG, "Power factor %.4f above 1, clamped", pf);
163 pf = 1.0f;
164 }
165 } else if (power == 0) {
166 // No load, report ideal power factor
167 pf = 1.0f;
168 }
169 }
170 PUBLISH_STATE(this->apparent_power_sensor_, apparent_power);
171 PUBLISH_STATE(this->power_factor_sensor_, pf);
172}
173
174void HLW8032Component::dump_config() {
175 ESP_LOGCONFIG(TAG,
176 "Configuration:\n"
177 " Current resistor: %.1f mΩ\n"
178 " Voltage Divider: %.3f",
179 this->current_resistor_ * 1000.0f, this->voltage_divider_);
180 LOG_SENSOR(" ", "Voltage", this->voltage_sensor_);
181 LOG_SENSOR(" ", "Current", this->current_sensor_);
182 LOG_SENSOR(" ", "Power", this->power_sensor_);
183 LOG_SENSOR(" ", "Apparent Power", this->apparent_power_sensor_);
184 LOG_SENSOR(" ", "Power Factor", this->power_factor_sensor_);
185}
186} // namespace esphome::hlw8032
virtual void loop()
This method will be called repeatedly.
Definition component.cpp:86
uint32_t read_uint24_(uint8_t offset)
Definition hlw8032.cpp:64
bool read_available(F &&on_byte)
Same as read_available_chunks(), calling on_byte(uint8_t) once per byte.
Definition uart.h:77
ESPHOME_LOG_TAG(TAG, "hlw8032")
constexpr uint32_t encode_uint24(uint8_t byte1, uint8_t byte2, uint8_t byte3)
Encode a 24-bit value given three bytes in most to least significant byte order.
Definition helpers.h:1231
static void uint32_t