From dbdb59f0d069049a95efeb01e287dd51aca4f123 Mon Sep 17 00:00:00 2001 From: dakkar Date: Fri, 1 Apr 2022 10:59:46 +0100 Subject: start of data logging to sd card --- datalog.h | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ display.h | 4 ++++ main.ino | 13 +++++++++++-- 3 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 datalog.h 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 +#include + +#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; + } + } +}; diff --git a/display.h b/display.h index 8e9616f..b6dd433 100644 --- a/display.h +++ b/display.h @@ -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; diff --git a/main.ino b/main.ino index 180f5cd..9fcabbe 100644 --- a/main.ino +++ b/main.ino @@ -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); } -- cgit v1.2.3