ESPHome  2024.3.1
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  if (this->power_sensor_ != nullptr) {
151  this->power_sensor_->publish_state(0.0f);
152  }
153  } else if (have_power) {
154  power = power_coeff / float(power_cycle);
155  if (this->power_sensor_ != nullptr) {
156  this->power_sensor_->publish_state(power);
157  }
158  }
159 
160  float current = 0.0f;
161  float calculated_current = 0.0f;
162  if (have_current) {
163  // Assumption: if we don't have power measurement, then current is likely below 50mA
164  if (have_power && voltage > 1.0f) {
165  calculated_current = power / voltage;
166  }
167  // Datasheet: minimum measured current is 50mA
168  if (calculated_current > 0.05f) {
169  current = current_coeff / float(current_cycle);
170  }
171  if (this->current_sensor_ != nullptr) {
172  this->current_sensor_->publish_state(current);
173  }
174  }
175 
176  if (have_voltage && have_current) {
177  const float apparent_power = voltage * current;
178  if (this->apparent_power_sensor_ != nullptr) {
179  this->apparent_power_sensor_->publish_state(apparent_power);
180  }
181  if (this->power_factor_sensor_ != nullptr && (have_power || power_cycle_exceeds_range)) {
182  float pf = NAN;
183  if (apparent_power > 0) {
184  pf = power / apparent_power;
185  if (pf < 0 || pf > 1) {
186  ESP_LOGD(TAG, "Impossible power factor: %.4f not in interval [0, 1]", pf);
187  pf = NAN;
188  }
189  } else if (apparent_power == 0 && power == 0) {
190  // No load, report ideal power factor
191  pf = 1.0f;
192  } else if (current == 0 && calculated_current <= 0.05f) {
193  // Datasheet: minimum measured current is 50mA
194  ESP_LOGV(TAG, "Can't calculate power factor (current below minimum for CSE7766)");
195  } else {
196  ESP_LOGW(TAG, "Can't calculate power factor from P = %.4f W, S = %.4f VA", power, apparent_power);
197  }
199  }
200  }
201 
202 #if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERY_VERBOSE
203  {
204  std::stringstream ss;
205  ss << "Parsed:";
206  if (have_voltage) {
207  ss << " V=" << voltage << "V";
208  }
209  if (have_current) {
210  ss << " I=" << current * 1000.0f << "mA (~" << calculated_current * 1000.0f << "mA)";
211  }
212  if (have_power) {
213  ss << " P=" << power << "W";
214  }
215  if (energy != 0.0f) {
216  ss << " E=" << energy << "kWh (" << cf_pulses << ")";
217  }
218  ESP_LOGVV(TAG, "%s", ss.str().c_str());
219  }
220 #endif
221 }
222 
223 uint32_t CSE7766Component::get_24_bit_uint_(uint8_t start_index) {
224  return (uint32_t(this->raw_data_[start_index]) << 16) | (uint32_t(this->raw_data_[start_index + 1]) << 8) |
225  uint32_t(this->raw_data_[start_index + 2]);
226 }
227 
229  ESP_LOGCONFIG(TAG, "CSE7766:");
230  LOG_SENSOR(" ", "Voltage", this->voltage_sensor_);
231  LOG_SENSOR(" ", "Current", this->current_sensor_);
232  LOG_SENSOR(" ", "Power", this->power_sensor_);
233  LOG_SENSOR(" ", "Energy", this->energy_sensor_);
234  LOG_SENSOR(" ", "Apparent Power", this->apparent_power_sensor_);
235  LOG_SENSOR(" ", "Power Factor", this->power_factor_sensor_);
236  this->check_uart_settings(4800);
237 }
238 
239 } // namespace cse7766
240 } // namespace esphome
sensor::Sensor * power_sensor_
Definition: cse7766.h:35
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:146
sensor::Sensor * power_factor_sensor_
Definition: cse7766.h:38
uint32_t get_24_bit_uint_(uint8_t start_index)
Definition: cse7766.cpp:223
sensor::Sensor * energy_sensor_
Definition: cse7766.h:36
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:161
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:39
uint8_t checksum
Definition: bl0939.h:35
float get_setup_priority() const override
Definition: cse7766.cpp:40
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
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:34
sensor::Sensor * apparent_power_sensor_
Definition: cse7766.h:37
sensor::Sensor * voltage_sensor_
Definition: cse7766.h:33