ESPHome 2026.2.1
Loading...
Searching...
No Matches
hlw8012.cpp
Go to the documentation of this file.
1#include "hlw8012.h"
2#include "esphome/core/log.h"
3
4namespace esphome {
5namespace hlw8012 {
6
7static const char *const TAG = "hlw8012";
8
9// valid for HLW8012 and CSE7759
10static const uint32_t HLW8012_CLOCK_FREQUENCY = 3579000;
11
13 float reference_voltage = 0;
14 this->sel_pin_->setup();
18
19 // Initialize multipliers
21 reference_voltage = 1.218f;
22 this->power_multiplier_ =
23 reference_voltage * reference_voltage * this->voltage_divider_ / this->current_resistor_ / 1721506.0f;
24 this->current_multiplier_ = reference_voltage / this->current_resistor_ / 94638.0f;
25 this->voltage_multiplier_ = reference_voltage * this->voltage_divider_ / 15397.0f;
26 } else {
27 // HLW8012 and CSE7759 have same reference specs
28 reference_voltage = 2.43f;
29 this->power_multiplier_ = reference_voltage * reference_voltage * this->voltage_divider_ / this->current_resistor_ *
30 64.0f / 24.0f / HLW8012_CLOCK_FREQUENCY;
31 this->current_multiplier_ = reference_voltage / this->current_resistor_ * 512.0f / 24.0f / HLW8012_CLOCK_FREQUENCY;
32 this->voltage_multiplier_ = reference_voltage * this->voltage_divider_ * 256.0f / HLW8012_CLOCK_FREQUENCY;
33 }
34}
36 ESP_LOGCONFIG(TAG,
37 "HLW8012:\n"
38 " Change measurement mode every %" PRIu32 "\n"
39 " Current resistor: %.1f mΩ\n"
40 " Voltage Divider: %.1f",
41 this->change_mode_every_, this->current_resistor_ * 1000.0f, this->voltage_divider_);
42 LOG_PIN(" SEL Pin: ", this->sel_pin_);
43 LOG_PIN(" CF Pin: ", this->cf_pin_);
44 LOG_PIN(" CF1 Pin: ", this->cf1_pin_);
45 LOG_UPDATE_INTERVAL(this);
46 LOG_SENSOR(" ", "Voltage", this->voltage_sensor_);
47 LOG_SENSOR(" ", "Current", this->current_sensor_);
48 LOG_SENSOR(" ", "Power", this->power_sensor_);
49 LOG_SENSOR(" ", "Energy", this->energy_sensor_);
50}
52 // HLW8012 has 50% duty cycle
55 float cf_hz = raw_cf / (this->get_update_interval() / 1000.0f);
56 if (raw_cf <= 1) {
57 // don't count single pulse as power
58 cf_hz = 0.0f;
59 }
60 float cf1_hz = raw_cf1 / (this->get_update_interval() / 1000.0f);
61 if (raw_cf1 <= 1) {
62 // don't count single pulse as anything
63 cf1_hz = 0.0f;
64 }
65
66 if (this->nth_value_++ < 2) {
67 return;
68 }
69
70 float power = cf_hz * this->power_multiplier_;
71
72 if (this->change_mode_at_ != 0 || this->change_mode_every_ == 0) {
73 // Only read cf1 after one cycle. Apparently it's quite unstable after being changed.
74 if (this->current_mode_) {
75 float current = cf1_hz * this->current_multiplier_;
76 ESP_LOGD(TAG, "Got power=%.1fW, current=%.1fA", power, current);
77 if (this->current_sensor_ != nullptr) {
78 this->current_sensor_->publish_state(current);
79 }
80 } else {
81 float voltage = cf1_hz * this->voltage_multiplier_;
82 ESP_LOGD(TAG, "Got power=%.1fW, voltage=%.1fV", power, voltage);
83 if (this->voltage_sensor_ != nullptr) {
84 this->voltage_sensor_->publish_state(voltage);
85 }
86 }
87 }
88
89 if (this->power_sensor_ != nullptr) {
90 this->power_sensor_->publish_state(power);
91 }
92
93 if (this->energy_sensor_ != nullptr) {
94 cf_total_pulses_ += raw_cf;
95 float energy = cf_total_pulses_ * this->power_multiplier_ / 3600;
96 this->energy_sensor_->publish_state(energy);
97 }
98
99 if (this->change_mode_every_ != 0 && this->change_mode_at_++ == this->change_mode_every_) {
100 this->current_mode_ = !this->current_mode_;
101 ESP_LOGV(TAG, "Changing mode to %s mode", this->current_mode_ ? "CURRENT" : "VOLTAGE");
102 this->change_mode_at_ = 0;
104 }
105}
106
107} // namespace hlw8012
108} // namespace esphome
virtual void setup()=0
virtual void digital_write(bool value)=0
virtual uint32_t get_update_interval() const
Get the update interval in ms of this sensor.
sensor::Sensor * voltage_sensor_
Definition hlw8012.h:65
sensor::Sensor * current_sensor_
Definition hlw8012.h:66
pulse_counter::PulseCounterStorageBase & cf_store_
Definition hlw8012.h:62
sensor::Sensor * power_sensor_
Definition hlw8012.h:67
pulse_counter::PulseCounterStorageBase & cf1_store_
Definition hlw8012.h:64
HLW8012SensorModels sensor_model_
Definition hlw8012.h:58
sensor::Sensor * energy_sensor_
Definition hlw8012.h:68
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:65
@ HLW8012_SENSOR_MODEL_BL0937
Definition hlw8012.h:18
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
virtual pulse_counter_t read_raw_value()=0
virtual bool pulse_counter_setup(InternalGPIOPin *pin)=0