Skip to content

Commit

Permalink
Merge pull request #129 from kike-canaries/fix_issue_103
Browse files Browse the repository at this point in the history
added sea level pressure setter and improve documentation
  • Loading branch information
hpsaturn authored Feb 3, 2022
2 parents d8638dd + e923720 commit d8605e0
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 10 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ void setup() {
sensors.setSampleTime(15); // [optional] sensors sample time (default 5s)
sensors.setTempOffset(cfg.toffset); // [optional] temperature compensation
sensors.setCO2AltitudeOffset(cfg.altoffset); // [optional] CO2 altitude compensation
sensors.setSeaLevelPressure(1036.25); // [optional] Set sea level pressure in hpa
sensors.setDebugMode(false); // [optional] debug mode to get detailed msgs
sensors.detectI2COnly(true); // [optional] force to only i2c sensors
sensors.init(); // Auto detection to UART and i2c sensors
Expand Down
33 changes: 27 additions & 6 deletions src/Sensors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,13 @@ void Sensors::setSampleTime(int seconds) {
}
}

/// set CO2 recalibration PPM value (400 to 2000)
/**
* @brief set CO2 recalibration PPM value (400 to 2000)
* @param ppmValue the ppm value to set, normally 400.
*
* This method is used to set the CO2 recalibration value, please use it only on outdoor conditions.
* Please see the documentation of each sensor for more information.
*/
void Sensors::setCO2RecalibrationFactor(int ppmValue) {
if (isSensorRegistered(SENSORS::SSCD30)) {
Serial.println("-->[SLIB] SCD30 calibration to\t: " + String(ppmValue));
Expand Down Expand Up @@ -160,7 +166,12 @@ void Sensors::setCO2RecalibrationFactor(int ppmValue) {
}
}

/// set CO2 Altitude offset, recommended on high altitude
/**
* @brief set CO2 altitude offset (m)
* @param altitude (m)
*
* This method is used to compensate the CO2 value with the altitude. Recommended on high altitude.
*/
void Sensors::setCO2AltitudeOffset(float altitude){
this->altoffset = altitude;
this->hpa = hpaCalculation(altitude); //hPa hectopascal calculation based on altitude
Expand All @@ -177,6 +188,16 @@ void Sensors::setCO2AltitudeOffset(float altitude){
}
}

/**
* @brief set the sea level pressure (hPa)
* @param hpa (hPa)
*
* This method is used to set the sea level pressure for some sensors that need it.
*/
void Sensors::setSeaLevelPressure(float hpa) {
sea_level_pressure = hpa;
}

/// restart and re-init all sensors (not recommended)
void Sensors::restart() {
_serial->flush();
Expand Down Expand Up @@ -759,7 +780,7 @@ bool Sensors::CO2Mhz19Read() {
}

bool Sensors::CO2CM1106Read() {
CO2Val = cm1106->get_co2();;
CO2Val = cm1106->get_co2();
if (CO2Val > 0) {
dataReady = true;
if(altoffset != 0) CO2correctionAlt();
Expand Down Expand Up @@ -849,7 +870,7 @@ void Sensors::bme280Read() {
humi = humi1;
temp = temp1-toffset;
pres = bme280.readPressure();
alt = bme280.readAltitude(SEALEVELPRESSURE_HPA);
alt = bme280.readAltitude(sea_level_pressure);
dataReady = true;
DEBUG("-->[SLIB] BME280 read\t\t: done!");
unitRegister(UNIT::TEMP);
Expand All @@ -859,7 +880,7 @@ void Sensors::bme280Read() {
void Sensors::bmp280Read() {
float temp1 = bmp280.readTemperature();
float press1 = bmp280.readPressure();
float alt1 = bmp280.readAltitude(SEALEVELPRESSURE_HPA);
float alt1 = bmp280.readAltitude(sea_level_pressure);
if (press1 == 0 || isnan(temp1) || isnan(alt1)) return;
temp = temp1-toffset;
pres = press1/100; // convert to hPa
Expand All @@ -880,7 +901,7 @@ void Sensors::bme680Read() {
humi = bme680.humidity;
pres = bme680.pressure / 100.0;
gas = bme680.gas_resistance / 1000.0;
alt = bme680.readAltitude(SEALEVELPRESSURE_HPA);
alt = bme680.readAltitude(sea_level_pressure);
dataReady = true;
DEBUG("-->[SLIB] BME680 read\t\t: done!");
unitRegister(UNIT::TEMP);
Expand Down
9 changes: 5 additions & 4 deletions src/Sensors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,7 @@
#define SENSOR_RETRY 1000 // Max Serial characters

// Sensirion SPS30 sensor
#define SENSOR_COMMS SERIALPORT2 // UART OR I2C

//H&T definitions
#define SEALEVELPRESSURE_HPA 1036.25
#define SENSOR_COMMS SERIALPORT2 // UART OR I2C

#define SENSOR_UNITS \
X(NUNIT, "NUNIT", "NUNIT") \
Expand Down Expand Up @@ -247,6 +244,8 @@ class Sensors {

void setCO2AltitudeOffset(float altitude);

void setSeaLevelPressure(float hpa);

String getFormatTemp();

String getFormatPress();
Expand Down Expand Up @@ -348,6 +347,8 @@ class Sensors {
float CO2humi = 0.0; // humidity of CO2 sensor
float CO2temp = 0.0; // temperature of CO2 sensor

float sea_level_pressure = 1036.25;

void am2320Init();
void am2320Read();

Expand Down

0 comments on commit d8605e0

Please sign in to comment.