7 static const char *
const TAG =
"pzemac";
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;
14 if (data.size() < 20) {
15 ESP_LOGW(TAG,
"Invalid size for PZEM AC!");
25 auto pzem_get_16bit = [&](
size_t i) -> uint16_t {
26 return (uint16_t(data[i + 0]) << 8) | (uint16_t(data[i + 1]) << 0);
28 auto pzem_get_32bit = [&](
size_t i) -> uint32_t {
29 return (uint32_t(pzem_get_16bit(i + 2)) << 16) | (uint32_t(pzem_get_16bit(i + 0)) << 0);
33 float voltage = raw_voltage / 10.0f;
35 uint32_t raw_current = pzem_get_32bit(2);
36 float current = raw_current / 1000.0f;
38 uint32_t raw_active_power = pzem_get_32bit(6);
39 float active_power = raw_active_power / 10.0f;
41 float active_energy =
static_cast<float>(pzem_get_32bit(10));
43 uint16_t raw_frequency = pzem_get_16bit(14);
46 uint16_t raw_power_factor = pzem_get_16bit(16);
47 float power_factor = raw_power_factor / 100.0f;
49 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,
50 active_energy, frequency, power_factor);
67 ESP_LOGCONFIG(TAG,
"PZEMAC:");
68 ESP_LOGCONFIG(TAG,
" Address: 0x%02X", this->
address_);
78 std::vector<uint8_t>
cmd;
80 cmd.push_back(PZEM_CMD_RESET_ENERGY);
void dump_config() override
sensor::Sensor * frequency_sensor_
sensor::Sensor * power_sensor_
sensor::Sensor * energy_sensor_
void send_raw(const std::vector< uint8_t > &payload)
void publish_state(float state)
Publish a new state to the front-end.
void on_modbus_data(const std::vector< uint8_t > &data) override
sensor::Sensor * power_factor_sensor_
Implementation of SPI Controller mode.
sensor::Sensor * current_sensor_
void send(uint8_t function, uint16_t start_address, uint16_t number_of_entities, uint8_t payload_len=0, const uint8_t *payload=nullptr)
sensor::Sensor * voltage_sensor_