ESPHome  2024.9.2
pzemdc.cpp
Go to the documentation of this file.
1 #include "pzemdc.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace pzemdc {
6 
7 static const char *const TAG = "pzemdc";
8 
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
12 
13 void PZEMDC::on_modbus_data(const std::vector<uint8_t> &data) {
14  if (data.size() < 16) {
15  ESP_LOGW(TAG, "Invalid size for PZEM DC!");
16  return;
17  }
18 
19  // See https://github.com/esphome/feature-requests/issues/49#issuecomment-538636809
20  // 0 1 2 3 4 5 6 7 = ModBus register
21  // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 = Buffer index
22  // 01 04 10 05 40 00 0A 00 0D 00 00 00 02 00 00 00 00 00 00 D6 29
23  // Id Cc Sz Volt- Curre Power------ Energy----- HiAlm LoAlm Crc--
24 
25  auto pzem_get_16bit = [&](size_t i) -> uint16_t {
26  return (uint16_t(data[i + 0]) << 8) | (uint16_t(data[i + 1]) << 0);
27  };
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);
30  };
31 
32  uint16_t raw_voltage = pzem_get_16bit(0);
33  float voltage = raw_voltage / 100.0f; // max 655.35 V
34 
35  uint16_t raw_current = pzem_get_16bit(2);
36  float current = raw_current / 100.0f; // max 655.35 A
37 
38  uint32_t raw_power = pzem_get_32bit(4);
39  float power = raw_power / 10.0f; // max 429496729.5 W
40 
41  uint32_t raw_energy = pzem_get_32bit(8);
42  float energy = raw_energy / 1000.0f; // max 4294967.295 kWh
43 
44  ESP_LOGD(TAG, "PZEM DC: V=%.1f V, I=%.3f A, P=%.1f W", voltage, current, power);
45  if (this->voltage_sensor_ != nullptr)
46  this->voltage_sensor_->publish_state(voltage);
47  if (this->current_sensor_ != nullptr)
48  this->current_sensor_->publish_state(current);
49  if (this->power_sensor_ != nullptr)
50  this->power_sensor_->publish_state(power);
51  if (this->energy_sensor_ != nullptr)
52  this->energy_sensor_->publish_state(energy);
53 }
54 
55 void PZEMDC::update() { this->send(PZEM_CMD_READ_IN_REGISTERS, 0, 8); }
57  ESP_LOGCONFIG(TAG, "PZEMDC:");
58  ESP_LOGCONFIG(TAG, " Address: 0x%02X", this->address_);
59  LOG_SENSOR("", "Voltage", this->voltage_sensor_);
60  LOG_SENSOR("", "Current", this->current_sensor_);
61  LOG_SENSOR("", "Power", this->power_sensor_);
62  LOG_SENSOR("", "Energy", this->energy_sensor_);
63 }
64 
66  std::vector<uint8_t> cmd;
67  cmd.push_back(this->address_);
68  cmd.push_back(PZEM_CMD_RESET_ENERGY);
69  this->send_raw(cmd);
70 }
71 
72 } // namespace pzemdc
73 } // namespace esphome
void dump_config() override
Definition: pzemdc.cpp:56
sensor::Sensor * power_sensor_
Definition: pzemdc.h:31
u_int8_t raw_voltage
sensor::Sensor * current_sensor_
Definition: pzemdc.h:30
sensor::Sensor * energy_sensor_
Definition: pzemdc.h:32
void send_raw(const std::vector< uint8_t > &payload)
Definition: modbus.h:66
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:39
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
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:62
void on_modbus_data(const std::vector< uint8_t > &data) override
Definition: pzemdc.cpp:13
sensor::Sensor * voltage_sensor_
Definition: pzemdc.h:29
void update() override
Definition: pzemdc.cpp:55
stm32_cmd_t * cmd
Definition: stm32flash.h:96