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