ESPHome 2026.11.0-dev
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
12 this->discard_available();
13 // Set module address
15}
16
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 = (uint16_t(resp[1]) << 8) | (uint16_t(resp[2]) << 0);
49 float voltage = int_voltage + (resp[3] / 10.0f);
50 if (this->voltage_sensor_ != nullptr)
51 this->voltage_sensor_->publish_state(voltage);
52 ESP_LOGD(TAG, "Got Voltage %.1f V", voltage);
54 break;
55 }
56 case 0xA1: { // Current Response
57 uint16_t int_current = (uint16_t(resp[1]) << 8) | (uint16_t(resp[2]) << 0);
58 float current = int_current + (resp[3] / 100.0f);
59 if (this->current_sensor_ != nullptr)
60 this->current_sensor_->publish_state(current);
61 ESP_LOGD(TAG, "Got Current %.2f A", current);
63 break;
64 }
65 case 0xA2: { // Active Power Response
66 uint16_t power = (uint16_t(resp[1]) << 8) | (uint16_t(resp[2]) << 0);
67 if (this->power_sensor_ != nullptr)
68 this->power_sensor_->publish_state(power);
69 ESP_LOGD(TAG, "Got Power %u W", power);
71 break;
72 }
73
74 case 0xA3: { // Energy Response
75 uint32_t energy = encode_uint24(resp[1], resp[2], resp[3]);
76 if (this->energy_sensor_ != nullptr)
77 this->energy_sensor_->publish_state(energy);
78 ESP_LOGD(TAG, "Got Energy %" PRIu32 " Wh", energy);
79 this->write_state_(DONE);
80 break;
81 }
82
83 case 0xA5: // Set Power Alarm Response
84 case 0xB0: // Voltage Request
85 case 0xB1: // Current Request
86 case 0xB2: // Active Power Response
87 case 0xB3: // Energy Request
88 case 0xB4: // Set Module Address Request
89 case 0xB5: // Set Power Alarm Request
90 default:
91 break;
92 }
93
94 this->last_read_ = now;
95 }
96}
99 if (state == DONE) {
100 this->read_state_ = state;
101 return;
102 }
103 std::array<uint8_t, 7> data{};
104 data[0] = state;
105 data[1] = 192;
106 data[2] = 168;
107 data[3] = 1;
108 data[4] = 1;
109 data[5] = 0;
110 data[6] = 0;
111 for (int i = 0; i < 6; i++)
112 data[6] += data[i];
113
114 this->write_array(data);
115 this->read_state_ = state;
116}
118 ESP_LOGCONFIG(TAG, "PZEM004T:");
119 LOG_SENSOR("", "Voltage", this->voltage_sensor_);
120 LOG_SENSOR("", "Current", this->current_sensor_);
121 LOG_SENSOR("", "Power", this->power_sensor_);
122}
123
124} // 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.
void dump_config() override
Definition pzem004t.cpp:117
enum esphome::pzem004t::PZEM004T::PZEM004TReadState DONE
sensor::Sensor * energy_sensor_
Definition pzem004t.h:28
sensor::Sensor * current_sensor_
Definition pzem004t.h:26
sensor::Sensor * power_sensor_
Definition pzem004t.h:27
void write_state_(PZEM004TReadState state)
Definition pzem004t.cpp:98
sensor::Sensor * voltage_sensor_
Definition pzem004t.h:25
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:65
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:894
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:1002
Application App
Global storage of Application pointer - only one Application can exist.
static void uint32_t