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

Refactor : fewer dependencies

parent a9ddfba0
...@@ -132,7 +132,19 @@ void loop() { ...@@ -132,7 +132,19 @@ void loop() {
// Short press for night mode, Long press for calibration. // Short press for night mode, Long press for calibration.
checkFlashButton(); 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; uint32_t duration = millis() - t0;
if (duration > max_loop_duration) { if (duration > max_loop_duration) {
......
...@@ -95,18 +95,6 @@ namespace sensor { ...@@ -95,18 +95,6 @@ namespace sensor {
previous_co2 = co2; 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() { void startCalibrationProcess() {
/** From the sensor documentation: /** From the sensor documentation:
* For best results, the sensor has to be run in a stable environment in continuous mode at * For best results, the sensor has to be run in a stable environment in continuous mode at
...@@ -157,14 +145,25 @@ namespace sensor { ...@@ -157,14 +145,25 @@ namespace sensor {
} }
} }
void processData() { /** Gets fresh data if available, checks calibration status, displays CO2 levels.
bool freshData = updateDataIfAvailable(); * 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. //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) { if (co2 <= 0) {
// No measurement yet. Waiting. // No measurement yet. Waiting.
LedEffects::showWaitingLED(color::blue); LedEffects::showWaitingLED(color::blue);
return; return false;
} }
/** /**
...@@ -174,22 +173,7 @@ namespace sensor { ...@@ -174,22 +173,7 @@ namespace sensor {
if (should_calibrate) { if (should_calibrate) {
countStableMeasurements(); countStableMeasurements();
} }
logToSerial(); 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) { if (should_calibrate) {
...@@ -197,9 +181,10 @@ namespace sensor { ...@@ -197,9 +181,10 @@ namespace sensor {
calibrateAndRestart(); calibrateAndRestart();
} }
LedEffects::showWaitingLED(waiting_color); LedEffects::showWaitingLED(waiting_color);
return; return false;
} }
displayCO2OnLedRing(); displayCO2OnLedRing();
return freshData;
} }
} }
...@@ -7,16 +7,8 @@ ...@@ -7,16 +7,8 @@
#include "config.h" #include "config.h"
#include "led_effects.h" #include "led_effects.h"
#include "util.h" #include "util.h"
#include "csv_writer.h" // To close filesystem before restart.
#include <Wire.h> #include <Wire.h>
#ifdef MQTT
# include "mqtt.h"
#endif
#ifdef LORAWAN
# include "lorawan.h"
#endif
namespace config { namespace config {
extern uint16_t measurement_timestep; // [s] Value between 2 and 1800 (range for SCD30 sensor) extern uint16_t measurement_timestep; // [s] Value between 2 and 1800 (range for SCD30 sensor)
extern const bool auto_calibrate_sensor; // [true / false] extern const bool auto_calibrate_sensor; // [true / false]
...@@ -32,7 +24,7 @@ namespace sensor { ...@@ -32,7 +24,7 @@ namespace sensor {
extern String timestamp; extern String timestamp;
void initialize(); void initialize();
void processData(); bool processData();
void startCalibrationProcess(); void startCalibrationProcess();
} }
#endif #endif
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