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