7 static const char *
const TAG =
"pzemdc";
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() < 16) {
15 ESP_LOGW(TAG,
"Invalid size for PZEM DC!");
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 / 100.0f;
35 uint16_t raw_current = pzem_get_16bit(2);
36 float current = raw_current / 100.0f;
38 uint32_t raw_power = pzem_get_32bit(4);
39 float power = raw_power / 10.0f;
41 uint32_t raw_energy = pzem_get_32bit(8);
42 float energy = raw_energy / 1000.0f;
44 ESP_LOGD(TAG,
"PZEM DC: V=%.1f V, I=%.3f A, P=%.1f W", voltage, current, power);
57 ESP_LOGCONFIG(TAG,
"PZEMDC:");
58 ESP_LOGCONFIG(TAG,
" Address: 0x%02X", this->
address_);
66 std::vector<uint8_t>
cmd;
68 cmd.push_back(PZEM_CMD_RESET_ENERGY);
void dump_config() override
sensor::Sensor * power_sensor_
sensor::Sensor * current_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.
Implementation of SPI Controller mode.
void send(uint8_t function, uint16_t start_address, uint16_t number_of_entities, uint8_t payload_len=0, const uint8_t *payload=nullptr)
void on_modbus_data(const std::vector< uint8_t > &data) override
sensor::Sensor * voltage_sensor_