17void PZEM004T::loop() {
27 if (!resp_opt.has_value())
29 auto resp = *resp_opt;
35 const uint8_t sum =
sum_bytes(resp.data(), 6);
38 ESP_LOGV(TAG,
"PZEM004T invalid checksum! 0x%02X != 0x%02X", sum, resp[6]);
49 float voltage = int_voltage + (resp[3] / 10.0f);
50 PUBLISH_STATE(this->voltage_sensor_, voltage);
51 ESP_LOGD(TAG,
"Got Voltage %.1f V", voltage);
57 float current = int_current + (resp[3] / 100.0f);
58 PUBLISH_STATE(this->current_sensor_, current);
59 ESP_LOGD(TAG,
"Got Current %.2f A", current);
65 PUBLISH_STATE(this->power_sensor_, power);
66 ESP_LOGD(TAG,
"Got Power %u W", power);
73 PUBLISH_STATE(this->energy_sensor_, energy);
74 ESP_LOGD(TAG,
"Got Energy %" PRIu32
" Wh", energy);
96 this->read_state_ =
state;
99 std::array<uint8_t, 7> data{};
109 this->read_state_ =
state;
111void PZEM004T::dump_config() {
112 ESP_LOGCONFIG(TAG,
"PZEM004T:");
113 LOG_SENSOR(
"",
"Voltage", this->voltage_sensor_);
114 LOG_SENSOR(
"",
"Current", this->current_sensor_);
115 LOG_SENSOR(
"",
"Power", this->power_sensor_);
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
virtual void setup()
Where the component's initialization should happen.
enum esphome::pzem004t::PZEM004T::PZEM004TReadState DONE
void write_state_(PZEM004TReadState state)
optional< std::array< uint8_t, N > > read_array()
void discard_available()
Read and drop every byte waiting in the receive buffer.
void write_array(const uint8_t *data, size_t len)
ESPHOME_LOG_TAG(TAG, "pzem004t")
constexpr uint8_t sum_bytes(const uint8_t *data, size_t len, uint8_t sum=0)
Add up the bytes of data with size len, truncated to 8 bits: the checksum many serial and IR protocol...
constexpr uint32_t encode_uint24(uint8_t byte1, uint8_t byte2, uint8_t byte3)
Encode a 24-bit value given three bytes in most to least significant byte order.
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
Application App
Global storage of Application pointer - only one Application can exist.