diff options
author | dakkar <dakkar@thenautilus.net> | 2022-04-01 10:59:46 +0100 |
---|---|---|
committer | dakkar <dakkar@thenautilus.net> | 2022-04-01 10:59:46 +0100 |
commit | dbdb59f0d069049a95efeb01e287dd51aca4f123 (patch) | |
tree | 6de5e8d1472f534f556bd440721aafb215fe13c3 /datalog.h | |
parent | clear display on startup (diff) | |
download | env-sensor-dbdb59f0d069049a95efeb01e287dd51aca4f123.tar.gz env-sensor-dbdb59f0d069049a95efeb01e287dd51aca4f123.tar.bz2 env-sensor-dbdb59f0d069049a95efeb01e287dd51aca4f123.zip |
start of data logging to sd card
Diffstat (limited to 'datalog.h')
-rw-r--r-- | datalog.h | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/datalog.h b/datalog.h new file mode 100644 index 0000000..39473e3 --- /dev/null +++ b/datalog.h @@ -0,0 +1,54 @@ +#pragma once + +#include <SD.h> +#include <FS.h> + +#include "data.h" + +/* + most of this comes from + https://github.com/Xinyuan-LilyGO/LilyGo-T5-Epaper-Series/blob/master/examples/GxEPD_SD_1.02_Example/SD_Example.ino + */ +class DataLog { +private: + SPIClass SDSPI; + File logfile; + char line[100]; + uint8_t linesSinceLastFlush; + uint8_t flushThreshold; +public: + DataLog(uint8_t ft=4) : SDSPI(VSPI), flushThreshold(ft) {} + + void start() { + SDSPI.begin(SDCARD_SCLK, SDCARD_MISO, SDCARD_MOSI); + SD.begin(SDCARD_CS, SDSPI); + logfile = SD.open("air-quality.csv",FILE_APPEND,true); + linesSinceLastFlush=0; + } + + void show(const SensorData *data) { + sprintf( + line, + //milli co2 temp humid pm1 pm2.5 pm4 pm10 battery + "% 5lu, % 5u, % 5.1f, % 5.1f, % 6.1f, % 6.1f, % 6.1f, % 6.1f, % 5.2f\n", + millis()/1000, + + data->co2, + data->temperature, + data->humidity, + + data->pm.mc_1p0, + max(0.0f,data->pm.mc_2p5 - data->pm.mc_1p0), + max(0.0f,data->pm.mc_4p0 - data->pm.mc_2p5), + max(0.0f,data->pm.mc_10p0 - data->pm.mc_4p0), + + data->batteryVoltage + ); + logfile.write((uint8_t *)line,65); + Serial.write(line); + if (++linesSinceLastFlush > flushThreshold) { + logfile.flush(); + linesSinceLastFlush=0; + } + } +}; |