ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
pzem004t.cpp
Go to the documentation of this file.
1#include "pzem004t.h"
3#include "esphome/core/log.h"
5#include <cinttypes>
6
8
9ESPHOME_LOG_TAG(TAG, "pzem004t");
10
11void PZEM004T::setup() {
12 this->discard_available();
13 // Set module address
15}
16
17void PZEM004T::loop() {
19 if (now - this->last_read_ > 500 && this->available() < 7) {
20 this->discard_available();
21 this->last_read_ = now;
22 }
23
24 // PZEM004T packet size is 7 byte
25 while (this->available() >= 7) {
26 auto resp_opt = this->read_array<7>();
27 if (!resp_opt.has_value())
28 break;
29 auto resp = *resp_opt;
30 // packet format:
31 // 0: packet type
32 // 1-5: data
33 // 6: checksum (sum of other bytes)
34 // see https://github.com/olehs/PZEM004T
35 const uint8_t sum = sum_bytes(resp.data(), 6);
36
37 if (sum != resp[6]) {
38 ESP_LOGV(TAG, "PZEM004T invalid checksum! 0x%02X != 0x%02X", sum, resp[6]);
39 continue;
40 }
41
42 switch (resp[0]) {
43 case 0xA4: { // Set Module Address Response
45 break;
46 }
47 case 0xA0: { // Voltage Response
48 uint16_t int_voltage = encode_uint16(resp[1], resp[2]);
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);
53 break;
54 }
55 case 0xA1: { // Current Response
56 uint16_t int_current = encode_uint16(resp[1], resp[2]);
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);
61 break;
62 }
63 case 0xA2: { // Active Power Response
64 uint16_t power = encode_uint16(resp[1], resp[2]);
65 PUBLISH_STATE(this->power_sensor_, power);
66 ESP_LOGD(TAG, "Got Power %u W", power);
68 break;
69 }
70
71 case 0xA3: { // Energy Response
72 uint32_t energy = encode_uint24(resp[1], resp[2], resp[3]);
73 PUBLISH_STATE(this->energy_sensor_, energy);
74 ESP_LOGD(TAG, "Got Energy %" PRIu32 " Wh", energy);
75 this->write_state_(DONE);
76 break;
77 }
78
79 case 0xA5: // Set Power Alarm Response
80 case 0xB0: // Voltage Request
81 case 0xB1: // Current Request
82 case 0xB2: // Active Power Response
83 case 0xB3: // Energy Request
84 case 0xB4: // Set Module Address Request
85 case 0xB5: // Set Power Alarm Request
86 default:
87 break;
88 }
89
90 this->last_read_ = now;
91 }
92}
93void PZEM004T::update() { this->write_state_(READ_VOLTAGE); }
95 if (state == DONE) {
96 this->read_state_ = state;
97 return;
98 }
99 std::array<uint8_t, 7> data{};
100 data[0] = state;
101 data[1] = 192;
102 data[2] = 168;
103 data[3] = 1;
104 data[4] = 1;
105 data[5] = 0;
106 data[6] = sum_bytes(data.data(), 6);
107
108 this->write_array(data);
109 this->read_state_ = state;
110}
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_);
116}
117
118} // namespace esphome::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.
virtual void setup()
Where the component's initialization should happen.
Definition component.cpp:84
enum esphome::pzem004t::PZEM004T::PZEM004TReadState DONE
void write_state_(PZEM004TReadState state)
Definition pzem004t.cpp:94
optional< std::array< uint8_t, N > > read_array()
Definition uart.h:43
void discard_available()
Read and drop every byte waiting in the receive buffer.
Definition uart.cpp:36
void write_array(const uint8_t *data, size_t len)
Definition uart.h:30
bool state
Definition fan.h:2
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...
Definition helpers.h:939
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.
Definition helpers.h:1047
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
Definition helpers.h:1043
Application App
Global storage of Application pointer - only one Application can exist.
static void uint32_t