ESPHome  2023.5.5
pzem004t.cpp
Go to the documentation of this file.
1 #include "pzem004t.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace pzem004t {
6 
7 static const char *const TAG = "pzem004t";
8 
10  // Clear UART buffer
11  while (this->available())
12  this->read();
13  // Set module address
15 }
16 
18  const uint32_t now = millis();
19  if (now - this->last_read_ > 500 && this->available() < 7) {
20  while (this->available())
21  this->read();
22  this->last_read_ = now;
23  }
24 
25  // PZEM004T packet size is 7 byte
26  while (this->available() >= 7) {
27  auto resp = *this->read_array<7>();
28  // packet format:
29  // 0: packet type
30  // 1-5: data
31  // 6: checksum (sum of other bytes)
32  // see https://github.com/olehs/PZEM004T
33  uint8_t sum = 0;
34  for (int i = 0; i < 6; i++)
35  sum += resp[i];
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);
62  this->write_state_(READ_POWER);
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 = (uint32_t(resp[1]) << 16) | (uint32_t(resp[2]) << 8) | (uint32_t(resp[3]));
76  if (this->energy_sensor_ != nullptr)
77  this->energy_sensor_->publish_state(energy);
78  ESP_LOGD(TAG, "Got Energy %u 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 pzem004t
125 } // 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:27
void loop() override
Definition: pzem004t.cpp:17
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:117
void setup() override
Definition: pzem004t.cpp:9
Definition: a4988.cpp:4
void update() override
Definition: pzem004t.cpp:97
void write_state_(PZEM004TReadState state)
Definition: pzem004t.cpp:98
enum esphome::pzem004t::PZEM004T::PZEM004TReadState DONE
bool state
Definition: fan.h:34