8 static const char *
const TAG =
"atm90e32";
11 if (this->
read16_(ATM90E32_REGISTER_METEREN) != 1) {
16 if (this->
phase_[0].voltage_sensor_ !=
nullptr) {
19 if (this->
phase_[1].voltage_sensor_ !=
nullptr) {
22 if (this->
phase_[2].voltage_sensor_ !=
nullptr) {
25 if (this->
phase_[0].current_sensor_ !=
nullptr) {
28 if (this->
phase_[1].current_sensor_ !=
nullptr) {
31 if (this->
phase_[2].current_sensor_ !=
nullptr) {
34 if (this->
phase_[0].power_sensor_ !=
nullptr) {
37 if (this->
phase_[1].power_sensor_ !=
nullptr) {
40 if (this->
phase_[2].power_sensor_ !=
nullptr) {
43 if (this->
phase_[0].reactive_power_sensor_ !=
nullptr) {
46 if (this->
phase_[1].reactive_power_sensor_ !=
nullptr) {
49 if (this->
phase_[2].reactive_power_sensor_ !=
nullptr) {
52 if (this->
phase_[0].power_factor_sensor_ !=
nullptr) {
55 if (this->
phase_[1].power_factor_sensor_ !=
nullptr) {
58 if (this->
phase_[2].power_factor_sensor_ !=
nullptr) {
61 if (this->
phase_[0].forward_active_energy_sensor_ !=
nullptr) {
64 if (this->
phase_[1].forward_active_energy_sensor_ !=
nullptr) {
67 if (this->
phase_[2].forward_active_energy_sensor_ !=
nullptr) {
70 if (this->
phase_[0].reverse_active_energy_sensor_ !=
nullptr) {
73 if (this->
phase_[1].reverse_active_energy_sensor_ !=
nullptr) {
76 if (this->
phase_[2].reverse_active_energy_sensor_ !=
nullptr) {
89 ESP_LOGCONFIG(TAG,
"Setting up ATM90E32 Component...");
92 uint16_t mmode0 = 0x87;
102 this->
write16_(ATM90E32_REGISTER_SOFTRESET, 0x789A);
103 this->
write16_(ATM90E32_REGISTER_CFGREGACCEN, 0x55AA);
104 this->
write16_(ATM90E32_REGISTER_METEREN, 0x0001);
105 if (this->
read16_(ATM90E32_REGISTER_LASTSPIDATA) != 0x0001) {
106 ESP_LOGW(TAG,
"Could not initialize ATM90E32 IC, check SPI settings");
110 this->
write16_(ATM90E32_REGISTER_PLCONSTH, 0x0861);
111 this->
write16_(ATM90E32_REGISTER_PLCONSTL, 0xC468);
112 this->
write16_(ATM90E32_REGISTER_ZXCONFIG, 0xD654);
113 this->
write16_(ATM90E32_REGISTER_MMODE0, mmode0);
115 this->
write16_(ATM90E32_REGISTER_PSTARTTH, 0x1D4C);
116 this->
write16_(ATM90E32_REGISTER_QSTARTTH, 0x1D4C);
117 this->
write16_(ATM90E32_REGISTER_PPHASETH, 0x02EE);
118 this->
write16_(ATM90E32_REGISTER_QPHASETH, 0x02EE);
119 this->
write16_(ATM90E32_REGISTER_UGAINA, this->
phase_[0].volt_gain_);
120 this->
write16_(ATM90E32_REGISTER_IGAINA, this->
phase_[0].ct_gain_);
121 this->
write16_(ATM90E32_REGISTER_UGAINB, this->
phase_[1].volt_gain_);
122 this->
write16_(ATM90E32_REGISTER_IGAINB, this->
phase_[1].ct_gain_);
123 this->
write16_(ATM90E32_REGISTER_UGAINC, this->
phase_[2].volt_gain_);
124 this->
write16_(ATM90E32_REGISTER_IGAINC, this->
phase_[2].ct_gain_);
125 this->
write16_(ATM90E32_REGISTER_CFGREGACCEN, 0x0000);
129 ESP_LOGCONFIG(
"",
"ATM90E32:");
130 LOG_PIN(
" CS Pin: ", this->
cs_);
132 ESP_LOGE(TAG,
"Communication with ATM90E32 failed!");
134 LOG_UPDATE_INTERVAL(
this);
135 LOG_SENSOR(
" ",
"Voltage A", this->
phase_[0].voltage_sensor_);
136 LOG_SENSOR(
" ",
"Current A", this->
phase_[0].current_sensor_);
137 LOG_SENSOR(
" ",
"Power A", this->
phase_[0].power_sensor_);
138 LOG_SENSOR(
" ",
"Reactive Power A", this->
phase_[0].reactive_power_sensor_);
139 LOG_SENSOR(
" ",
"PF A", this->
phase_[0].power_factor_sensor_);
140 LOG_SENSOR(
" ",
"Active Forward Energy A", this->
phase_[0].forward_active_energy_sensor_);
141 LOG_SENSOR(
" ",
"Active Reverse Energy A", this->
phase_[0].reverse_active_energy_sensor_);
142 LOG_SENSOR(
" ",
"Voltage B", this->
phase_[1].voltage_sensor_);
143 LOG_SENSOR(
" ",
"Current B", this->
phase_[1].current_sensor_);
144 LOG_SENSOR(
" ",
"Power B", this->
phase_[1].power_sensor_);
145 LOG_SENSOR(
" ",
"Reactive Power B", this->
phase_[1].reactive_power_sensor_);
146 LOG_SENSOR(
" ",
"PF B", this->
phase_[1].power_factor_sensor_);
147 LOG_SENSOR(
" ",
"Active Forward Energy B", this->
phase_[1].forward_active_energy_sensor_);
148 LOG_SENSOR(
" ",
"Active Reverse Energy B", this->
phase_[1].reverse_active_energy_sensor_);
149 LOG_SENSOR(
" ",
"Voltage C", this->
phase_[2].voltage_sensor_);
150 LOG_SENSOR(
" ",
"Current C", this->
phase_[2].current_sensor_);
151 LOG_SENSOR(
" ",
"Power C", this->
phase_[2].power_sensor_);
152 LOG_SENSOR(
" ",
"Reactive Power C", this->
phase_[2].reactive_power_sensor_);
153 LOG_SENSOR(
" ",
"PF C", this->
phase_[2].power_factor_sensor_);
154 LOG_SENSOR(
" ",
"Active Forward Energy C", this->
phase_[2].forward_active_energy_sensor_);
155 LOG_SENSOR(
" ",
"Active Reverse Energy C", this->
phase_[2].reverse_active_energy_sensor_);
162 uint8_t addrh = (1 << 7) | ((a_register >> 8) & 0x03);
163 uint8_t addrl = (a_register & 0xFF);
175 output = (uint16_t(data[0] & 0xFF) << 8) | (data[1] & 0xFF);
176 ESP_LOGVV(TAG,
"read16_ 0x%04X output 0x%04X", a_register, output);
181 uint16_t val_h = this->
read16_(addr_h);
182 uint16_t val_l = this->
read16_(addr_l);
183 int32_t
val = (val_h << 16) | val_l;
185 ESP_LOGVV(TAG,
"read32_ addr_h 0x%04X val_h 0x%04X addr_l 0x%04X val_l 0x%04X = %d", addr_h, val_h, addr_l, val_l,
192 uint8_t addrh = (a_register >> 8) & 0x03;
193 uint8_t addrl = (a_register & 0xFF);
195 ESP_LOGVV(TAG,
"write16_ 0x%04X val 0x%04X", a_register, val);
207 uint16_t voltage = this->
read16_(ATM90E32_REGISTER_URMSA);
208 return (
float) voltage / 100;
211 uint16_t voltage = this->
read16_(ATM90E32_REGISTER_URMSB);
212 return (
float) voltage / 100;
215 uint16_t voltage = this->
read16_(ATM90E32_REGISTER_URMSC);
216 return (
float) voltage / 100;
219 uint16_t current = this->
read16_(ATM90E32_REGISTER_IRMSA);
220 return (
float) current / 1000;
223 uint16_t current = this->
read16_(ATM90E32_REGISTER_IRMSB);
224 return (
float) current / 1000;
227 uint16_t current = this->
read16_(ATM90E32_REGISTER_IRMSC);
228 return (
float) current / 1000;
231 int val = this->
read32_(ATM90E32_REGISTER_PMEANA, ATM90E32_REGISTER_PMEANALSB);
232 return val * 0.00032f;
235 int val = this->
read32_(ATM90E32_REGISTER_PMEANB, ATM90E32_REGISTER_PMEANBLSB);
236 return val * 0.00032f;
239 int val = this->
read32_(ATM90E32_REGISTER_PMEANC, ATM90E32_REGISTER_PMEANCLSB);
240 return val * 0.00032f;
243 int val = this->
read32_(ATM90E32_REGISTER_QMEANA, ATM90E32_REGISTER_QMEANALSB);
244 return val * 0.00032f;
247 int val = this->
read32_(ATM90E32_REGISTER_QMEANB, ATM90E32_REGISTER_QMEANBLSB);
248 return val * 0.00032f;
251 int val = this->
read32_(ATM90E32_REGISTER_QMEANC, ATM90E32_REGISTER_QMEANCLSB);
252 return val * 0.00032f;
255 int16_t pf = this->
read16_(ATM90E32_REGISTER_PFMEANA);
256 return (
float) pf / 1000;
259 int16_t pf = this->
read16_(ATM90E32_REGISTER_PFMEANB);
260 return (
float) pf / 1000;
263 int16_t pf = this->
read16_(ATM90E32_REGISTER_PFMEANC);
264 return (
float) pf / 1000;
267 uint16_t
val = this->
read16_(ATM90E32_REGISTER_APENERGYA);
268 if ((UINT32_MAX - this->
phase_[0].cumulative_forward_active_energy_) > val) {
273 return ((
float) this->
phase_[0].cumulative_forward_active_energy_ * 10 / 3200);
276 uint16_t
val = this->
read16_(ATM90E32_REGISTER_APENERGYB);
277 if (UINT32_MAX - this->
phase_[1].cumulative_forward_active_energy_ > val) {
282 return ((
float) this->
phase_[1].cumulative_forward_active_energy_ * 10 / 3200);
285 uint16_t
val = this->
read16_(ATM90E32_REGISTER_APENERGYC);
286 if (UINT32_MAX - this->
phase_[2].cumulative_forward_active_energy_ > val) {
291 return ((
float) this->
phase_[2].cumulative_forward_active_energy_ * 10 / 3200);
294 uint16_t
val = this->
read16_(ATM90E32_REGISTER_ANENERGYA);
295 if (UINT32_MAX - this->
phase_[0].cumulative_reverse_active_energy_ > val) {
300 return ((
float) this->
phase_[0].cumulative_reverse_active_energy_ * 10 / 3200);
303 uint16_t
val = this->
read16_(ATM90E32_REGISTER_ANENERGYB);
304 if (UINT32_MAX - this->
phase_[1].cumulative_reverse_active_energy_ > val) {
309 return ((
float) this->
phase_[1].cumulative_reverse_active_energy_ * 10 / 3200);
312 uint16_t
val = this->
read16_(ATM90E32_REGISTER_ANENERGYC);
313 if (UINT32_MAX - this->
phase_[2].cumulative_reverse_active_energy_ > val) {
318 return ((
float) this->
phase_[2].cumulative_reverse_active_energy_ * 10 / 3200);
321 uint16_t freq = this->
read16_(ATM90E32_REGISTER_FREQ);
322 return (
float) freq / 100;
325 uint16_t ctemp = this->
read16_(ATM90E32_REGISTER_TEMP);
326 return (
float) ctemp;
sensor::Sensor * voltage_sensor_
sensor::Sensor * forward_active_energy_sensor_
const float DATA
For components that import data from directly connected sensors like DHT.
float get_reactive_power_c_()
float get_setup_priority() const override
void write16_(uint16_t a_register, uint16_t val)
float get_reactive_power_b_()
std::array< uint8_t, N > read_array()
uint32_t cumulative_reverse_active_energy_
float get_reverse_active_energy_c_()
int read32_(uint16_t addr_h, uint16_t addr_l)
float get_reactive_power_a_()
sensor::Sensor * current_sensor_
float get_line_current_b_()
void write_byte(uint8_t data)
float get_power_factor_b_()
float get_chip_temperature_()
float get_forward_active_energy_c_()
float get_reverse_active_energy_a_()
void dump_config() override
float get_line_voltage_c_()
float get_reverse_active_energy_b_()
float get_power_factor_a_()
void status_clear_warning()
float get_active_power_b_()
void publish_state(float state)
Publish a new state to the front-end.
struct esphome::atm90e32::ATM90E32Component::ATM90E32Phase phase_[3]
float get_active_power_c_()
float get_forward_active_energy_b_()
uint32_t cumulative_forward_active_energy_
float get_forward_active_energy_a_()
sensor::Sensor * reverse_active_energy_sensor_
void status_set_warning()
float get_active_power_a_()
float get_line_voltage_a_()
float get_line_current_a_()
float get_line_voltage_b_()
uint16_t read16_(uint16_t a_register)
virtual void mark_failed()
Mark this component as failed.
sensor::Sensor * chip_temperature_sensor_
float get_line_current_c_()
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
sensor::Sensor * power_sensor_
sensor::Sensor * freq_sensor_
float get_power_factor_c_()
sensor::Sensor * reactive_power_sensor_
sensor::Sensor * power_factor_sensor_