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 | |
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
-rw-r--r-- | datalog.h | 54 | ||||
-rw-r--r-- | display.h | 4 | ||||
-rw-r--r-- | main.ino | 13 |
3 files changed, 69 insertions, 2 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; + } + } +}; @@ -42,6 +42,10 @@ public: } }; +/* + most of this comes from + https://github.com/Xinyuan-LilyGO/LilyGo-T5-Epaper-Series/blob/master/examples/GxEPD_Partial_Update_Example/GxEPD_Partial_Update_Example.ino + */ class Display { private: GxIO_Class io; @@ -4,6 +4,7 @@ #include "data.h" #include "battery.h" #include "display.h" +#include "datalog.h" #include "co2.h" #include "pm.h" @@ -14,6 +15,7 @@ CO2 co2; SensorData data; Display display; +DataLog datalog; void setup() { Serial.begin(115200); @@ -27,6 +29,10 @@ void setup() { display.start(); display.show(&data); + // I think this has to come after the display.start(), they may both + // use the same SPI thing + datalog.start(); + battery.start(); co2.start(); pm.start(); @@ -42,7 +48,10 @@ void loop() { if (pm.dataReady()) { changed = true; pm.read(&data); } if (co2.dataReady()) { changed = true; co2.read(&data); } - if (changed) display.show(&data); - + if (changed) { + display.show(&data); + datalog.show(&data); + } + delay(5000); } |