Commit a4e0fed5 authored by Eric Duminil's avatar Eric Duminil
Browse files

Slowly replacing code

parent 715f636c
...@@ -19,7 +19,8 @@ namespace config { ...@@ -19,7 +19,8 @@ namespace config {
} }
namespace sensor { namespace sensor {
SCD30 scd30; S8_UART *sensor_S8;
S8_sensor sensor;
uint16_t co2 = 0; uint16_t co2 = 0;
float temperature = 0; float temperature = 0;
float humidity = 0; float humidity = 0;
...@@ -61,65 +62,45 @@ namespace sensor { ...@@ -61,65 +62,45 @@ namespace sensor {
#endif #endif
#if defined(ESP32) #if defined(ESP32)
// For ESP32 : RX on GPIO17, TX on GPIO16 // For ESP32 : RX on GPIO17, TX on GPIO16
# define S8_UART_PORT 2 // Change UART port if it is needed # define S8_UART_PORT 2
Wire.begin(21, 22); // ESP32
HardwareSerial S8_serial(S8_UART_PORT); HardwareSerial S8_serial(S8_UART_PORT);
/** S8_serial.begin(S8_BAUDRATE);
* SCD30 ESP32 sensor_S8 = new S8_UART(S8_serial);
* VCC --- 3V3
* GND --- GND
* SCL --- SCL (GPIO22) //NOTE: GPIO3 Would be more convenient (right next to GND)
* SDA --- SDA (GPIO21) //NOTE: GPIO1 would be more convenient (right next to GPO3)
*/
#endif #endif
Serial.println(); Serial.println();
scd30.enableDebugging(); // Prints firmware version in the console.
if (!scd30.begin(config::auto_calibrate_sensor)) { // Check if S8 is available
Serial.println(F("ERROR - CO2 sensor not detected. Please check wiring!")); sensor_S8->get_firmware_version(sensor.firm_version);
int len = strlen(sensor.firm_version);
if (len == 0) {
Serial.println(F("ERROR - Senseair S8 CO2 sensor not detected. Please check wiring!"));
led_effects::showKITTWheel(color::red, 30); led_effects::showKITTWheel(color::red, 30);
ESP.restart(); ESP.restart();
} }
// Changes of the SCD30's measurement timestep do not come into effect // Show basic S8 sensor info
// before the next measurement takes place. That means that after a hard reset Serial.println(">>> SenseAir S8 NDIR CO2 sensor <<<");
// of the ESP the SCD30 sometimes needs a long time until switching back to 2 s printf("Firmware version: %s\n", sensor.firm_version);
// for acclimatization. Resetting it after startup seems to fix this behaviour. sensor.sensor_id = sensor_S8->get_sensor_ID();
scd30.reset(); Serial.print("Sensor ID: 0x");
printIntToHex(sensor.sensor_id, 4);
//NOTE: It seems that the sensor needs some time for getting/setting temperature offset. Serial.println("");
delay(500);
Serial.print(F("Setting temperature offset to -")); Serial.println("Setup done!");
Serial.print(abs(config::temperature_offset)); Serial.flush();
Serial.println(F(" K."));
scd30.setTemperatureOffset(abs(config::temperature_offset)); // setTemperatureOffset only accepts positive numbers, but shifts the temperature down.
delay(500);
//NOTE: Even once the temperature offset is saved, the sensor still needs some time (~10 minutes?) to apply it.
Serial.print(F("Temperature offset is : "));
Serial.print(getTemperatureOffset());
Serial.println(F(" K"));
Serial.print(F("Auto-calibration is ")); Serial.print(F("Auto-calibration is "));
Serial.println(config::auto_calibrate_sensor ? "ON." : "OFF."); Serial.println(config::auto_calibrate_sensor ? "ON." : "OFF.");
// 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_bootup);
Serial.println(F(" s during acclimatization."));
scd30.setMeasurementInterval(config::measurement_timestep_bootup); // [s]
sensor_console::defineIntCommand("co2", setCO2forDebugging, F("1500 (Sets co2 level, for debugging)")); sensor_console::defineIntCommand("co2", setCO2forDebugging, F("1500 (Sets co2 level, for debugging)"));
sensor_console::defineIntCommand("timer", setTimer, F("30 (Sets measurement interval, in s)")); // sensor_console::defineIntCommand("timer", setTimer, F("30 (Sets measurement interval, in s)"));
sensor_console::defineCommand("calibrate", startCalibrationProcess, F("(Starts calibration process)")); // sensor_console::defineCommand("calibrate", startCalibrationProcess, F("(Starts calibration process)"));
sensor_console::defineIntCommand("calibrate", calibrateSensorToSpecificPPM, // sensor_console::defineIntCommand("calibrate", calibrateSensorToSpecificPPM,
F("600 (Starts calibration process, to given ppm)")); // F("600 (Starts calibration process, to given ppm)"));
sensor_console::defineIntCommand("calibrate!", calibrateSensorRightNow, // sensor_console::defineIntCommand("calibrate!", calibrateSensorRightNow,
F("600 (Calibrates right now, to given ppm)")); // F("600 (Calibrates right now, to given ppm)"));
sensor_console::defineIntCommand("auto_calibrate", setAutoCalibration, F("0/1 (Disables/enables autocalibration)")); // sensor_console::defineIntCommand("auto_calibrate", setAutoCalibration, F("0/1 (Disables/enables autocalibration)"));
sensor_console::defineCommand("reset_scd", resetSCD, F("(Resets SCD30)")); // sensor_console::defineCommand("reset_scd", resetSCD, F("(Resets SCD30)"));
} }
bool hasSensorSettled() { bool hasSensorSettled() {
...@@ -151,27 +132,11 @@ namespace sensor { ...@@ -151,27 +132,11 @@ namespace sensor {
} }
void startCalibrationProcess() { void startCalibrationProcess() {
/** From the sensor documentation: Serial.println(F("Implement ME!"));
* Before applying FRC, SCD30 needs to be operated for 2 minutes with the desired measurement period in continuous mode.
*/
Serial.print(F("Setting SCD30 timestep to "));
Serial.print(config::timestep_during_calibration);
Serial.println(F("s, prior to calibration."));
scd30.setMeasurementInterval(config::timestep_during_calibration); // [s] The change will only take effect after next measurement.
Serial.println(F("Waiting until the measurements are stable for at least 2 minutes."));
Serial.println(F("It could take a very long time."));
switchState(PREPARE_CALIBRATION_UNSTABLE);
} }
void calibrate() { void calibrate() {
Serial.print(F("Calibrating SCD30 now...")); Serial.println(F("Implement ME!"));
scd30.setAltitudeCompensation(config::altitude_above_sea_level);
scd30.setForcedRecalibrationFactor(config::co2_calibration_level);
Serial.println(F(" Done!"));
Serial.println(F("Sensor calibrated."));
switchState(BOOTUP); // In order to stop the calibration and select the desired timestep.
//WARNING: Do not reset the ampel or the SCD30!
//At least one measurement needs to happen in order for the calibration to be correctly applied.
} }
void logToSerial() { void logToSerial() {
...@@ -204,13 +169,6 @@ namespace sensor { ...@@ -204,13 +169,6 @@ namespace sensor {
} }
switchState(READY); switchState(READY);
Serial.println(F("Sensor acclimatization finished.")); Serial.println(F("Sensor acclimatization finished."));
Serial.print(F("Setting SCD30 timestep to "));
Serial.print(config::measurement_timestep);
Serial.println(F(" s."));
if (config::measurement_timestep < 10) {
Serial.println(F("WARNING: Timesteps shorter than 10s can lead to unreliable measurements!"));
}
scd30.setMeasurementInterval(config::measurement_timestep); // [s]
} }
// Check for pre-calibration states first, because we do not want to // Check for pre-calibration states first, because we do not want to
...@@ -272,9 +230,9 @@ namespace sensor { ...@@ -272,9 +230,9 @@ namespace sensor {
if (freshData) { if (freshData) {
ntp::getLocalTime(timestamp); ntp::getLocalTime(timestamp);
co2 = scd30.getCO2(); co2 = sensor_S8->get_co2();
temperature = scd30.getTemperature(); temperature = 0.0;
humidity = scd30.getHumidity(); humidity = 0.0;
switchStateForCurrentPPM(); switchStateForCurrentPPM();
...@@ -290,7 +248,7 @@ namespace sensor { ...@@ -290,7 +248,7 @@ namespace sensor {
} }
float getTemperatureOffset() { float getTemperatureOffset() {
return -abs(scd30.getTemperatureOffset()); return 0.0;
} }
/***************************************************************** /*****************************************************************
...@@ -304,21 +262,11 @@ namespace sensor { ...@@ -304,21 +262,11 @@ namespace sensor {
} }
void setAutoCalibration(int32_t autoCalibration) { void setAutoCalibration(int32_t autoCalibration) {
config::auto_calibrate_sensor = autoCalibration; Serial.println(F("Implement ME!"));
scd30.setAutoSelfCalibration(autoCalibration);
Serial.print(F("Setting auto-calibration to : "));
Serial.println(autoCalibration ? F("On.") : F("Off."));
} }
void setTimer(int32_t timestep) { void setTimer(int32_t timestep) {
if (timestep >= 2 && timestep <= 1800) { Serial.println(F("Implement ME!"));
Serial.print(F("Setting Measurement Interval to : "));
Serial.print(timestep);
Serial.println(F("s (change will only be applied after next measurement)."));
scd30.setMeasurementInterval(timestep);
config::measurement_timestep = timestep;
led_effects::showKITTWheel(color::green, 1);
}
} }
void calibrateSensorToSpecificPPM(int32_t calibrationLevel) { void calibrateSensorToSpecificPPM(int32_t calibrationLevel) {
...@@ -342,8 +290,6 @@ namespace sensor { ...@@ -342,8 +290,6 @@ namespace sensor {
} }
void resetSCD() { void resetSCD() {
Serial.print(F("Resetting SCD30...")); Serial.println(F("Implement ME!"));
scd30.reset();
Serial.println(F("done."));
} }
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment