From 6da72010a3cd7a37673c4d7907ed23f48213b38c Mon Sep 17 00:00:00 2001 From: Mario Mariete <11509521+melkati@users.noreply.github.com> Date: Fri, 16 Feb 2024 12:00:17 +0100 Subject: [PATCH 1/7] =?UTF-8?q?Fix=20CO2scd30Init()=20function.=20Do=20not?= =?UTF-8?q?=20multiply=20scd30.getTemperatureOffset()=20by=20100=20(it?= =?UTF-8?q?=C2=B4s=20already=20in=20hundreths=20of=20a=20degree=20C)=20to?= =?UTF-8?q?=20avoid=20setting=20temperature=20offset=20on=20each=20sensor?= =?UTF-8?q?=20init.=20Do=20sensorRegister(SENSORS::SSCD30)=20prior=20to=20?= =?UTF-8?q?seeting=20offsets.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Sensors.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Sensors.cpp b/src/Sensors.cpp index d76287bb..b3c7d932 100644 --- a/src/Sensors.cpp +++ b/src/Sensors.cpp @@ -1586,6 +1586,8 @@ void Sensors::CO2scd30Init() { #endif delay(10); + sensorRegister(SENSORS::SSCD30); + DEBUG("-->[SLIB] SCD30 Temp offset\t:", String(scd30.getTemperatureOffset()).c_str()); DEBUG("-->[SLIB] SCD30 Altitude offset\t:", String(scd30.getAltitudeOffset()).c_str()); @@ -1595,11 +1597,10 @@ void Sensors::CO2scd30Init() { delay(10); } - if (uint16_t((scd30.getTemperatureOffset() * 100)) != (uint16_t(toffset * 100))) { + if (uint16_t((scd30.getTemperatureOffset())) != (uint16_t(toffset * 100))) { setSCD30TempOffset(toffset); delay(10); } - sensorRegister(SENSORS::SSCD30); } /// set SCD30 temperature compensation From 8511eac0b6e2fdbac28f3ebd84d4918c26dc6238 Mon Sep 17 00:00:00 2001 From: Mario Mariete <11509521+melkati@users.noreply.github.com> Date: Fri, 16 Feb 2024 12:19:38 +0100 Subject: [PATCH 2/7] Add debug statement for SCD30 temperature offset in CO2scd30Init() --- src/Sensors.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Sensors.cpp b/src/Sensors.cpp index b3c7d932..5d932cf9 100644 --- a/src/Sensors.cpp +++ b/src/Sensors.cpp @@ -1598,6 +1598,7 @@ void Sensors::CO2scd30Init() { } if (uint16_t((scd30.getTemperatureOffset())) != (uint16_t(toffset * 100))) { + DEBUG("-->[SLIB] SCD30 Temp offset to\t:", String(toffset).c_str()); setSCD30TempOffset(toffset); delay(10); } From 42329340867bc088a6653f97509f18c757cef30d Mon Sep 17 00:00:00 2001 From: Mario Mariete <11509521+melkati@users.noreply.github.com> Date: Mon, 19 Feb 2024 14:45:47 +0100 Subject: [PATCH 3/7] Do sensorRegister(SENSORS::SSCD4x) prior to seeting offsets. --- src/Sensors.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Sensors.cpp b/src/Sensors.cpp index 5d932cf9..8984061b 100644 --- a/src/Sensors.cpp +++ b/src/Sensors.cpp @@ -1629,6 +1629,7 @@ void Sensors::CO2scd4xInit() { scd4x.begin(Wire); error = scd4x.stopPeriodicMeasurement(); if (error) return; + sensorRegister(SENSORS::SSCD4X); scd4x.getTemperatureOffset(tTemperatureOffset); scd4x.getSensorAltitude(tSensorAltitude); DEBUG("-->[SLIB] SCD4x Temp offset\t:", String(tTemperatureOffset).c_str()); @@ -1644,7 +1645,6 @@ void Sensors::CO2scd4xInit() { } error = scd4x.startPeriodicMeasurement(); if (error) DEBUG("[W][SLIB] SCD4x periodic measure\t: starting error:", String(error).c_str()); - sensorRegister(SENSORS::SSCD4X); } /// set SCD4x temperature compensation From 048230530a2057ed968c1d8d5afcadaebb3a4f5e Mon Sep 17 00:00:00 2001 From: Mario Mariete <11509521+melkati@users.noreply.github.com> Date: Tue, 20 Feb 2024 11:05:12 +0100 Subject: [PATCH 4/7] Add temperature offset getter for Sensirion sensors --- src/Sensors.cpp | 37 +++++++++++++++++++++++++++++++++++-- src/Sensors.hpp | 2 ++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/Sensors.cpp b/src/Sensors.cpp index 8984061b..4a5bae36 100644 --- a/src/Sensors.cpp +++ b/src/Sensors.cpp @@ -355,7 +355,6 @@ void Sensors::tempRegister(bool isCO2temp) { /** * @brief Set temperature offset for all temperature sensors - * @param offset temperature offset in °C (default 0). * * Positive value for offset to be subtracetd to the temperature. */ @@ -366,6 +365,18 @@ void Sensors::setTempOffset(float offset) { setsen5xTempOffset(toffset); } +/** + * @brief Get temperature offset for Sensirion sensors (from internal sensor in SCD4x and SCD30) + * @param offset temperature offset in °C (default 0). + * @return float with the temperature offset. + * Positive value for offset to be subtracetd to the temperature. + */ +float getTempOffset() { + float toffset; + toffset = getSCD30TempOffset(toffset * 100); + toffset = getSCD4xTempOffset(toffset); +} + /// get Gas resistance value of BMP680 sensor float Sensors::getGas() { return gas; } @@ -1612,6 +1623,16 @@ void Sensors::setSCD30TempOffset(float offset) { } } +/// get SCD30 temperature compensation +float Sensors::getSCD30TempOffset() { + float offset = 0.0; + if (isSensorRegistered(SENSORS::SSCD30)) { + offset = scd30.getTemperatureOffset() / 100.0; + Serial.println("-->[SLIB] SCD30 get temp offset\t: " + String(offset)); + } + return offset; +} + /// set SCD30 altitude compensation void Sensors::setSCD30AltitudeOffset(float offset) { if (isSensorRegistered(SENSORS::SSCD30)) { @@ -1640,7 +1661,7 @@ void Sensors::CO2scd4xInit() { offsetDifference = abs((toffset * 100) - (tTemperatureOffset * 100)); if (offsetDifference > 0.5) { // Accounts for SCD4x conversion rounding errors in temperature offset - Serial.println("-->[SLIB] SCD4x new offset\t: Temp offset to" + String(toffset)); + Serial.println("-->[SLIB] SCD4x new offset\t: Temp offset to " + String(toffset)); setSCD4xTempOffset(toffset); } error = scd4x.startPeriodicMeasurement(); @@ -1658,6 +1679,18 @@ void Sensors::setSCD4xTempOffset(float offset) { } } +/// get SCD4x temperature compensation +float Sensors::getSCD4xTempOffset() { + float offset = 0.0; + if (isSensorRegistered(SENSORS::SSCD4X)) { + scd4x.stopPeriodicMeasurement(); + delay(510); + scd4x.getTemperatureOffset(offset); + scd4x.startPeriodicMeasurement(); + Serial.println("-->[SLIB] SCD4x get temperature offset\t: " + String(offset)); + } +} + /// set SCD4x altitude compensation void Sensors::setSCD4xAltitudeOffset(float offset) { if (isSensorRegistered(SENSORS::SSCD4X)) { diff --git a/src/Sensors.hpp b/src/Sensors.hpp index cb048029..c044febf 100644 --- a/src/Sensors.hpp +++ b/src/Sensors.hpp @@ -306,6 +306,8 @@ class Sensors { void setTempOffset(float offset); + float getTempOffset(); + void setCO2AltitudeOffset(float altitude); void setSeaLevelPressure(float hpa); From c18bd12003ec12dee9aa34227f1f9ca1c7d90b66 Mon Sep 17 00:00:00 2001 From: Mario Mariete <11509521+melkati@users.noreply.github.com> Date: Tue, 20 Feb 2024 14:32:04 +0100 Subject: [PATCH 5/7] Fix temperature offset getters for SCD4x and SCD30 sensors --- src/Sensors.cpp | 27 +++++++++++++++++++++------ src/Sensors.hpp | 2 ++ 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/src/Sensors.cpp b/src/Sensors.cpp index 4a5bae36..3992ac4a 100644 --- a/src/Sensors.cpp +++ b/src/Sensors.cpp @@ -373,8 +373,9 @@ void Sensors::setTempOffset(float offset) { */ float getTempOffset() { float toffset; - toffset = getSCD30TempOffset(toffset * 100); - toffset = getSCD4xTempOffset(toffset); + toffset = getSCD30TempOffset(); + toffset = getSCD4xTempOffset(); + return toffset; } /// get Gas resistance value of BMP680 sensor @@ -1682,13 +1683,27 @@ void Sensors::setSCD4xTempOffset(float offset) { /// get SCD4x temperature compensation float Sensors::getSCD4xTempOffset() { float offset = 0.0; + uint16_t error; if (isSensorRegistered(SENSORS::SSCD4X)) { scd4x.stopPeriodicMeasurement(); - delay(510); - scd4x.getTemperatureOffset(offset); - scd4x.startPeriodicMeasurement(); - Serial.println("-->[SLIB] SCD4x get temperature offset\t: " + String(offset)); + if (error) { + DEBUG("[SLIB] SCD4x stopPeriodicMeasurement()\t: error:", String(error).c_str()); + return 0.0; + } else { + DEBUG("[SLIB] SCD4x stopPeriodicMeasurement()\t: done!"); + } + error = scd4x.getTemperatureOffset(offset); + if (error) { + DEBUG("[SLIB] SCD4x get temp offset\t: error:", String(error).c_str()); + return 0.0; + } + error = scd4x.startPeriodicMeasurement(); + if (error) { + DEBUG("[SLIB] SCD4x startPeriodicMeasurement()\t: error:", String(error).c_str()); + return 0.0; + } } + return offset; } /// set SCD4x altitude compensation diff --git a/src/Sensors.hpp b/src/Sensors.hpp index c044febf..8b78ed58 100644 --- a/src/Sensors.hpp +++ b/src/Sensors.hpp @@ -421,6 +421,7 @@ class Sensors { void CO2scd30Init(); void CO2scd30Read(); void setSCD30TempOffset(float offset); + float getSCD30TempOffset(); void setSCD30AltitudeOffset(float offset); void CO2correctionAlt(); float hpaCalculation(float altitude); @@ -428,6 +429,7 @@ class Sensors { void CO2scd4xInit(); void CO2scd4xRead(); void setSCD4xTempOffset(float offset); + float getSCD4xTempOffset(); void setSCD4xAltitudeOffset(float offset); void sen5xInit(); From 93180ee6148d59443ef6e1ac68b605e98df27b13 Mon Sep 17 00:00:00 2001 From: Mario Mariete <11509521+melkati@users.noreply.github.com> Date: Tue, 20 Feb 2024 15:04:19 +0100 Subject: [PATCH 6/7] Fix getTempOffset function declaration in Sensors class --- src/Sensors.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Sensors.cpp b/src/Sensors.cpp index 3992ac4a..33099845 100644 --- a/src/Sensors.cpp +++ b/src/Sensors.cpp @@ -367,11 +367,10 @@ void Sensors::setTempOffset(float offset) { /** * @brief Get temperature offset for Sensirion sensors (from internal sensor in SCD4x and SCD30) - * @param offset temperature offset in °C (default 0). * @return float with the temperature offset. * Positive value for offset to be subtracetd to the temperature. */ -float getTempOffset() { +float Sensors::getTempOffset() { float toffset; toffset = getSCD30TempOffset(); toffset = getSCD4xTempOffset(); From 6d188f921afe3e8fad68051f1029759675eafa99 Mon Sep 17 00:00:00 2001 From: Mario Mariete <11509521+melkati@users.noreply.github.com> Date: Wed, 21 Feb 2024 06:45:17 +0100 Subject: [PATCH 7/7] Refactor getTempOffset function to check if sensor is registered --- src/Sensors.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/Sensors.cpp b/src/Sensors.cpp index 33099845..65f91946 100644 --- a/src/Sensors.cpp +++ b/src/Sensors.cpp @@ -371,9 +371,13 @@ void Sensors::setTempOffset(float offset) { * Positive value for offset to be subtracetd to the temperature. */ float Sensors::getTempOffset() { - float toffset; - toffset = getSCD30TempOffset(); - toffset = getSCD4xTempOffset(); + float toffset = 0.0; + if (isSensorRegistered(SENSORS::SSCD30)) { + toffset = getSCD30TempOffset(); + } + if (isSensorRegistered(SENSORS::SSCD4X)) { + toffset = getSCD4xTempOffset(); + } return toffset; }