diff --git a/ampel-firmware.ino b/ampel-firmware.ino index bdce462c9aafa172eb06fec057d22eec56293d3b..a903956e74a0b20412224bd319747a8798d8f2c2 100644 --- a/ampel-firmware.ino +++ b/ampel-firmware.ino @@ -132,7 +132,19 @@ void loop() { // Short press for night mode, Long press for calibration. checkFlashButton(); - sensor::processData(); + if (sensor::processData()) { +#ifdef CSV_WRITER + csv_writer::logIfTimeHasCome(sensor::timestamp, sensor::co2, sensor::temperature, sensor::humidity); +#endif + +#ifdef MQTT + mqtt::publishIfTimeHasCome(sensor::timestamp, sensor::co2, sensor::temperature, sensor::humidity); +#endif + +#if defined(LORAWAN) && defined(ESP32) + lorawan::preparePayloadIfTimehasCome(); +#endif + } uint32_t duration = millis() - t0; if (duration > max_loop_duration) { diff --git a/co2_sensor.cpp b/co2_sensor.cpp index 6e3dec3a2b471d9c079e515e9a47cde5e80f5d24..59d7446619285c42f9e792d3db5339ae7d6a4d10 100644 --- a/co2_sensor.cpp +++ b/co2_sensor.cpp @@ -95,18 +95,6 @@ namespace sensor { previous_co2 = co2; } - bool updateDataIfAvailable() { - if (scd30.dataAvailable()) { - // checkTimerDeviation(); - timestamp = ntp::getLocalTime(); - co2 = scd30.getCO2(); - temperature = scd30.getTemperature(); - humidity = scd30.getHumidity(); - return true; - } - return false; - } - void startCalibrationProcess() { /** From the sensor documentation: * For best results, the sensor has to be run in a stable environment in continuous mode at @@ -157,14 +145,25 @@ namespace sensor { } } - void processData() { - bool freshData = updateDataIfAvailable(); + /** Gets fresh data if available, checks calibration status, displays CO2 levels. + * Returns true if fresh data is available, for further processing (MQTT) + */ + bool processData() { + bool freshData = scd30.dataAvailable(); + + if (freshData) { + // checkTimerDeviation(); + timestamp = ntp::getLocalTime(); + co2 = scd30.getCO2(); + temperature = scd30.getTemperature(); + humidity = scd30.getHumidity(); + } //NOTE: Data is available, but it's sometimes erroneous: the sensor outputs zero ppm but non-zero temperature and non-zero humidity. if (co2 <= 0) { // No measurement yet. Waiting. LedEffects::showWaitingLED(color::blue); - return; + return false; } /** @@ -174,22 +173,7 @@ namespace sensor { if (should_calibrate) { countStableMeasurements(); } - logToSerial(); - - //TODO: Move the 3 back to ampel-firmware.ino and remove headers from co2_sensor.h - -#ifdef CSV_WRITER - csv_writer::logIfTimeHasCome(timestamp, co2, temperature, humidity); -#endif - -#ifdef MQTT - mqtt::publishIfTimeHasCome(timestamp, co2, temperature, humidity); -#endif - -#if defined(LORAWAN) && defined(ESP32) - lorawan::preparePayloadIfTimehasCome(); -#endif } if (should_calibrate) { @@ -197,9 +181,10 @@ namespace sensor { calibrateAndRestart(); } LedEffects::showWaitingLED(waiting_color); - return; + return false; } displayCO2OnLedRing(); + return freshData; } } diff --git a/co2_sensor.h b/co2_sensor.h index fc5c051e02d0f60d4c09abbb4ca3bf7b9bac63d9..f11eb815582358a2732b34ec4afe790773683c0d 100644 --- a/co2_sensor.h +++ b/co2_sensor.h @@ -7,16 +7,8 @@ #include "config.h" #include "led_effects.h" #include "util.h" -#include "csv_writer.h" // To close filesystem before restart. #include <Wire.h> -#ifdef MQTT -# include "mqtt.h" -#endif -#ifdef LORAWAN -# include "lorawan.h" -#endif - namespace config { extern uint16_t measurement_timestep; // [s] Value between 2 and 1800 (range for SCD30 sensor) extern const bool auto_calibrate_sensor; // [true / false] @@ -32,7 +24,7 @@ namespace sensor { extern String timestamp; void initialize(); - void processData(); + bool processData(); void startCalibrationProcess(); } #endif