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

Calibration

parent eac9178c
......@@ -5,6 +5,8 @@ namespace config {
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]
int8_t max_deviation_during_calibration = 30; // [ppm]
int8_t enough_stable_measurements = 60;
#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.
......@@ -42,6 +44,21 @@ namespace sensor {
}
}
void calibrateSensorToSpecificPPM(int32_t calibrationLevel) {
if (calibrationLevel >= 400 && calibrationLevel <= 2000) {
Serial.print(F("Force calibration, at "));
config::co2_calibration_level = calibrationLevel;
Serial.print(config::co2_calibration_level);
Serial.println(" ppm.");
sensor::startCalibrationProcess();
}
}
void calibrateSensorRightNow(int32_t calibrationLevel) {
stable_measurements = config::enough_stable_measurements;
calibrateSensorToSpecificPPM(calibrationLevel);
}
void initialize() {
#if defined(ESP8266)
Wire.begin(12, 14); // ESP8266 - D6, D5;
......@@ -88,6 +105,8 @@ namespace sensor {
sensor_commands::defineCallback("co2", setCO2forDebugging);
sensor_commands::defineCallback("timer", setTimer);
sensor_commands::defineCallback("calibrate", calibrateSensorToSpecificPPM);
sensor_commands::defineCallback("calibrate!", calibrateSensorRightNow);
}
//NOTE: should timer deviation be used to adjust measurement_timestep?
......@@ -102,7 +121,8 @@ namespace sensor {
void countStableMeasurements() {
static int16_t previous_co2 = 0;
if (co2 > (previous_co2 - 30) && co2 < (previous_co2 + 30)) {
if (co2 > (previous_co2 - config::max_deviation_during_calibration)
&& co2 < (previous_co2 + config::max_deviation_during_calibration)) {
stable_measurements++;
Serial.print(F("Number of stable measurements : "));
Serial.println(stable_measurements);
......@@ -197,7 +217,7 @@ namespace sensor {
}
if (should_calibrate) {
if (stable_measurements == 60) {
if (stable_measurements == config::enough_stable_measurements) {
calibrateAndRestart();
}
led_effects::showWaitingLED(waiting_color);
......
......@@ -126,6 +126,7 @@ namespace csv_writer {
Serial.println();
sensor_commands::defineCallback("csv", setCSVinterval);
// sensor_commands::defineCallback("format_filesystem", FS_LIB.format);
}
File openOrCreate() {
......
......@@ -27,6 +27,14 @@ namespace mqtt {
const char *json_sensor_format;
String last_successful_publish = "";
void setMQTTinterval(int32_t sending_interval) {
config::sending_interval = sending_interval;
Serial.print(F("Setting Sending Interval to : "));
Serial.print(config::sending_interval);
Serial.println("s.");
led_effects::showKITTWheel(color::green, 1);
}
void initialize(String &topic) {
json_sensor_format = PSTR("{\"time\":\"%s\", \"co2\":%d, \"temp\":%.1f, \"rh\":%.1f}");
publish_topic = topic;
......@@ -35,6 +43,8 @@ namespace mqtt {
#endif
// mqttClient.setSocketTimeout(config::mqtt_timeout); //NOTE: somehow doesn't seem to have any effect on connect()
mqttClient.setServer(config::mqtt_server, config::mqtt_port);
sensor_commands::defineCallback("mqtt", setMQTTinterval);
}
void publish(const String &timestamp, int16_t co2, float temperature, float humidity) {
......@@ -55,27 +65,6 @@ namespace mqtt {
}
}
void setMQTTinterval(String messageString) {
messageString.replace("mqtt ", "");
config::sending_interval = messageString.toInt();
Serial.print(F("Setting Sending Interval to : "));
Serial.print(config::sending_interval);
Serial.println("s.");
led_effects::showKITTWheel(color::green, 1);
}
void calibrateSensorToSpecificPPM(String messageString) {
messageString.replace("calibrate ", "");
long int calibrationLevel = messageString.toInt();
if (calibrationLevel >= 400 && calibrationLevel <= 2000) {
Serial.print(F("Force calibration, at "));
config::co2_calibration_level = messageString.toInt();
Serial.print(config::co2_calibration_level);
Serial.println(" ppm.");
sensor::startCalibrationProcess();
}
}
void sendInfoAboutLocalNetwork() {
char info_topic[60]; // Should be enough for "CO2sensors/ESPd03cc5/info"
snprintf(info_topic, sizeof(info_topic), "%s/info", publish_topic.c_str());
......@@ -110,17 +99,13 @@ namespace mqtt {
}
Serial.println("'.");
sensor_commands::run(messageString.c_str());
delay(50);
led_effects::onBoardLEDOff();
return;
//TODO: Move this logic to a separate class, which could be used by Serial/MQTT/WebServer
if (messageString == "calibrate") {
sensor::startCalibrationProcess();
} else if (messageString.startsWith("calibrate ")) {
calibrateSensorToSpecificPPM(messageString);
} else if (messageString.startsWith("mqtt ")) {
setMQTTinterval(messageString);
} else if (messageString == "publish") {
if (messageString == "publish") {
Serial.println(F("Forcing MQTT publish now."));
publish(sensor::timestamp, sensor::co2, sensor::temperature, sensor::humidity);
#ifdef AMPEL_CSV
......@@ -135,11 +120,7 @@ namespace mqtt {
} else if (messageString == "reset") {
ESP.restart(); // softer than ESP.reset()
} else {
led_effects::showKITTWheel(color::red, 1);
Serial.println(F("Message not supported. Doing nothing."));
}
delay(50);
led_effects::onBoardLEDOff();
}
void reconnect() {
......
......@@ -62,6 +62,8 @@ namespace sensor_commands {
Serial.print(" ");
Serial.println(callbacks[i].name);
}
led_effects::showKITTWheel(color::red, 1);
Serial.println(F("Message not supported. Doing nothing."));
}
void run(const char *command) {
......
#include <Arduino.h>
#include "led_effects.h"
/** Other scripts can use this namespace, in order to define callbacks.
* Those callbacks can then be used to send commands to the sensor (reset, calibrate, night mode, ...)
......
Supports Markdown
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