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