From 541d8ce451826aefd21c1f0fd23b31ea3784658a Mon Sep 17 00:00:00 2001 From: Andreas Schiermeier Date: Tue, 1 Nov 2022 23:30:46 +0100 Subject: [PATCH] feat: SCD30 support pressure compensation updates --- esphome/components/scd30/scd30.cpp | 30 ++++++++++++++++++++++++------ esphome/components/scd30/scd30.h | 2 ++ 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/esphome/components/scd30/scd30.cpp b/esphome/components/scd30/scd30.cpp index 3eeca23800..b7d7b4ae89 100644 --- a/esphome/components/scd30/scd30.cpp +++ b/esphome/components/scd30/scd30.cpp @@ -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 diff --git a/esphome/components/scd30/scd30.h b/esphome/components/scd30/scd30.h index 40f075e673..8373fd56df 100644 --- a/esphome/components/scd30/scd30.h +++ b/esphome/components/scd30/scd30.h @@ -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};