ESPHome 2026.5.0b1
Loading...
Searching...
No Matches
pzemac.cpp
Go to the documentation of this file.
1#include "pzemac.h"
2#include "esphome/core/log.h"
3
4namespace esphome::pzemac {
5
6static const char *const TAG = "pzemac";
7
8static const uint8_t PZEM_CMD_READ_IN_REGISTERS = 0x04;
9static const uint8_t PZEM_CMD_RESET_ENERGY = 0x42;
10static const uint8_t PZEM_REGISTER_COUNT = 10; // 10x 16-bit registers
11
12void PZEMAC::on_modbus_data(const std::vector<uint8_t> &data) {
13 if (data.size() < 20) {
14 ESP_LOGW(TAG, "Invalid size for PZEM AC!");
15 return;
16 }
17
18 // See https://github.com/esphome/feature-requests/issues/49#issuecomment-538636809
19 // 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
20 // 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
21 // Id Cc Sz Volt- Current---- Power------ Energy----- Frequ PFact Alarm Crc--
22 // 0 2 6 10 14 16
23
24 auto pzem_get_16bit = [&](size_t i) -> uint16_t {
25 return (uint16_t(data[i + 0]) << 8) | (uint16_t(data[i + 1]) << 0);
26 };
27 auto pzem_get_32bit = [&](size_t i) -> uint32_t {
28 return (uint32_t(pzem_get_16bit(i + 2)) << 16) | (uint32_t(pzem_get_16bit(i + 0)) << 0);
29 };
30
31 uint16_t raw_voltage = pzem_get_16bit(0);
32 float voltage = raw_voltage / 10.0f; // max 6553.5 V
33
34 uint32_t raw_current = pzem_get_32bit(2);
35 float current = raw_current / 1000.0f; // max 4294967.295 A
36
37 uint32_t raw_active_power = pzem_get_32bit(6);
38 float active_power = raw_active_power / 10.0f; // max 429496729.5 W
39
40 float active_energy = static_cast<float>(pzem_get_32bit(10));
41
42 uint16_t raw_frequency = pzem_get_16bit(14);
43 float frequency = raw_frequency / 10.0f;
44
45 uint16_t raw_power_factor = pzem_get_16bit(16);
46 float power_factor = raw_power_factor / 100.0f;
47
48 ESP_LOGD(TAG, "PZEM AC: V=%.1f V, I=%.3f A, P=%.1f W, E=%.1f Wh, F=%.1f Hz, PF=%.2f", voltage, current, active_power,
49 active_energy, frequency, power_factor);
50 if (this->voltage_sensor_ != nullptr)
51 this->voltage_sensor_->publish_state(voltage);
52 if (this->current_sensor_ != nullptr)
53 this->current_sensor_->publish_state(current);
54 if (this->power_sensor_ != nullptr)
55 this->power_sensor_->publish_state(active_power);
56 if (this->energy_sensor_ != nullptr)
57 this->energy_sensor_->publish_state(active_energy);
58 if (this->frequency_sensor_ != nullptr)
59 this->frequency_sensor_->publish_state(frequency);
60 if (this->power_factor_sensor_ != nullptr)
61 this->power_factor_sensor_->publish_state(power_factor);
62}
63
64void PZEMAC::update() { this->send(PZEM_CMD_READ_IN_REGISTERS, 0, PZEM_REGISTER_COUNT); }
66 ESP_LOGCONFIG(TAG,
67 "PZEMAC:\n"
68 " Address: 0x%02X",
69 this->address_);
70 LOG_SENSOR("", "Voltage", this->voltage_sensor_);
71 LOG_SENSOR("", "Current", this->current_sensor_);
72 LOG_SENSOR("", "Power", this->power_sensor_);
73 LOG_SENSOR("", "Energy", this->energy_sensor_);
74 LOG_SENSOR("", "Frequency", this->frequency_sensor_);
75 LOG_SENSOR("", "Power Factor", this->power_factor_sensor_);
76}
77
79 std::vector<uint8_t> cmd;
80 cmd.push_back(this->address_);
81 cmd.push_back(PZEM_CMD_RESET_ENERGY);
82 this->send_raw(cmd);
83}
84
85} // namespace esphome::pzemac
uint16_le_t frequency
Definition bl0942.h:6
void send_raw(const std::vector< uint8_t > &payload)
Definition modbus.h:103
void send(uint8_t function, uint16_t start_address, uint16_t number_of_entities, uint8_t payload_len=0, const uint8_t *payload=nullptr)
Definition modbus.h:99
sensor::Sensor * frequency_sensor_
Definition pzemac.h:35
sensor::Sensor * voltage_sensor_
Definition pzemac.h:31
void update() override
Definition pzemac.cpp:64
sensor::Sensor * energy_sensor_
Definition pzemac.h:34
sensor::Sensor * power_sensor_
Definition pzemac.h:33
void dump_config() override
Definition pzemac.cpp:65
sensor::Sensor * power_factor_sensor_
Definition pzemac.h:36
sensor::Sensor * current_sensor_
Definition pzemac.h:32
void on_modbus_data(const std::vector< uint8_t > &data) override
Definition pzemac.cpp:12
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
u_int8_t raw_voltage
static void uint32_t