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]);
48 uint16_t int_voltage = (uint16_t(resp[1]) << 8) | (uint16_t(resp[2]) << 0);
49 float voltage = int_voltage + (resp[3] / 10.0f);
52 ESP_LOGD(TAG,
"Got Voltage %.1f V", voltage);
57 uint16_t int_current = (uint16_t(resp[1]) << 8) | (uint16_t(resp[2]) << 0);
58 float current = int_current + (resp[3] / 100.0f);
61 ESP_LOGD(TAG,
"Got Current %.2f A", current);
66 uint16_t power = (uint16_t(resp[1]) << 8) | (uint16_t(resp[2]) << 0);
69 ESP_LOGD(TAG,
"Got Power %u W", power);
78 ESP_LOGD(TAG,
"Got Energy %" PRIu32
" Wh", energy);
100 this->read_state_ =
state;
103 std::array<uint8_t, 7> data{};
111 for (
int i = 0; i < 6; i++)
115 this->read_state_ =
state;
118 ESP_LOGCONFIG(TAG,
"PZEM004T:");
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.
void dump_config() override
enum esphome::pzem004t::PZEM004T::PZEM004TReadState DONE
sensor::Sensor * energy_sensor_
sensor::Sensor * current_sensor_
sensor::Sensor * power_sensor_
void write_state_(PZEM004TReadState state)
sensor::Sensor * voltage_sensor_
void publish_state(float state)
Publish a new state to the front-end.
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.
Application App
Global storage of Application pointer - only one Application can exist.