9 static const char *
const TAG =
"atm90e32";
13 for (uint8_t phase = 0; phase < 3; phase++) {
14 if (this->
phase_[phase].voltage_sensor_ !=
nullptr) {
18 for (uint8_t phase = 0; phase < 3; phase++) {
19 if (this->
phase_[phase].current_sensor_ !=
nullptr) {
23 for (uint8_t phase = 0; phase < 3; phase++) {
24 if (this->
phase_[phase].power_sensor_ !=
nullptr) {
28 for (uint8_t phase = 0; phase < 3; phase++) {
29 if (this->
phase_[phase].power_factor_sensor_ !=
nullptr) {
33 for (uint8_t phase = 0; phase < 3; phase++) {
34 if (this->
phase_[phase].reactive_power_sensor_ !=
nullptr) {
38 for (uint8_t phase = 0; phase < 3; phase++) {
39 if (this->
phase_[phase].forward_active_energy_sensor_ !=
nullptr) {
43 for (uint8_t phase = 0; phase < 3; phase++) {
44 if (this->
phase_[phase].reverse_active_energy_sensor_ !=
nullptr) {
48 for (uint8_t phase = 0; phase < 3; phase++) {
49 if (this->
phase_[phase].phase_angle_sensor_ !=
nullptr) {
53 for (uint8_t phase = 0; phase < 3; phase++) {
54 if (this->
phase_[phase].harmonic_active_power_sensor_ !=
nullptr) {
58 for (uint8_t phase = 0; phase < 3; phase++) {
59 if (this->
phase_[phase].peak_current_sensor_ !=
nullptr) {
64 for (uint8_t phase = 0; phase < 3; phase++) {
65 if (this->
phase_[phase].voltage_sensor_ !=
nullptr) {
69 for (uint8_t phase = 0; phase < 3; phase++) {
70 if (this->
phase_[phase].current_sensor_ !=
nullptr) {
74 for (uint8_t phase = 0; phase < 3; phase++) {
75 if (this->
phase_[phase].power_sensor_ !=
nullptr) {
79 for (uint8_t phase = 0; phase < 3; phase++) {
80 if (this->
phase_[phase].power_factor_sensor_ !=
nullptr) {
84 for (uint8_t phase = 0; phase < 3; phase++) {
85 if (this->
phase_[phase].reactive_power_sensor_ !=
nullptr) {
89 for (uint8_t phase = 0; phase < 3; phase++) {
90 if (this->
phase_[phase].forward_active_energy_sensor_ !=
nullptr) {
95 for (uint8_t phase = 0; phase < 3; phase++) {
96 if (this->
phase_[phase].reverse_active_energy_sensor_ !=
nullptr) {
101 for (uint8_t phase = 0; phase < 3; phase++) {
102 if (this->
phase_[phase].phase_angle_sensor_ !=
nullptr) {
106 for (uint8_t phase = 0; phase < 3; phase++) {
107 if (this->
phase_[phase].harmonic_active_power_sensor_ !=
nullptr) {
112 for (uint8_t phase = 0; phase < 3; phase++) {
113 if (this->
phase_[phase].peak_current_sensor_ !=
nullptr) {
127 if (this->
read16_(ATM90E32_REGISTER_METEREN) != 1) {
136 ESP_LOGCONFIG(TAG,
"Setting up ATM90E32 Component...");
139 uint16_t mmode0 = 0x87;
149 this->
write16_(ATM90E32_REGISTER_SOFTRESET, 0x789A);
151 this->
write16_(ATM90E32_REGISTER_CFGREGACCEN, 0x55AA);
152 if (this->
read16_(ATM90E32_REGISTER_LASTSPIDATA) != 0x55AA) {
153 ESP_LOGW(TAG,
"Could not initialize ATM90E32 IC, check SPI settings");
158 this->
write16_(ATM90E32_REGISTER_METEREN, 0x0001);
159 this->
write16_(ATM90E32_REGISTER_SAGPEAKDETCFG, 0xFF3F);
160 this->
write16_(ATM90E32_REGISTER_PLCONSTH, 0x0861);
161 this->
write16_(ATM90E32_REGISTER_PLCONSTL, 0xC468);
162 this->
write16_(ATM90E32_REGISTER_ZXCONFIG, 0xD654);
163 this->
write16_(ATM90E32_REGISTER_MMODE0, mmode0);
165 this->
write16_(ATM90E32_REGISTER_PSTARTTH, 0x1D4C);
166 this->
write16_(ATM90E32_REGISTER_QSTARTTH, 0x1D4C);
167 this->
write16_(ATM90E32_REGISTER_SSTARTTH, 0x1D4C);
168 this->
write16_(ATM90E32_REGISTER_PPHASETH, 0x02EE);
169 this->
write16_(ATM90E32_REGISTER_QPHASETH, 0x02EE);
194 this->
write16_(ATM90E32_REGISTER_CFGREGACCEN, 0x0000);
198 ESP_LOGCONFIG(
"",
"ATM90E32:");
199 LOG_PIN(
" CS Pin: ", this->
cs_);
201 ESP_LOGE(TAG,
"Communication with ATM90E32 failed!");
203 LOG_UPDATE_INTERVAL(
this);
204 LOG_SENSOR(
" ",
"Voltage A", this->
phase_[
PHASEA].voltage_sensor_);
205 LOG_SENSOR(
" ",
"Current A", this->
phase_[
PHASEA].current_sensor_);
206 LOG_SENSOR(
" ",
"Power A", this->
phase_[
PHASEA].power_sensor_);
207 LOG_SENSOR(
" ",
"Reactive Power A", this->
phase_[
PHASEA].reactive_power_sensor_);
208 LOG_SENSOR(
" ",
"PF A", this->
phase_[
PHASEA].power_factor_sensor_);
209 LOG_SENSOR(
" ",
"Active Forward Energy A", this->
phase_[
PHASEA].forward_active_energy_sensor_);
210 LOG_SENSOR(
" ",
"Active Reverse Energy A", this->
phase_[
PHASEA].reverse_active_energy_sensor_);
211 LOG_SENSOR(
" ",
"Harmonic Power A", this->
phase_[
PHASEA].harmonic_active_power_sensor_);
212 LOG_SENSOR(
" ",
"Phase Angle A", this->
phase_[
PHASEA].phase_angle_sensor_);
213 LOG_SENSOR(
" ",
"Peak Current A", this->
phase_[
PHASEA].peak_current_sensor_);
214 LOG_SENSOR(
" ",
"Voltage B", this->
phase_[
PHASEB].voltage_sensor_);
215 LOG_SENSOR(
" ",
"Current B", this->
phase_[
PHASEB].current_sensor_);
216 LOG_SENSOR(
" ",
"Power B", this->
phase_[
PHASEB].power_sensor_);
217 LOG_SENSOR(
" ",
"Reactive Power B", this->
phase_[
PHASEB].reactive_power_sensor_);
218 LOG_SENSOR(
" ",
"PF B", this->
phase_[
PHASEB].power_factor_sensor_);
219 LOG_SENSOR(
" ",
"Active Forward Energy B", this->
phase_[
PHASEB].forward_active_energy_sensor_);
220 LOG_SENSOR(
" ",
"Active Reverse Energy B", this->
phase_[
PHASEB].reverse_active_energy_sensor_);
221 LOG_SENSOR(
" ",
"Harmonic Power A", this->
phase_[
PHASEB].harmonic_active_power_sensor_);
222 LOG_SENSOR(
" ",
"Phase Angle A", this->
phase_[
PHASEB].phase_angle_sensor_);
223 LOG_SENSOR(
" ",
"Peak Current A", this->
phase_[
PHASEB].peak_current_sensor_);
224 LOG_SENSOR(
" ",
"Voltage C", this->
phase_[
PHASEC].voltage_sensor_);
225 LOG_SENSOR(
" ",
"Current C", this->
phase_[
PHASEC].current_sensor_);
226 LOG_SENSOR(
" ",
"Power C", this->
phase_[
PHASEC].power_sensor_);
227 LOG_SENSOR(
" ",
"Reactive Power C", this->
phase_[
PHASEC].reactive_power_sensor_);
228 LOG_SENSOR(
" ",
"PF C", this->
phase_[
PHASEC].power_factor_sensor_);
229 LOG_SENSOR(
" ",
"Active Forward Energy C", this->
phase_[
PHASEC].forward_active_energy_sensor_);
230 LOG_SENSOR(
" ",
"Active Reverse Energy C", this->
phase_[
PHASEC].reverse_active_energy_sensor_);
231 LOG_SENSOR(
" ",
"Harmonic Power A", this->
phase_[
PHASEC].harmonic_active_power_sensor_);
232 LOG_SENSOR(
" ",
"Phase Angle A", this->
phase_[
PHASEC].phase_angle_sensor_);
233 LOG_SENSOR(
" ",
"Peak Current A", this->
phase_[
PHASEC].peak_current_sensor_);
244 uint8_t addrh = (1 << 7) | ((a_register >> 8) & 0x03);
245 uint8_t addrl = (a_register & 0xFF);
255 output = (uint16_t(data[0] & 0xFF) << 8) | (data[1] & 0xFF);
256 ESP_LOGVV(TAG,
"read16_ 0x%04" PRIX16
" output 0x%04" PRIX16, a_register, output);
261 const uint16_t val_h = this->
read16_(addr_h);
262 const uint16_t val_l = this->
read16_(addr_l);
263 const int32_t
val = (val_h << 16) | val_l;
266 "read32_ addr_h 0x%04" PRIX16
" val_h 0x%04" PRIX16
" addr_l 0x%04" PRIX16
" val_l 0x%04" PRIX16
268 addr_h, val_h, addr_l, val_l, val);
274 ESP_LOGVV(TAG,
"write16_ 0x%04" PRIX16
" val 0x%04" PRIX16, a_register, val);
279 if (this->
read16_(ATM90E32_REGISTER_LASTSPIDATA) != val)
280 ESP_LOGW(TAG,
"SPI write error 0x%04X val 0x%04X", a_register, val);
310 const uint16_t voltage = this->
read16_(ATM90E32_REGISTER_URMS + phase);
311 if (this->
read16_(ATM90E32_REGISTER_LASTSPIDATA) != voltage)
312 ESP_LOGW(TAG,
"SPI URMS voltage register read error.");
313 return (
float) voltage / 100;
317 const uint8_t reads = 10;
318 uint32_t accumulation = 0;
319 uint16_t voltage = 0;
320 for (uint8_t i = 0; i < reads; i++) {
321 voltage = this->
read16_(ATM90E32_REGISTER_URMS + phase);
322 if (this->
read16_(ATM90E32_REGISTER_LASTSPIDATA) != voltage)
323 ESP_LOGW(TAG,
"SPI URMS voltage register read error.");
324 accumulation += voltage;
326 voltage = accumulation / reads;
332 const uint8_t reads = 10;
333 uint32_t accumulation = 0;
334 uint16_t current = 0;
335 for (uint8_t i = 0; i < reads; i++) {
336 current = this->
read16_(ATM90E32_REGISTER_IRMS + phase);
337 if (this->
read16_(ATM90E32_REGISTER_LASTSPIDATA) != current)
338 ESP_LOGW(TAG,
"SPI IRMS current register read error.");
339 accumulation += current;
341 current = accumulation / reads;
347 const uint16_t current = this->
read16_(ATM90E32_REGISTER_IRMS + phase);
348 if (this->
read16_(ATM90E32_REGISTER_LASTSPIDATA) != current)
349 ESP_LOGW(TAG,
"SPI IRMS current register read error.");
350 return (
float) current / 1000;
354 const int val = this->
read32_(ATM90E32_REGISTER_PMEAN + phase, ATM90E32_REGISTER_PMEANLSB + phase);
355 return val * 0.00032f;
359 const int val = this->
read32_(ATM90E32_REGISTER_QMEAN + phase, ATM90E32_REGISTER_QMEANLSB + phase);
360 return val * 0.00032f;
364 const int16_t powerfactor = this->
read16_(ATM90E32_REGISTER_PFMEAN + phase);
365 if (this->
read16_(ATM90E32_REGISTER_LASTSPIDATA) != powerfactor)
366 ESP_LOGW(TAG,
"SPI power factor read error.");
367 return (
float) powerfactor / 1000;
371 const uint16_t
val = this->
read16_(ATM90E32_REGISTER_APENERGY + phase);
372 if ((UINT32_MAX - this->
phase_[phase].cumulative_forward_active_energy_) > val) {
377 return ((
float) this->
phase_[phase].cumulative_forward_active_energy_ * 10 / 3200);
381 const uint16_t
val = this->
read16_(ATM90E32_REGISTER_ANENERGY);
382 if (UINT32_MAX - this->
phase_[phase].cumulative_reverse_active_energy_ > val) {
387 return ((
float) this->
phase_[phase].cumulative_reverse_active_energy_ * 10 / 3200);
391 int val = this->
read32_(ATM90E32_REGISTER_PMEANH + phase, ATM90E32_REGISTER_PMEANHLSB + phase);
392 return val * 0.00032f;
396 uint16_t
val = this->
read16_(ATM90E32_REGISTER_PANGLE + phase) / 10.0;
397 return (
float) (val > 180) ? val - 360.0 : val;
401 int16_t
val = (float) this->
read16_(ATM90E32_REGISTER_IPEAK + phase);
409 const uint16_t freq = this->
read16_(ATM90E32_REGISTER_FREQ);
410 return (
float) freq / 100;
414 const uint16_t ctemp = this->
read16_(ATM90E32_REGISTER_TEMP);
415 return (
float) ctemp;
419 const uint8_t num_reads = 5;
420 uint64_t total_value = 0;
421 for (
int i = 0; i < num_reads; ++i) {
422 const uint32_t measurement_value =
read32_(ATM90E32_REGISTER_URMS + phase, ATM90E32_REGISTER_URMSLSB + phase);
423 total_value += measurement_value;
425 const uint32_t average_value = total_value / num_reads;
426 const uint32_t shifted_value = average_value >> 7;
427 const uint32_t voltage_offset = ~shifted_value + 1;
428 return voltage_offset & 0xFFFF;
432 const uint8_t num_reads = 5;
433 uint64_t total_value = 0;
434 for (
int i = 0; i < num_reads; ++i) {
435 const uint32_t measurement_value =
read32_(ATM90E32_REGISTER_IRMS + phase, ATM90E32_REGISTER_IRMSLSB + phase);
436 total_value += measurement_value;
438 const uint32_t average_value = total_value / num_reads;
439 const uint32_t current_offset = ~average_value + 1;
440 return current_offset & 0xFFFF;
float get_local_phase_active_power_(uint8_t)
sensor::Sensor * voltage_sensor_
float reverse_active_energy_
sensor::Sensor * forward_active_energy_sensor_
float get_phase_power_factor_(uint8_t)
float get_setup_priority() const override
float get_local_phase_current_(uint8_t)
void write16_(uint16_t a_register, uint16_t val)
void status_set_warning(const char *message="unspecified")
float get_phase_reactive_power_(uint8_t)
uint32_t cumulative_reverse_active_energy_
void spi_setup() override
int read32_(uint16_t addr_h, uint16_t addr_l)
sensor::Sensor * current_sensor_
void write_byte(uint8_t data)
void delay_microseconds_safe(uint32_t us)
Delay for the given amount of microseconds, possibly yielding to other processes during the wait...
static const uint8_t PHASEC
static const uint8_t PHASEB
float get_chip_temperature_()
bool peak_current_signed_
float get_phase_forward_active_energy_(uint8_t)
sensor::Sensor * harmonic_active_power_sensor_
void dump_config() override
void read_array(uint8_t *data, size_t length)
static const uint8_t PHASEA
float get_phase_peak_current_(uint8_t)
float get_phase_current_avg_(uint8_t)
sensor::Sensor * phase_angle_sensor_
void status_clear_warning()
float forward_active_energy_
void publish_state(float state)
Publish a new state to the front-end.
float get_local_phase_voltage_(uint8_t)
struct esphome::atm90e32::ATM90E32Component::ATM90E32Phase phase_[3]
float get_local_phase_forward_active_energy_(uint8_t)
uint16_t calibrate_voltage_offset_phase(uint8_t)
sensor::Sensor * peak_current_sensor_
uint32_t cumulative_forward_active_energy_
float get_phase_current_(uint8_t)
sensor::Sensor * reverse_active_energy_sensor_
float get_phase_reverse_active_energy_(uint8_t)
float get_local_phase_peak_current_(uint8_t)
float get_phase_voltage_avg_(uint8_t)
float get_phase_active_power_(uint8_t)
float get_phase_voltage_(uint8_t)
bool get_publish_interval_flag_()
float get_phase_harmonic_active_power_(uint8_t)
float harmonic_active_power_
uint16_t read16_(uint16_t a_register)
virtual void mark_failed()
Mark this component as failed.
const float IO
For components that represent GPIO pins like PCF8573.
sensor::Sensor * chip_temperature_sensor_
float get_phase_angle_(uint8_t)
float get_local_phase_reverse_active_energy_(uint8_t)
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
uint16_t calibrate_current_offset_phase(uint8_t)
sensor::Sensor * power_sensor_
void write_byte16(uint16_t data)
Write 16 bit data.
sensor::Sensor * freq_sensor_
sensor::Sensor * reactive_power_sensor_
float get_local_phase_harmonic_active_power_(uint8_t)
float get_local_phase_power_factor_(uint8_t)
void set_publish_interval_flag_(bool flag)
float get_local_phase_angle_(uint8_t)
float get_local_phase_reactive_power_(uint8_t)
void IRAM_ATTR HOT delay(uint32_t ms)
sensor::Sensor * power_factor_sensor_