ESPHome 2025.5.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
20 const uint32_t now = App.get_loop_component_start_time();
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 = *this->read_array<7>();
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 uint8_t sum = 0;
36 for (int i = 0; i < 6; i++)
37 sum += resp[i];
38
39 if (sum != resp[6]) {
40 ESP_LOGV(TAG, "PZEM004T invalid checksum! 0x%02X != 0x%02X", sum, resp[6]);
41 continue;
42 }
43
44 switch (resp[0]) {
45 case 0xA4: { // Set Module Address Response
47 break;
48 }
49 case 0xA0: { // Voltage Response
50 uint16_t int_voltage = (uint16_t(resp[1]) << 8) | (uint16_t(resp[2]) << 0);
51 float voltage = int_voltage + (resp[3] / 10.0f);
52 if (this->voltage_sensor_ != nullptr)
53 this->voltage_sensor_->publish_state(voltage);
54 ESP_LOGD(TAG, "Got Voltage %.1f V", voltage);
56 break;
57 }
58 case 0xA1: { // Current Response
59 uint16_t int_current = (uint16_t(resp[1]) << 8) | (uint16_t(resp[2]) << 0);
60 float current = int_current + (resp[3] / 100.0f);
61 if (this->current_sensor_ != nullptr)
62 this->current_sensor_->publish_state(current);
63 ESP_LOGD(TAG, "Got Current %.2f A", current);
65 break;
66 }
67 case 0xA2: { // Active Power Response
68 uint16_t power = (uint16_t(resp[1]) << 8) | (uint16_t(resp[2]) << 0);
69 if (this->power_sensor_ != nullptr)
70 this->power_sensor_->publish_state(power);
71 ESP_LOGD(TAG, "Got Power %u W", power);
73 break;
74 }
75
76 case 0xA3: { // Energy Response
77 uint32_t energy = (uint32_t(resp[1]) << 16) | (uint32_t(resp[2]) << 8) | (uint32_t(resp[3]));
78 if (this->energy_sensor_ != nullptr)
79 this->energy_sensor_->publish_state(energy);
80 ESP_LOGD(TAG, "Got Energy %" PRIu32 " Wh", energy);
81 this->write_state_(DONE);
82 break;
83 }
84
85 case 0xA5: // Set Power Alarm Response
86 case 0xB0: // Voltage Request
87 case 0xB1: // Current Request
88 case 0xB2: // Active Power Response
89 case 0xB3: // Energy Request
90 case 0xB4: // Set Module Address Request
91 case 0xB5: // Set Power Alarm Request
92 default:
93 break;
94 }
95
96 this->last_read_ = now;
97 }
98}
101 if (state == DONE) {
102 this->read_state_ = state;
103 return;
104 }
105 std::array<uint8_t, 7> data{};
106 data[0] = state;
107 data[1] = 192;
108 data[2] = 168;
109 data[3] = 1;
110 data[4] = 1;
111 data[5] = 0;
112 data[6] = 0;
113 for (int i = 0; i < 6; i++)
114 data[6] += data[i];
115
116 this->write_array(data);
117 this->read_state_ = state;
118}
120 ESP_LOGCONFIG(TAG, "PZEM004T:");
121 LOG_SENSOR("", "Voltage", this->voltage_sensor_);
122 LOG_SENSOR("", "Current", this->current_sensor_);
123 LOG_SENSOR("", "Power", this->power_sensor_);
124}
125
126} // namespace pzem004t
127} // 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:119
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:100
sensor::Sensor * voltage_sensor_
Definition pzem004t.h:26
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:39
optional< std::array< uint8_t, N > > read_array()
Definition uart.h:33
void write_array(const uint8_t *data, size_t len)
Definition uart.h:21
bool state
Definition fan.h:0
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.