7 static const char *
const TAG =
"cse7766";
10 const uint32_t now =
millis();
24 this->raw_data_index_ = 0;
29 if (this->raw_data_index_ == 23) {
34 this->raw_data_index_ = (this->raw_data_index_ + 1) % 24;
43 return !((byte != 0x55) && ((byte & 0xF0) != 0xF0) && (byte != 0xAA));
48 ESP_LOGV(TAG,
"Invalid Header 2 Start: 0x%02X!", byte);
56 for (uint8_t i = 2; i < 23; i++) {
61 ESP_LOGW(TAG,
"Invalid checksum from CSE7766: 0x%02X != 0x%02X", checksum, this->
raw_data_[23]);
70 ESP_LOGVV(TAG,
"CSE7766 Data: ");
71 for (uint8_t i = 0; i < 23; i++) {
72 ESP_LOGVV(TAG,
" %u: 0b" BYTE_TO_BINARY_PATTERN
" (0x%02X)", i + 1, BYTE_TO_BINARY(this->
raw_data_[i]),
77 if (header1 == 0xAA) {
78 ESP_LOGE(TAG,
"CSE7766 not calibrated!");
82 bool power_cycle_exceeds_range =
false;
84 if ((header1 & 0xF0) == 0xF0) {
86 ESP_LOGE(TAG,
"CSE7766 reports abnormal external circuit or chip damage: (0x%02X)", header1);
87 if (header1 & (1 << 3)) {
88 ESP_LOGE(TAG,
" Voltage cycle exceeds range.");
90 if (header1 & (1 << 2)) {
91 ESP_LOGE(TAG,
" Current cycle exceeds range.");
93 if (header1 & (1 << 0)) {
94 ESP_LOGE(TAG,
" Coefficient storage area is abnormal.");
99 power_cycle_exceeds_range = header1 & (1 << 1);
112 bool have_voltage = adj & 0x40;
115 this->
voltage_acc_ += voltage_calib / float(voltage_cycle);
119 bool have_power = adj & 0x10;
125 if (!power_cycle_exceeds_range) {
126 power = power_calib / float(power_cycle);
141 this->cf_pulses_last_ = cf_pulses;
142 this->
energy_total_ += difference * float(power_calib) / 1000000.0f / 3600.0f;
148 float current = 0.0f;
149 if (have_voltage && !have_power) {
153 }
else if (power != 0.0f) {
154 current = current_calib / float(current_cycle);
161 const auto publish_state = [](
const char *
name,
sensor::Sensor *sensor,
float &acc, uint32_t &counts) {
163 const auto avg = acc / counts;
165 ESP_LOGV(TAG,
"Got %s_acc=%.2f %s_counts=%d %s=%.1f", name, acc, name, counts, name, avg);
167 if (sensor !=
nullptr) {
168 sensor->publish_state(avg);
191 return (uint32_t(this->
raw_data_[start_index]) << 16) | (uint32_t(this->
raw_data_[start_index + 1]) << 8) |
192 uint32_t(this->
raw_data_[start_index + 2]);
196 ESP_LOGCONFIG(TAG,
"CSE7766:");
197 LOG_UPDATE_INTERVAL(
this);
sensor::Sensor * power_sensor_
uint32_t energy_total_counts_
const float DATA
For components that import data from directly connected sensors like DHT.
uint32_t get_24_bit_uint_(uint8_t start_index)
sensor::Sensor * energy_sensor_
uint32_t IRAM_ATTR HOT millis()
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...
bool read_byte(uint8_t *data)
void status_clear_warning()
void publish_state(float state)
Publish a new state to the front-end.
void dump_config() override
void status_set_warning()
float get_setup_priority() const override
sensor::Sensor * current_sensor_
uint32_t last_transmission_
Base-class for all sensors.
sensor::Sensor * voltage_sensor_