Add retry logic for NC read and better logging

- Retry NC read up to 3 times with delays
- Log product name detection
- Check if SEN66 is detected

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-12-01 11:22:29 +07:00
parent 99facda246
commit 7498c34340

View File

@@ -104,7 +104,11 @@ void SEN5XComponent::initialize_sensor_() {
current_int++;
} while (current_char && --max);
ESP_LOGI(TAG, "Product: %s", product_name_.c_str());
ESP_LOGI(TAG, "Product: %s (length=%d)", product_name_.c_str(), product_name_.length());
// Check if this is a SEN66 - NC values only available on SEN66
bool is_sen66 = product_name_.find("SEN66") != std::string::npos;
ESP_LOGI(TAG, "SEN66 detected: %s", is_sen66 ? "yes" : "no");
// Start measurement now
if (!this->write_command(SEN5X_CMD_START_MEASUREMENTS)) {
@@ -268,30 +272,38 @@ bool SEN5XComponent::read_number_concentration(uint16_t *nc05, uint16_t *nc10,
uint16_t *nc25, uint16_t *nc40,
uint16_t *nc100) {
// SEN6x datasheet: command 0x0316, read delay 20ms, returns 5 x uint16
// Try using write_command + delay + read_data pattern like main measurement
if (!this->write_command(SEN6X_CMD_READ_NUMBER_CONCENTRATION)) {
this->status_set_warning();
ESP_LOGW(TAG, "Failed to write NC command (0x0316), err=%d", this->last_error_);
return false;
// Try multiple times with small delays in case sensor is busy
for (int retry = 0; retry < 3; retry++) {
if (retry > 0) {
delay(10); // Small delay between retries
}
if (!this->write_command(SEN6X_CMD_READ_NUMBER_CONCENTRATION)) {
ESP_LOGD(TAG, "NC write attempt %d failed, err=%d", retry + 1, this->last_error_);
continue;
}
delay(20); // Datasheet specifies 20ms
uint16_t raw[5];
if (!this->read_data(raw, 5)) {
ESP_LOGD(TAG, "NC read attempt %d failed, err=%d", retry + 1, this->last_error_);
continue;
}
*nc05 = raw[0];
*nc10 = raw[1];
*nc25 = raw[2];
*nc40 = raw[3];
*nc100 = raw[4];
ESP_LOGD(TAG, "NC values: 0.5=%u, 1.0=%u, 2.5=%u, 4.0=%u, 10=%u", raw[0], raw[1], raw[2], raw[3], raw[4]);
return true;
}
delay(20); // Datasheet specifies 20ms
uint16_t raw[5];
if (!this->read_data(raw, 5)) {
this->status_set_warning();
ESP_LOGW(TAG, "Failed to read NC data (0x0316), err=%d", this->last_error_);
return false;
}
*nc05 = raw[0];
*nc10 = raw[1];
*nc25 = raw[2];
*nc40 = raw[3];
*nc100 = raw[4];
ESP_LOGD(TAG, "NC values: 0.5=%u, 1.0=%u, 2.5=%u, 4.0=%u, 10=%u", raw[0], raw[1], raw[2], raw[3], raw[4]);
return true;
this->status_set_warning();
ESP_LOGW(TAG, "Failed to read NC after 3 attempts");
return false;
}
bool SEN5XComponent::start_measurement() {