ESPHome 2026.2.4
Loading...
Searching...
No Matches
cse7766.cpp
Go to the documentation of this file.
1#include "cse7766.h"
4#include "esphome/core/log.h"
5
6namespace esphome {
7namespace cse7766 {
8
9static const char *const TAG = "cse7766";
10
12 const uint32_t now = App.get_loop_component_start_time();
13 if (now - this->last_transmission_ >= 500) {
14 // last transmission too long ago. Reset RX index.
15 this->raw_data_index_ = 0;
16 }
17
18 // Early return prevents updating last_transmission_ when no data is available.
19 size_t avail = this->available();
20 if (avail == 0) {
21 return;
22 }
23
24 this->last_transmission_ = now;
25
26 // Read all available bytes in batches to reduce UART call overhead.
27 // At 4800 baud (~480 bytes/sec) with ~122 Hz loop rate, typically ~4 bytes per call.
28 uint8_t buf[CSE7766_RAW_DATA_SIZE];
29 while (avail > 0) {
30 size_t to_read = std::min(avail, sizeof(buf));
31 if (!this->read_array(buf, to_read)) {
32 break;
33 }
34 avail -= to_read;
35
36 for (size_t i = 0; i < to_read; i++) {
37 this->raw_data_[this->raw_data_index_] = buf[i];
38 if (!this->check_byte_()) {
39 this->raw_data_index_ = 0;
40 this->status_set_warning();
41 continue;
42 }
43
44 if (this->raw_data_index_ == CSE7766_RAW_DATA_SIZE - 1) {
45 this->parse_data_();
47 }
48
49 this->raw_data_index_ = (this->raw_data_index_ + 1) % CSE7766_RAW_DATA_SIZE;
50 }
51 }
52}
53
55 uint8_t index = this->raw_data_index_;
56 uint8_t byte = this->raw_data_[index];
57 if (index == 0) {
58 return (byte == 0x55) || ((byte & 0xF0) == 0xF0) || (byte == 0xAA);
59 }
60
61 if (index == 1) {
62 if (byte != 0x5A) {
63 ESP_LOGV(TAG, "Invalid Header 2 Start: 0x%02X!", byte);
64 return false;
65 }
66 return true;
67 }
68
69 if (index == CSE7766_RAW_DATA_SIZE - 1) {
70 uint8_t checksum = 0;
71 for (uint8_t i = 2; i < CSE7766_RAW_DATA_SIZE - 1; i++) {
72 checksum += this->raw_data_[i];
73 }
74
75 if (checksum != this->raw_data_[CSE7766_RAW_DATA_SIZE - 1]) {
76 ESP_LOGW(TAG, "Invalid checksum from CSE7766: 0x%02X != 0x%02X", checksum,
77 this->raw_data_[CSE7766_RAW_DATA_SIZE - 1]);
78 return false;
79 }
80 return true;
81 }
82
83 return true;
84}
86#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERY_VERBOSE
87 {
88 char hex_buf[format_hex_pretty_size(CSE7766_RAW_DATA_SIZE)];
89 ESP_LOGVV(TAG, "Raw data: %s", format_hex_pretty_to(hex_buf, this->raw_data_, sizeof(this->raw_data_)));
90 }
91#endif
92
93 // Parse header
94 uint8_t header1 = this->raw_data_[0];
95
96 if (header1 == 0xAA) {
97 ESP_LOGE(TAG, "CSE7766 not calibrated!");
98 return;
99 }
100
101 bool power_cycle_exceeds_range = false;
102 if ((header1 & 0xF0) == 0xF0) {
103 if (header1 & 0xD) {
104 ESP_LOGE(TAG, "CSE7766 reports abnormal external circuit or chip damage: (0x%02X)", header1);
105 if (header1 & (1 << 3)) {
106 ESP_LOGE(TAG, " Voltage cycle exceeds range.");
107 }
108 if (header1 & (1 << 2)) {
109 ESP_LOGE(TAG, " Current cycle exceeds range.");
110 }
111 if (header1 & (1 << 0)) {
112 ESP_LOGE(TAG, " Coefficient storage area is abnormal.");
113 }
114
115 // Datasheet: voltage or current cycle exceeding range means invalid values
116 return;
117 }
118
119 power_cycle_exceeds_range = header1 & (1 << 1);
120 }
121
122 // Parse data frame
123 uint32_t voltage_coeff = this->get_24_bit_uint_(2);
124 uint32_t voltage_cycle = this->get_24_bit_uint_(5);
125 uint32_t current_coeff = this->get_24_bit_uint_(8);
126 uint32_t current_cycle = this->get_24_bit_uint_(11);
127 uint32_t power_coeff = this->get_24_bit_uint_(14);
128 uint32_t power_cycle = this->get_24_bit_uint_(17);
129 uint8_t adj = this->raw_data_[20];
130 uint16_t cf_pulses = (this->raw_data_[21] << 8) + this->raw_data_[22];
131
132 bool have_power = adj & 0x10;
133 bool have_current = adj & 0x20;
134 bool have_voltage = adj & 0x40;
135
136 float voltage = 0.0f;
137 if (have_voltage) {
138 voltage = voltage_coeff / float(voltage_cycle);
139 if (this->voltage_sensor_ != nullptr) {
140 this->voltage_sensor_->publish_state(voltage);
141 }
142 }
143
144 float energy = 0.0;
145 if (this->energy_sensor_ != nullptr) {
146 if (this->cf_pulses_last_ == 0 && !this->energy_sensor_->has_state()) {
147 this->cf_pulses_last_ = cf_pulses;
148 }
149 uint16_t cf_diff = cf_pulses - this->cf_pulses_last_;
150 this->cf_pulses_total_ += cf_diff;
151 this->cf_pulses_last_ = cf_pulses;
152 energy = this->cf_pulses_total_ * float(power_coeff) / 1000000.0f / 3600.0f;
153 this->energy_sensor_->publish_state(energy);
154 }
155
156 float power = 0.0f;
157 if (power_cycle_exceeds_range) {
158 // Datasheet: power cycle exceeding range means active power is 0
159 have_power = true;
160 if (this->power_sensor_ != nullptr) {
161 this->power_sensor_->publish_state(0.0f);
162 }
163 } else if (have_power) {
164 power = power_coeff / float(power_cycle);
165 if (this->power_sensor_ != nullptr) {
166 this->power_sensor_->publish_state(power);
167 }
168 } else if (this->power_sensor_ != nullptr) {
169 // No valid power measurement from chip - publish 0W to avoid stale readings
170 // This typically happens when current is below the measurable threshold (~50mA)
171 this->power_sensor_->publish_state(0.0f);
172 }
173
174 float current = 0.0f;
175 float calculated_current = 0.0f;
176 if (have_current) {
177 // Assumption: if we don't have power measurement, then current is likely below 50mA
178 if (have_power && voltage > 1.0f) {
179 calculated_current = power / voltage;
180 }
181 // Datasheet: minimum measured current is 50mA
182 if (calculated_current > 0.05f) {
183 current = current_coeff / float(current_cycle);
184 }
185 if (this->current_sensor_ != nullptr) {
186 this->current_sensor_->publish_state(current);
187 }
188 }
189
190 if (have_voltage && have_current) {
191 const float apparent_power = voltage * current;
192 if (this->apparent_power_sensor_ != nullptr) {
193 this->apparent_power_sensor_->publish_state(apparent_power);
194 }
195 if (have_power && this->reactive_power_sensor_ != nullptr) {
196 const float reactive_power = apparent_power - power;
197 if (reactive_power < 0.0f) {
198 ESP_LOGD(TAG, "Impossible reactive power: %.4f is negative", reactive_power);
200 } else {
201 this->reactive_power_sensor_->publish_state(reactive_power);
202 }
203 }
204 if (this->power_factor_sensor_ != nullptr && (have_power || power_cycle_exceeds_range)) {
205 float pf = NAN;
206 if (apparent_power > 0) {
207 pf = power / apparent_power;
208 if (pf < 0 || pf > 1) {
209 ESP_LOGD(TAG, "Impossible power factor: %.4f not in interval [0, 1]", pf);
210 pf = NAN;
211 }
212 } else if (apparent_power == 0 && power == 0) {
213 // No load, report ideal power factor
214 pf = 1.0f;
215 } else if (current == 0 && calculated_current <= 0.05f) {
216 // Datasheet: minimum measured current is 50mA
217 ESP_LOGV(TAG, "Can't calculate power factor (current below minimum for CSE7766)");
218 } else {
219 ESP_LOGW(TAG, "Can't calculate power factor from P = %.4f W, S = %.4f VA", power, apparent_power);
220 }
222 }
223 }
224
225#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERY_VERBOSE
226 {
227 // Buffer: 7 + 15 + 33 + 15 + 25 = 95 chars max + null, rounded to 128 for safety margin.
228 // Float sizes with %.4f can be up to 11 chars for large values (e.g., 999999.9999).
229 char buf[128];
230 size_t pos = buf_append_printf(buf, sizeof(buf), 0, "Parsed:");
231 if (have_voltage) {
232 pos = buf_append_printf(buf, sizeof(buf), pos, " V=%.4fV", voltage);
233 }
234 if (have_current) {
235 pos = buf_append_printf(buf, sizeof(buf), pos, " I=%.4fmA (~%.4fmA)", current * 1000.0f,
236 calculated_current * 1000.0f);
237 }
238 if (have_power) {
239 pos = buf_append_printf(buf, sizeof(buf), pos, " P=%.4fW", power);
240 }
241 if (energy != 0.0f) {
242 buf_append_printf(buf, sizeof(buf), pos, " E=%.4fkWh (%u)", energy, cf_pulses);
243 }
244 ESP_LOGVV(TAG, "%s", buf);
245 }
246#endif
247}
248
250 ESP_LOGCONFIG(TAG, "CSE7766:");
251 LOG_SENSOR(" ", "Voltage", this->voltage_sensor_);
252 LOG_SENSOR(" ", "Current", this->current_sensor_);
253 LOG_SENSOR(" ", "Power", this->power_sensor_);
254 LOG_SENSOR(" ", "Energy", this->energy_sensor_);
255 LOG_SENSOR(" ", "Apparent Power", this->apparent_power_sensor_);
256 LOG_SENSOR(" ", "Reactive Power", this->reactive_power_sensor_);
257 LOG_SENSOR(" ", "Power Factor", this->power_factor_sensor_);
259}
260
261} // namespace cse7766
262} // namespace esphome
uint8_t checksum
Definition bl0906.h:3
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 status_set_warning(const char *message=nullptr)
void status_clear_warning()
bool has_state() const
sensor::Sensor * current_sensor_
Definition cse7766.h:42
sensor::Sensor * voltage_sensor_
Definition cse7766.h:41
sensor::Sensor * reactive_power_sensor_
Definition cse7766.h:46
uint32_t get_24_bit_uint_(uint8_t start_index) const
Definition cse7766.h:33
sensor::Sensor * apparent_power_sensor_
Definition cse7766.h:45
sensor::Sensor * power_sensor_
Definition cse7766.h:43
uint8_t raw_data_[CSE7766_RAW_DATA_SIZE]
Definition cse7766.h:38
sensor::Sensor * power_factor_sensor_
Definition cse7766.h:47
sensor::Sensor * energy_sensor_
Definition cse7766.h:44
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:65
optional< std::array< uint8_t, N > > read_array()
Definition uart.h:38
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:16
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
char * format_hex_pretty_to(char *buffer, size_t buffer_size, const uint8_t *data, size_t length, char separator)
Format byte array as uppercase hex to buffer (base implementation).
Definition helpers.cpp:353
size_t size_t pos
Definition helpers.h:729
constexpr size_t format_hex_pretty_size(size_t byte_count)
Calculate buffer size needed for format_hex_pretty_to with separator: "XX:XX:...:XX\0".
Definition helpers.h:978
Application App
Global storage of Application pointer - only one Application can exist.