Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sensor registration at Sensors::CO2scd30Init() and Sensors::CO2scd4xInit() #204

Merged
merged 9 commits into from
Feb 21, 2024
63 changes: 58 additions & 5 deletions src/Sensors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -366,6 +365,22 @@ void Sensors::setTempOffset(float offset) {
setsen5xTempOffset(toffset);
}

/**
* @brief Get temperature offset for Sensirion sensors (from internal sensor in SCD4x and SCD30)
* @return float with the temperature offset.
* Positive value for offset to be subtracetd to the temperature.
*/
float Sensors::getTempOffset() {
float toffset = 0.0;
if (isSensorRegistered(SENSORS::SSCD30)) {
toffset = getSCD30TempOffset();
}
if (isSensorRegistered(SENSORS::SSCD4X)) {
toffset = getSCD4xTempOffset();
}
return toffset;
}

/// get Gas resistance value of BMP680 sensor
float Sensors::getGas() { return gas; }

Expand Down Expand Up @@ -1586,6 +1601,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());

Expand All @@ -1595,11 +1612,11 @@ void Sensors::CO2scd30Init() {
delay(10);
}

if (uint16_t((scd30.getTemperatureOffset() * 100)) != (uint16_t(toffset * 100))) {
if (uint16_t((scd30.getTemperatureOffset())) != (uint16_t(toffset * 100))) {
DEBUG("-->[SLIB] SCD30 Temp offset to\t:", String(toffset).c_str());
setSCD30TempOffset(toffset);
delay(10);
}
sensorRegister(SENSORS::SSCD30);
}

/// set SCD30 temperature compensation
Expand All @@ -1610,6 +1627,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)) {
Expand All @@ -1627,6 +1654,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());
Expand All @@ -1637,12 +1665,11 @@ 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();
if (error) DEBUG("[W][SLIB] SCD4x periodic measure\t: starting error:", String(error).c_str());
sensorRegister(SENSORS::SSCD4X);
}

/// set SCD4x temperature compensation
Expand All @@ -1656,6 +1683,32 @@ 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();
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
void Sensors::setSCD4xAltitudeOffset(float offset) {
if (isSensorRegistered(SENSORS::SSCD4X)) {
Expand Down
4 changes: 4 additions & 0 deletions src/Sensors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,8 @@ class Sensors {

void setTempOffset(float offset);

float getTempOffset();

void setCO2AltitudeOffset(float altitude);

void setSeaLevelPressure(float hpa);
Expand Down Expand Up @@ -419,13 +421,15 @@ class Sensors {
void CO2scd30Init();
void CO2scd30Read();
void setSCD30TempOffset(float offset);
float getSCD30TempOffset();
void setSCD30AltitudeOffset(float offset);
void CO2correctionAlt();
float hpaCalculation(float altitude);

void CO2scd4xInit();
void CO2scd4xRead();
void setSCD4xTempOffset(float offset);
float getSCD4xTempOffset();
void setSCD4xAltitudeOffset(float offset);

void sen5xInit();
Expand Down
Loading