@@ -6,52 +6,52 @@ namespace pzemac {
66
77static const char *const TAG = " pzemac" ;
88
9- static const uint8_t PZEM_CMD_READ_IN_REGISTERS = 0x04 ;
10- static const uint8_t PZEM_CMD_RESET_ENERGY = 0x42 ;
11- static const uint8_t PZEM_REGISTER_COUNT = 10 ; // 10x 16-bit registers
9+ static const uint8_t PZEpre_CMD_READ_IN_REGISTERS = 0x04 ;
10+ static const uint8_t PZEpre_CMD_RESET_ENERGY = 0x42 ;
11+ static const uint8_t PZEpre_REGISTER_COUNT = 10 ; // 10x 16-bit registers
1212
1313void PZEMAC::on_modbus_data (const std::vector<uint8_t > &data) {
1414 if (data.size () < 20 ) {
1515 ESP_LOGW (TAG, " Invalid size for PZEM AC!" );
1616 return ;
1717 }
18- this ->m_last_update_time_ = millis ();
18+ this ->pre_last_update_time_ = millis ();
1919
2020 // See https://github.com/esphome/feature-requests/issues/49#issuecomment-538636809
2121 // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
2222 // 01 04 14 08 D1 00 6C 00 00 00 F4 00 00 00 26 00 00 01 F4 00 64 00 00 51 34
2323 // Id Cc Sz Volt- Current---- Power------ Energy----- Frequ PFact Alarm Crc--
2424 // 0 2 6 10 14 16
2525
26- auto pzem_get_16bit = [&](size_t i) -> uint16_t {
26+ auto pzepre_get_16bit = [&](size_t i) -> uint16_t {
2727 return (uint16_t (data[i + 0 ]) << 8 ) | (uint16_t (data[i + 1 ]) << 0 );
2828 };
29- auto pzem_get_32bit = [&](size_t i) -> uint32_t {
30- return (uint32_t (pzem_get_16bit (i + 2 )) << 16 ) | (uint32_t (pzem_get_16bit (i + 0 )) << 0 );
29+ auto pzepre_get_32bit = [&](size_t i) -> uint32_t {
30+ return (uint32_t (pzepre_get_16bit (i + 2 )) << 16 ) | (uint32_t (pzepre_get_16bit (i + 0 )) << 0 );
3131 };
3232
33- uint16_t raw_voltage = pzem_get_16bit (0 );
33+ uint16_t raw_voltage = pzepre_get_16bit (0 );
3434 float voltage = raw_voltage / 10 .0f ; // max 6553.5 V
3535
36- uint32_t raw_current = pzem_get_32bit (2 );
36+ uint32_t raw_current = pzepre_get_32bit (2 );
3737 float current = raw_current / 1000 .0f ; // max 4294967.295 A
3838
39- uint32_t raw_active_power = pzem_get_32bit (6 );
39+ uint32_t raw_active_power = pzepre_get_32bit (6 );
4040 float active_power = raw_active_power / 10 .0f ; // max 429496729.5 W
4141
42- float active_energy = static_cast <float >(pzem_get_32bit (10 ));
42+ float active_energy = static_cast <float >(pzepre_get_32bit (10 ));
4343
44- uint16_t raw_frequency = pzem_get_16bit (14 );
44+ uint16_t raw_frequency = pzepre_get_16bit (14 );
4545 float frequency = raw_frequency / 10 .0f ;
4646
47- uint16_t raw_power_factor = pzem_get_16bit (16 );
47+ uint16_t raw_power_factor = pzepre_get_16bit (16 );
4848 float power_factor = raw_power_factor / 100 .0f ;
4949
5050 ESP_LOGD (TAG,
5151 " PZEM AC: Addr 0x%02X, V=%.1f V, I=%.3f A, P=%.1f W, E=%.1f Wh, E(pre)=%.1f Wh, E-E(pre)=%.1f Wh, F=%.1f "
5252 " Hz, PF=%.2f" ,
53- int (this ->address_ ), voltage, current, active_power, active_energy, this ->m_last_energy_sensor ,
54- active_energy - this ->m_last_energy_sensor , frequency, power_factor);
53+ int (this ->address_ ), voltage, current, active_power, active_energy, this ->pre_last_energy_sensor ,
54+ active_energy - this ->pre_last_energy_sensor , frequency, power_factor);
5555 if (this ->voltage_sensor_ != nullptr ) {
5656 if (voltage < 450 ) {
5757 this ->voltage_sensor_ ->publish_state (voltage);
@@ -74,15 +74,15 @@ void PZEMAC::on_modbus_data(const std::vector<uint8_t> &data) {
7474 }
7575 }
7676 if (this ->energy_sensor_ != nullptr ) {
77- if (this ->m_last_energy_sensor == 0 ) {
77+ if (this ->pre_last_energy_sensor == 0 ) {
7878 this ->energy_sensor_ ->publish_state (active_energy);
79- this ->m_last_energy_sensor = active_energy;
79+ this ->pre_last_energy_sensor = active_energy;
8080 } else {
81- if (abs (active_energy - this ->m_last_energy_sensor ) < 1000 ) {
81+ if (abs (active_energy - this ->pre_last_energy_sensor ) < 1000 ) {
8282 this ->energy_sensor_ ->publish_state (active_energy);
83- this ->m_last_energy_sensor = active_energy;
83+ this ->pre_last_energy_sensor = active_energy;
8484 } else {
85- this ->energy_sensor_ ->publish_state (this ->m_last_energy_sensor );
85+ this ->energy_sensor_ ->publish_state (this ->pre_last_energy_sensor );
8686 }
8787 }
8888 }
@@ -95,10 +95,10 @@ void PZEMAC::on_modbus_data(const std::vector<uint8_t> &data) {
9595}
9696
9797void PZEMAC::update () {
98- this ->send (PZEM_CMD_READ_IN_REGISTERS , 0 , PZEM_REGISTER_COUNT );
98+ this ->send (PZEpre_CMD_READ_IN_REGISTERS , 0 , PZEpre_REGISTER_COUNT );
9999
100100 if (this ->get_update_interval () != SCHEDULER_DONT_RUN &&
101- (millis () - this ->m_last_update_time_ ) > this ->get_update_interval () * 2 ) {
101+ (millis () - this ->pre_last_update_time_ ) > this ->get_update_interval () * 2 ) {
102102 ESP_LOGE (TAG, " PZEM AC Addr 0x%02X: Timeout!!!" , int (this ->address_ ));
103103 if (this ->voltage_sensor_ != nullptr ) {
104104 this ->voltage_sensor_ ->publish_state (0 .0f );
@@ -110,7 +110,7 @@ void PZEMAC::update() {
110110 this ->power_sensor_ ->publish_state (0 .0f );
111111 }
112112 if (this ->energy_sensor_ != nullptr ) {
113- this ->energy_sensor_ ->publish_state (this ->m_last_energy_sensor );
113+ this ->energy_sensor_ ->publish_state (this ->pre_last_energy_sensor );
114114 }
115115 if (this ->frequency_sensor_ != nullptr ) {
116116 this ->frequency_sensor_ ->publish_state (0 .0f );
@@ -135,7 +135,7 @@ void PZEMAC::dump_config() {
135135void PZEMAC::reset_energy_ () {
136136 std::vector<uint8_t > cmd;
137137 cmd.push_back (this ->address_ );
138- cmd.push_back (PZEM_CMD_RESET_ENERGY );
138+ cmd.push_back (PZEpre_CMD_RESET_ENERGY );
139139 this ->send_raw (cmd);
140140}
141141
0 commit comments