ESPHome 2026.5.0b1
Loading...
Searching...
No Matches
pzem004t.cpp
Go to the documentation of this file.
1#include "pzem004t.h"
2#include "esphome/core/log.h"
4#include <cinttypes>
5
7
8static const char *const TAG = "pzem004t";
9
11 // Clear UART buffer
12 while (this->available())
13 this->read();
14 // Set module address
16}
17
20 if (now - this->last_read_ > 500 && this->available() < 7) {
21 while (this->available())
22 this->read();
23 this->last_read_ = now;
24 }
25
26 // PZEM004T packet size is 7 byte
27 while (this->available() >= 7) {
28 auto resp_opt = this->read_array<7>();
29 if (!resp_opt.has_value())
30 break;
31 auto resp = *resp_opt;
32 // packet format:
33 // 0: packet type
34 // 1-5: data
35 // 6: checksum (sum of other bytes)
36 // see https://github.com/olehs/PZEM004T
37 uint8_t sum = 0;
38 for (int i = 0; i < 6; i++)
39 sum += resp[i];
40
41 if (sum != resp[6]) {
42 ESP_LOGV(TAG, "PZEM004T invalid checksum! 0x%02X != 0x%02X", sum, resp[6]);
43 continue;
44 }
45
46 switch (resp[0]) {
47 case 0xA4: { // Set Module Address Response
49 break;
50 }
51 case 0xA0: { // Voltage Response
52 uint16_t int_voltage = (uint16_t(resp[1]) << 8) | (uint16_t(resp[2]) << 0);
53 float voltage = int_voltage + (resp[3] / 10.0f);
54 if (this->voltage_sensor_ != nullptr)
55 this->voltage_sensor_->publish_state(voltage);
56 ESP_LOGD(TAG, "Got Voltage %.1f V", voltage);
58 break;
59 }
60 case 0xA1: { // Current Response
61 uint16_t int_current = (uint16_t(resp[1]) << 8) | (uint16_t(resp[2]) << 0);
62 float current = int_current + (resp[3] / 100.0f);
63 if (this->current_sensor_ != nullptr)
64 this->current_sensor_->publish_state(current);
65 ESP_LOGD(TAG, "Got Current %.2f A", current);
67 break;
68 }
69 case 0xA2: { // Active Power Response
70 uint16_t power = (uint16_t(resp[1]) << 8) | (uint16_t(resp[2]) << 0);
71 if (this->power_sensor_ != nullptr)
72 this->power_sensor_->publish_state(power);
73 ESP_LOGD(TAG, "Got Power %u W", power);
75 break;
76 }
77
78 case 0xA3: { // Energy Response
79 uint32_t energy = (uint32_t(resp[1]) << 16) | (uint32_t(resp[2]) << 8) | (uint32_t(resp[3]));
80 if (this->energy_sensor_ != nullptr)
81 this->energy_sensor_->publish_state(energy);
82 ESP_LOGD(TAG, "Got Energy %" PRIu32 " Wh", energy);
83 this->write_state_(DONE);
84 break;
85 }
86
87 case 0xA5: // Set Power Alarm Response
88 case 0xB0: // Voltage Request
89 case 0xB1: // Current Request
90 case 0xB2: // Active Power Response
91 case 0xB3: // Energy Request
92 case 0xB4: // Set Module Address Request
93 case 0xB5: // Set Power Alarm Request
94 default:
95 break;
96 }
97
98 this->last_read_ = now;
99 }
100}
103 if (state == DONE) {
104 this->read_state_ = state;
105 return;
106 }
107 std::array<uint8_t, 7> data{};
108 data[0] = state;
109 data[1] = 192;
110 data[2] = 168;
111 data[3] = 1;
112 data[4] = 1;
113 data[5] = 0;
114 data[6] = 0;
115 for (int i = 0; i < 6; i++)
116 data[6] += data[i];
117
118 this->write_array(data);
119 this->read_state_ = state;
120}
122 ESP_LOGCONFIG(TAG, "PZEM004T:");
123 LOG_SENSOR("", "Voltage", this->voltage_sensor_);
124 LOG_SENSOR("", "Current", this->current_sensor_);
125 LOG_SENSOR("", "Power", this->power_sensor_);
126}
127
128} // 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:121
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:102
sensor::Sensor * voltage_sensor_
Definition pzem004t.h:25
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
optional< std::array< uint8_t, N > > read_array()
Definition uart.h:38
void write_array(const uint8_t *data, size_t len)
Definition uart.h:26
bool state
Definition fan.h:2
Application App
Global storage of Application pointer - only one Application can exist.
static void uint32_t