8 static const char *
const TAG =
"pzem004t";
19 const uint32_t now =
millis();
28 auto resp = *this->read_array<7>();
35 for (
int i = 0; i < 6; i++)
39 ESP_LOGV(TAG,
"PZEM004T invalid checksum! 0x%02X != 0x%02X", sum, resp[6]);
49 uint16_t int_voltage = (uint16_t(resp[1]) << 8) | (uint16_t(resp[2]) << 0);
50 float voltage = int_voltage + (resp[3] / 10.0f);
53 ESP_LOGD(TAG,
"Got Voltage %.1f V", voltage);
58 uint16_t int_current = (uint16_t(resp[1]) << 8) | (uint16_t(resp[2]) << 0);
59 float current = int_current + (resp[3] / 100.0f);
62 ESP_LOGD(TAG,
"Got Current %.2f A", current);
67 uint16_t power = (uint16_t(resp[1]) << 8) | (uint16_t(resp[2]) << 0);
70 ESP_LOGD(TAG,
"Got Power %u W", power);
76 uint32_t energy = (uint32_t(resp[1]) << 16) | (uint32_t(resp[2]) << 8) | (uint32_t(resp[3]));
79 ESP_LOGD(TAG,
"Got Energy %" PRIu32
" Wh", energy);
101 this->read_state_ =
state;
104 std::array<uint8_t, 7> data{};
112 for (
int i = 0; i < 6; i++)
116 this->read_state_ =
state;
119 ESP_LOGCONFIG(TAG,
"PZEM004T:");
void write_array(const uint8_t *data, size_t len)
sensor::Sensor * power_sensor_
sensor::Sensor * voltage_sensor_
sensor::Sensor * energy_sensor_
uint32_t IRAM_ATTR HOT millis()
sensor::Sensor * current_sensor_
void publish_state(float state)
Publish a new state to the front-end.
void dump_config() override
Implementation of SPI Controller mode.
void write_state_(PZEM004TReadState state)
enum esphome::pzem004t::PZEM004T::PZEM004TReadState DONE