6namespace helpers = modbus::helpers;
8static const char *
const TAG =
"pzemdc";
10static const uint8_t PZEM_CMD_RESET_ENERGY = 0x42;
11static const uint8_t PZEM_REGISTER_COUNT = 8;
15static const uint16_t PZEM_REGISTER_VOLTAGE = 0;
16static const uint16_t PZEM_REGISTER_CURRENT = 1;
17static const uint16_t PZEM_REGISTER_POWER = 2;
18static const uint16_t PZEM_REGISTER_ENERGY = 4;
27 auto publish_1_register = [&](
sensor::Sensor *sensor, uint16_t reg,
float divisor) ->
void {
28 if (sensor ==
nullptr)
34 auto publish_2_registers = [&](
sensor::Sensor *sensor, uint16_t reg,
float divisor) ->
void {
35 if (sensor ==
nullptr)
41 publish_1_register(this->
voltage_sensor_, PZEM_REGISTER_VOLTAGE, 100.0f);
42 publish_1_register(this->
current_sensor_, PZEM_REGISTER_CURRENT, 100.0f);
43 publish_2_registers(this->
power_sensor_, PZEM_REGISTER_POWER, 10.0f);
44 publish_2_registers(this->
energy_sensor_, PZEM_REGISTER_ENERGY, 1000.0f);
51 if (!request_pdu.empty() && request_pdu[0] == PZEM_CMD_RESET_ENERGY) {
53 ESP_LOGD(TAG,
"Energy reset acknowledged");
55 ESP_LOGW(TAG,
"Energy reset rejected");
75 const uint8_t pdu[] = {PZEM_CMD_RESET_ENERGY};
virtual void on_custom_response(std::span< const uint8_t > request_pdu, std::span< const uint8_t > response_pdu, ResponseStatus status)
Catch-all for custom function codes and anything that is not a standard-conformant transaction (see d...
sensor::Sensor * power_sensor_
void on_read_input_registers(uint16_t start_address, std::span< const uint16_t > registers, modbus::ResponseStatus status) override
sensor::Sensor * current_sensor_
void on_custom_response(std::span< const uint8_t > request_pdu, std::span< const uint8_t > response_pdu, modbus::ResponseStatus status) override
void dump_config() override
sensor::Sensor * voltage_sensor_
sensor::Sensor * energy_sensor_
Base-class for all sensors.
void publish_state(float state)
Publish a new state to the front-end.
constexpr std::optional< RegisterValueType< VALUE_TYPE > > value_at(std::span< const uint16_t > registers, uint16_t start_address, uint16_t address)
The value stored at an absolute register address, or nullopt when it is not wholly inside this respon...
std::optional< ExceptionCode > ResponseStatus
bool succeeded(ResponseStatus status)
True when a transaction carried no exception.