ESPHome  2024.11.0
cse7766.cpp
Go to the documentation of this file.
1 #include "cse7766.h"
2 #include "esphome/core/log.h"
3 #include <cinttypes>
4 #include <iomanip>
5 #include <sstream>
6 
7 namespace esphome {
8 namespace cse7766 {
9 
10 static const char *const TAG = "cse7766";
11 
13  const uint32_t now = millis();
14  if (now - this->last_transmission_ >= 500) {
15  // last transmission too long ago. Reset RX index.
16  this->raw_data_index_ = 0;
17  }
18 
19  if (this->available() == 0) {
20  return;
21  }
22 
23  this->last_transmission_ = now;
24  while (this->available() != 0) {
25  this->read_byte(&this->raw_data_[this->raw_data_index_]);
26  if (!this->check_byte_()) {
27  this->raw_data_index_ = 0;
28  this->status_set_warning();
29  continue;
30  }
31 
32  if (this->raw_data_index_ == 23) {
33  this->parse_data_();
34  this->status_clear_warning();
35  }
36 
37  this->raw_data_index_ = (this->raw_data_index_ + 1) % 24;
38  }
39 }
41 
43  uint8_t index = this->raw_data_index_;
44  uint8_t byte = this->raw_data_[index];
45  if (index == 0) {
46  return !((byte != 0x55) && ((byte & 0xF0) != 0xF0) && (byte != 0xAA));
47  }
48 
49  if (index == 1) {
50  if (byte != 0x5A) {
51  ESP_LOGV(TAG, "Invalid Header 2 Start: 0x%02X!", byte);
52  return false;
53  }
54  return true;
55  }
56 
57  if (index == 23) {
58  uint8_t checksum = 0;
59  for (uint8_t i = 2; i < 23; i++) {
60  checksum += this->raw_data_[i];
61  }
62 
63  if (checksum != this->raw_data_[23]) {
64  ESP_LOGW(TAG, "Invalid checksum from CSE7766: 0x%02X != 0x%02X", checksum, this->raw_data_[23]);
65  return false;
66  }
67  return true;
68  }
69 
70  return true;
71 }
73 #if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERY_VERBOSE
74  {
75  std::stringstream ss;
76  ss << "Raw data:" << std::hex << std::uppercase << std::setfill('0');
77  for (uint8_t i = 0; i < 23; i++) {
78  ss << ' ' << std::setw(2) << static_cast<unsigned>(this->raw_data_[i]);
79  }
80  ESP_LOGVV(TAG, "%s", ss.str().c_str());
81  }
82 #endif
83 
84  // Parse header
85  uint8_t header1 = this->raw_data_[0];
86 
87  if (header1 == 0xAA) {
88  ESP_LOGE(TAG, "CSE7766 not calibrated!");
89  return;
90  }
91 
92  bool power_cycle_exceeds_range = false;
93  if ((header1 & 0xF0) == 0xF0) {
94  if (header1 & 0xD) {
95  ESP_LOGE(TAG, "CSE7766 reports abnormal external circuit or chip damage: (0x%02X)", header1);
96  if (header1 & (1 << 3)) {
97  ESP_LOGE(TAG, " Voltage cycle exceeds range.");
98  }
99  if (header1 & (1 << 2)) {
100  ESP_LOGE(TAG, " Current cycle exceeds range.");
101  }
102  if (header1 & (1 << 0)) {
103  ESP_LOGE(TAG, " Coefficient storage area is abnormal.");
104  }
105 
106  // Datasheet: voltage or current cycle exceeding range means invalid values
107  return;
108  }
109 
110  power_cycle_exceeds_range = header1 & (1 << 1);
111  }
112 
113  // Parse data frame
114  uint32_t voltage_coeff = this->get_24_bit_uint_(2);
115  uint32_t voltage_cycle = this->get_24_bit_uint_(5);
116  uint32_t current_coeff = this->get_24_bit_uint_(8);
117  uint32_t current_cycle = this->get_24_bit_uint_(11);
118  uint32_t power_coeff = this->get_24_bit_uint_(14);
119  uint32_t power_cycle = this->get_24_bit_uint_(17);
120  uint8_t adj = this->raw_data_[20];
121  uint16_t cf_pulses = (this->raw_data_[21] << 8) + this->raw_data_[22];
122 
123  bool have_power = adj & 0x10;
124  bool have_current = adj & 0x20;
125  bool have_voltage = adj & 0x40;
126 
127  float voltage = 0.0f;
128  if (have_voltage) {
129  voltage = voltage_coeff / float(voltage_cycle);
130  if (this->voltage_sensor_ != nullptr) {
131  this->voltage_sensor_->publish_state(voltage);
132  }
133  }
134 
135  float energy = 0.0;
136  if (this->energy_sensor_ != nullptr) {
137  if (this->cf_pulses_last_ == 0 && !this->energy_sensor_->has_state()) {
138  this->cf_pulses_last_ = cf_pulses;
139  }
140  uint16_t cf_diff = cf_pulses - this->cf_pulses_last_;
141  this->cf_pulses_total_ += cf_diff;
142  this->cf_pulses_last_ = cf_pulses;
143  energy = this->cf_pulses_total_ * float(power_coeff) / 1000000.0f / 3600.0f;
144  this->energy_sensor_->publish_state(energy);
145  }
146 
147  float power = 0.0f;
148  if (power_cycle_exceeds_range) {
149  // Datasheet: power cycle exceeding range means active power is 0
150  have_power = true;
151  if (this->power_sensor_ != nullptr) {
152  this->power_sensor_->publish_state(0.0f);
153  }
154  } else if (have_power) {
155  power = power_coeff / float(power_cycle);
156  if (this->power_sensor_ != nullptr) {
157  this->power_sensor_->publish_state(power);
158  }
159  }
160 
161  float current = 0.0f;
162  float calculated_current = 0.0f;
163  if (have_current) {
164  // Assumption: if we don't have power measurement, then current is likely below 50mA
165  if (have_power && voltage > 1.0f) {
166  calculated_current = power / voltage;
167  }
168  // Datasheet: minimum measured current is 50mA
169  if (calculated_current > 0.05f) {
170  current = current_coeff / float(current_cycle);
171  }
172  if (this->current_sensor_ != nullptr) {
173  this->current_sensor_->publish_state(current);
174  }
175  }
176 
177  if (have_voltage && have_current) {
178  const float apparent_power = voltage * current;
179  if (this->apparent_power_sensor_ != nullptr) {
180  this->apparent_power_sensor_->publish_state(apparent_power);
181  }
182  if (have_power && this->reactive_power_sensor_ != nullptr) {
183  const float reactive_power = apparent_power - power;
184  if (reactive_power < 0.0f) {
185  ESP_LOGD(TAG, "Impossible reactive power: %.4f is negative", reactive_power);
187  } else {
188  this->reactive_power_sensor_->publish_state(reactive_power);
189  }
190  }
191  if (this->power_factor_sensor_ != nullptr && (have_power || power_cycle_exceeds_range)) {
192  float pf = NAN;
193  if (apparent_power > 0) {
194  pf = power / apparent_power;
195  if (pf < 0 || pf > 1) {
196  ESP_LOGD(TAG, "Impossible power factor: %.4f not in interval [0, 1]", pf);
197  pf = NAN;
198  }
199  } else if (apparent_power == 0 && power == 0) {
200  // No load, report ideal power factor
201  pf = 1.0f;
202  } else if (current == 0 && calculated_current <= 0.05f) {
203  // Datasheet: minimum measured current is 50mA
204  ESP_LOGV(TAG, "Can't calculate power factor (current below minimum for CSE7766)");
205  } else {
206  ESP_LOGW(TAG, "Can't calculate power factor from P = %.4f W, S = %.4f VA", power, apparent_power);
207  }
209  }
210  }
211 
212 #if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERY_VERBOSE
213  {
214  std::stringstream ss;
215  ss << "Parsed:";
216  if (have_voltage) {
217  ss << " V=" << voltage << "V";
218  }
219  if (have_current) {
220  ss << " I=" << current * 1000.0f << "mA (~" << calculated_current * 1000.0f << "mA)";
221  }
222  if (have_power) {
223  ss << " P=" << power << "W";
224  }
225  if (energy != 0.0f) {
226  ss << " E=" << energy << "kWh (" << cf_pulses << ")";
227  }
228  ESP_LOGVV(TAG, "%s", ss.str().c_str());
229  }
230 #endif
231 }
232 
233 uint32_t CSE7766Component::get_24_bit_uint_(uint8_t start_index) {
234  return (uint32_t(this->raw_data_[start_index]) << 16) | (uint32_t(this->raw_data_[start_index + 1]) << 8) |
235  uint32_t(this->raw_data_[start_index + 2]);
236 }
237 
239  ESP_LOGCONFIG(TAG, "CSE7766:");
240  LOG_SENSOR(" ", "Voltage", this->voltage_sensor_);
241  LOG_SENSOR(" ", "Current", this->current_sensor_);
242  LOG_SENSOR(" ", "Power", this->power_sensor_);
243  LOG_SENSOR(" ", "Energy", this->energy_sensor_);
244  LOG_SENSOR(" ", "Apparent Power", this->apparent_power_sensor_);
245  LOG_SENSOR(" ", "Reactive Power", this->reactive_power_sensor_);
246  LOG_SENSOR(" ", "Power Factor", this->power_factor_sensor_);
248 }
249 
250 } // namespace cse7766
251 } // namespace esphome
sensor::Sensor * power_sensor_
Definition: cse7766.h:38
const float DATA
For components that import data from directly connected sensors like DHT.
Definition: component.cpp:19
void status_set_warning(const char *message="unspecified")
Definition: component.cpp:151
sensor::Sensor * power_factor_sensor_
Definition: cse7766.h:42
uint8_t checksum
Definition: bl0906.h:210
uint32_t get_24_bit_uint_(uint8_t start_index)
Definition: cse7766.cpp:233
sensor::Sensor * energy_sensor_
Definition: cse7766.h:39
uint32_t IRAM_ATTR HOT millis()
Definition: core.cpp:25
void check_uart_settings(uint32_t baud_rate, uint8_t stop_bits=1, UARTParityOptions parity=UART_CONFIG_PARITY_NONE, uint8_t data_bits=8)
Check that the configuration of the UART bus matches the provided values and otherwise print a warnin...
Definition: uart.cpp:13
bool read_byte(uint8_t *data)
Definition: uart.h:29
void status_clear_warning()
Definition: component.cpp:166
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:39
float get_setup_priority() const override
Definition: cse7766.cpp:40
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
bool has_state() const
Return whether this sensor has gotten a full state (that passed through all filters) yet...
Definition: sensor.cpp:97
sensor::Sensor * current_sensor_
Definition: cse7766.h:37
sensor::Sensor * apparent_power_sensor_
Definition: cse7766.h:40
sensor::Sensor * voltage_sensor_
Definition: cse7766.h:36
sensor::Sensor * reactive_power_sensor_
Definition: cse7766.h:41