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