diff --git a/co2_sensor.cpp b/co2_sensor.cpp index 96d18efc59aa630a5576f639bf694d234cfdfaff..724588bb26526e622b01867712441ee211e59afa 100644 --- a/co2_sensor.cpp +++ b/co2_sensor.cpp @@ -2,17 +2,17 @@ namespace config { // Values should be defined in config.h - uint16_t measurement_timestep = MEASUREMENT_TIMESTEP; // [s] Value between 2 and 1800 (range for SCD30 sensor) - const uint16_t altitude_above_sea_level = ALTITUDE_ABOVE_SEA_LEVEL; // [m] - uint16_t co2_calibration_level = ATMOSPHERIC_CO2_CONCENTRATION; // [ppm] + uint16_t measurement_timestep = MEASUREMENT_TIMESTEP; // [s] Value between 2 and 1800 (range for SCD30 sensor) + const uint16_t altitude_above_sea_level = ALTITUDE_ABOVE_SEA_LEVEL; // [m] + uint16_t co2_calibration_level = ATMOSPHERIC_CO2_CONCENTRATION; // [ppm] #ifdef TEMPERATURE_OFFSET // Residual heat from CO2 sensor seems to be high enough to change the temperature reading. How much should it be offset? // NOTE: Sign isn't relevant. The returned temperature will always be shifted down. - const float temperature_offset = TEMPERATURE_OFFSET; // [K] + const float temperature_offset = TEMPERATURE_OFFSET; // [K] #else const float temperature_offset = -3.0; // [K] Temperature measured by sensor is usually at least 3K too high. #endif - const bool auto_calibrate_sensor = AUTO_CALIBRATE_SENSOR; // [true / false] + const bool auto_calibrate_sensor = AUTO_CALIBRATE_SENSOR; // [true / false] } namespace sensor { @@ -27,7 +27,7 @@ namespace sensor { Wire.begin(12, 14); // ESP8266 - D6, D5; #endif #if defined(ESP32) - Wire.begin(21, 22); // ESP32 + Wire.begin(21, 22); // ESP32 /** * SCD30 ESP32 * VCC --- 3V3 @@ -46,11 +46,12 @@ namespace sensor { } // SCD30 has its own timer. + //NOTE: The timer seems to be inaccurate, though, possibly depending on voltage. Should it be offset? Serial.println(); Serial.print(F("Setting SCD30 timestep to ")); Serial.print(config::measurement_timestep); Serial.println(" s."); - scd30.setMeasurementInterval(config::measurement_timestep); // [s] + scd30.setMeasurementInterval(config::measurement_timestep); // [s] Serial.print(F("Setting temperature offset to -")); Serial.print(abs(config::temperature_offset)); @@ -66,10 +67,21 @@ namespace sensor { Serial.println(config::auto_calibrate_sensor ? "ON." : "OFF."); } + //NOTE: should time offset be used to reset measurement_timestep? + void checkTimerDeviation() { + static int32_t previous_measurement_at = 0; // Used to monitor sensor timer offset. + int32_t now = millis(); + Serial.print("Measurement time offset : "); + Serial.print(now - previous_measurement_at - config::measurement_timestep * 1000); + Serial.println(" ms."); + previous_measurement_at = now; + } + bool updateDataIfAvailable() { //TODO: Save count of stable measurements - //TODO: Compare time to previous measurements, check that it's not too far away from config::measurement_interval + static int16_t previous_co2 = 0; if (scd30.dataAvailable()) { + // checkTimerDeviation(); co2 = scd30.getCO2(); temperature = scd30.getTemperature(); humidity = scd30.getHumidity();