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

Slowly replacing code

parent 715f636c
......@@ -19,7 +19,8 @@ namespace config {
}
namespace sensor {
SCD30 scd30;
S8_UART *sensor_S8;
S8_sensor sensor;
uint16_t co2 = 0;
float temperature = 0;
float humidity = 0;
......@@ -61,65 +62,45 @@ namespace sensor {
#endif
#if defined(ESP32)
// For ESP32 : RX on GPIO17, TX on GPIO16
# define S8_UART_PORT 2 // Change UART port if it is needed
Wire.begin(21, 22); // ESP32
# define S8_UART_PORT 2
HardwareSerial S8_serial(S8_UART_PORT);
/**
* SCD30 ESP32
* 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)
*/
S8_serial.begin(S8_BAUDRATE);
sensor_S8 = new S8_UART(S8_serial);
#endif
Serial.println();
scd30.enableDebugging(); // Prints firmware version in the console.
if (!scd30.begin(config::auto_calibrate_sensor)) {
Serial.println(F("ERROR - CO2 sensor not detected. Please check wiring!"));
// Check if S8 is available
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);
ESP.restart();
}
// Changes of the SCD30's measurement timestep do not come into effect
// before the next measurement takes place. That means that after a hard reset
// of the ESP the SCD30 sometimes needs a long time until switching back to 2 s
// for acclimatization. Resetting it after startup seems to fix this behaviour.
scd30.reset();
//NOTE: It seems that the sensor needs some time for getting/setting temperature offset.
delay(500);
Serial.print(F("Setting temperature offset to -"));
Serial.print(abs(config::temperature_offset));
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"));
// Show basic S8 sensor info
Serial.println(">>> SenseAir S8 NDIR CO2 sensor <<<");
printf("Firmware version: %s\n", sensor.firm_version);
sensor.sensor_id = sensor_S8->get_sensor_ID();
Serial.print("Sensor ID: 0x");
printIntToHex(sensor.sensor_id, 4);
Serial.println("");
Serial.println("Setup done!");
Serial.flush();
Serial.print(F("Auto-calibration is "));
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("timer", setTimer, F("30 (Sets measurement interval, in s)"));
sensor_console::defineCommand("calibrate", startCalibrationProcess, F("(Starts calibration process)"));
sensor_console::defineIntCommand("calibrate", calibrateSensorToSpecificPPM,
F("600 (Starts calibration process, to given ppm)"));
sensor_console::defineIntCommand("calibrate!", calibrateSensorRightNow,
F("600 (Calibrates right now, to given ppm)"));
sensor_console::defineIntCommand("auto_calibrate", setAutoCalibration, F("0/1 (Disables/enables autocalibration)"));
sensor_console::defineCommand("reset_scd", resetSCD, F("(Resets SCD30)"));
// sensor_console::defineIntCommand("timer", setTimer, F("30 (Sets measurement interval, in s)"));
// sensor_console::defineCommand("calibrate", startCalibrationProcess, F("(Starts calibration process)"));
// sensor_console::defineIntCommand("calibrate", calibrateSensorToSpecificPPM,
// F("600 (Starts calibration process, to given ppm)"));
// sensor_console::defineIntCommand("calibrate!", calibrateSensorRightNow,
// F("600 (Calibrates right now, to given ppm)"));
// sensor_console::defineIntCommand("auto_calibrate", setAutoCalibration, F("0/1 (Disables/enables autocalibration)"));
// sensor_console::defineCommand("reset_scd", resetSCD, F("(Resets SCD30)"));
}
bool hasSensorSettled() {
......@@ -151,27 +132,11 @@ namespace sensor {
}
void startCalibrationProcess() {
/** From the sensor documentation:
* 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);
Serial.println(F("Implement ME!"));
}
void calibrate() {
Serial.print(F("Calibrating SCD30 now..."));
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.
Serial.println(F("Implement ME!"));
}
void logToSerial() {
......@@ -204,13 +169,6 @@ namespace sensor {
}
switchState(READY);
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
......@@ -272,9 +230,9 @@ namespace sensor {
if (freshData) {
ntp::getLocalTime(timestamp);
co2 = scd30.getCO2();
temperature = scd30.getTemperature();
humidity = scd30.getHumidity();
co2 = sensor_S8->get_co2();
temperature = 0.0;
humidity = 0.0;
switchStateForCurrentPPM();
......@@ -290,7 +248,7 @@ namespace sensor {
}
float getTemperatureOffset() {
return -abs(scd30.getTemperatureOffset());
return 0.0;
}
/*****************************************************************
......@@ -304,21 +262,11 @@ namespace sensor {
}
void setAutoCalibration(int32_t autoCalibration) {
config::auto_calibrate_sensor = autoCalibration;
scd30.setAutoSelfCalibration(autoCalibration);
Serial.print(F("Setting auto-calibration to : "));
Serial.println(autoCalibration ? F("On.") : F("Off."));
Serial.println(F("Implement ME!"));
}
void setTimer(int32_t timestep) {
if (timestep >= 2 && timestep <= 1800) {
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);
}
Serial.println(F("Implement ME!"));
}
void calibrateSensorToSpecificPPM(int32_t calibrationLevel) {
......@@ -342,8 +290,6 @@ namespace sensor {
}
void resetSCD() {
Serial.print(F("Resetting SCD30..."));
scd30.reset();
Serial.println(F("done."));
Serial.println(F("Implement ME!"));
}
}
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