7 static const char *
const TAG =
"pzemdc";
9 static const uint8_t PZEM_CMD_READ_IN_REGISTERS = 0x04;
10 static const uint8_t PZEM_REGISTER_COUNT = 10;
13 if (data.size() < 16) {
14 ESP_LOGW(TAG,
"Invalid size for PZEM DC!");
24 auto pzem_get_16bit = [&](
size_t i) -> uint16_t {
25 return (uint16_t(data[i + 0]) << 8) | (uint16_t(data[i + 1]) << 0);
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);
31 uint16_t raw_voltage = pzem_get_16bit(0);
32 float voltage = raw_voltage / 100.0f;
34 uint16_t raw_current = pzem_get_16bit(2);
35 float current = raw_current / 100.0f;
37 uint32_t raw_power = pzem_get_32bit(4);
38 float power = raw_power / 10.0f;
40 ESP_LOGD(TAG,
"PZEM DC: V=%.1f V, I=%.3f A, P=%.1f W", voltage, current, power);
51 ESP_LOGCONFIG(TAG,
"PZEMDC:");
52 ESP_LOGCONFIG(TAG,
" Address: 0x%02X", this->
address_);
void dump_config() override
sensor::Sensor * power_sensor_
sensor::Sensor * current_sensor_
void publish_state(float state)
Publish a new state to the front-end.
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_