6namespace helpers = modbus::helpers;
10static const uint8_t PZEM_CMD_RESET_ENERGY = 0x42;
11static const uint8_t PZEM_REGISTER_COUNT = 10;
15static const uint16_t PZEM_REGISTER_VOLTAGE = 0;
16static const uint16_t PZEM_REGISTER_CURRENT = 1;
17static const uint16_t PZEM_REGISTER_ACTIVE_POWER = 3;
18static const uint16_t PZEM_REGISTER_ACTIVE_ENERGY = 5;
19static const uint16_t PZEM_REGISTER_FREQUENCY = 7;
20static const uint16_t PZEM_REGISTER_POWER_FACTOR = 8;
29 auto publish_1_register = [&](
sensor::Sensor *sensor, uint16_t reg,
float divisor) ->
void {
30 if (sensor ==
nullptr)
36 auto publish_2_registers = [&](
sensor::Sensor *sensor, uint16_t reg,
float divisor) ->
void {
37 if (sensor ==
nullptr)
43 publish_1_register(this->voltage_sensor_, PZEM_REGISTER_VOLTAGE, 10.0f);
44 publish_2_registers(this->current_sensor_, PZEM_REGISTER_CURRENT, 1000.0f);
45 publish_2_registers(this->power_sensor_, PZEM_REGISTER_ACTIVE_POWER, 10.0f);
46 publish_2_registers(this->energy_sensor_, PZEM_REGISTER_ACTIVE_ENERGY, 1.0f);
47 publish_1_register(this->frequency_sensor_, PZEM_REGISTER_FREQUENCY, 10.0f);
48 publish_1_register(this->power_factor_sensor_, PZEM_REGISTER_POWER_FACTOR, 100.0f);
55 if (!request_pdu.empty() && request_pdu[0] == PZEM_CMD_RESET_ENERGY) {
57 ESP_LOGD(TAG,
"Energy reset acknowledged");
59 ESP_LOGW(TAG,
"Energy reset rejected");
66void PZEMAC::update() { this->read_input_registers(0, PZEM_REGISTER_COUNT); }
72 LOG_SENSOR(
"",
"Voltage", this->voltage_sensor_);
73 LOG_SENSOR(
"",
"Current", this->current_sensor_);
74 LOG_SENSOR(
"",
"Power", this->power_sensor_);
75 LOG_SENSOR(
"",
"Energy", this->energy_sensor_);
76 LOG_SENSOR(
"",
"Frequency", this->frequency_sensor_);
77 LOG_SENSOR(
"",
"Power Factor", this->power_factor_sensor_);
81 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...
void on_custom_response(std::span< const uint8_t > request_pdu, std::span< const uint8_t > response_pdu, modbus::ResponseStatus status) override
void on_read_input_registers(uint16_t start_address, std::span< const uint16_t > registers, modbus::ResponseStatus status) override
void dump_config() override
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.
ESPHOME_LOG_TAG(TAG, "pzemac")