This commit is contained in:
schiermi 2024-05-12 11:50:55 +10:00 committed by GitHub
commit 8b0c5f2b9d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 26 additions and 6 deletions

View File

@ -102,15 +102,17 @@ void SCD30Component::setup() {
#endif
/// Sensor initialization
if (!this->write_command(SCD30_CMD_START_CONTINUOUS_MEASUREMENTS, this->ambient_pressure_compensation_)) {
ESP_LOGE(TAG, "Sensor SCD30 error starting continuous measurements.");
this->error_code_ = MEASUREMENT_INIT_FAILED;
this->mark_failed();
return;
}
restart_continuous_measurements_();
// check each 500ms if data is ready, and read it in that case
this->set_interval("status-check", 500, [this]() {
// remove millibar from comparison to avoid frequent updates +/- 10 millibar doesn't matter
if (this->current_ambient_pressure_compensation_ / 10 != this->ambient_pressure_compensation_ / 10) {
ESP_LOGD(TAG, "ambient pressure compensation triggered; restarting measurements");
this->restart_continuous_measurements_();
return;
}
if (this->is_data_ready_())
this->update();
});
@ -229,5 +231,21 @@ uint16_t SCD30Component::get_forced_calibration_reference() {
return forced_calibration_reference;
}
void SCD30Component::restart_continuous_measurements_() {
if (!this->write_command(SCD30_CMD_STOP_MEASUREMENTS, this->ambient_pressure_compensation_)) {
ESP_LOGE(TAG, "Sensor SCD30 error stopping continuous measurements.");
this->error_code_ = MEASUREMENT_INIT_FAILED;
this->mark_failed();
return;
}
if (!this->write_command(SCD30_CMD_START_CONTINUOUS_MEASUREMENTS, this->ambient_pressure_compensation_)) {
ESP_LOGE(TAG, "Sensor SCD30 error starting continuous measurements.");
this->error_code_ = MEASUREMENT_INIT_FAILED;
this->mark_failed();
return;
}
this->current_ambient_pressure_compensation_ = this->ambient_pressure_compensation_;
}
} // namespace scd30
} // namespace esphome

View File

@ -30,6 +30,7 @@ class SCD30Component : public Component, public sensirion_common::SensirionI2CDe
protected:
bool is_data_ready_();
void restart_continuous_measurements_();
enum ErrorCode {
COMMUNICATION_FAILED,
@ -41,6 +42,7 @@ class SCD30Component : public Component, public sensirion_common::SensirionI2CDe
bool enable_asc_{true};
uint16_t altitude_compensation_{0xFFFF};
uint16_t ambient_pressure_compensation_{0x0000};
uint16_t current_ambient_pressure_compensation_{0x0000};
float temperature_offset_{0.0};
uint16_t update_interval_{0xFFFF};