diff options
Diffstat (limited to 'datalog.h')
-rw-r--r-- | datalog.h | 70 |
1 files changed, 38 insertions, 32 deletions
@@ -1,53 +1,59 @@ #pragma once -#include <SD.h> -#include <FS.h> +#include <mySD.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 - */ +const int mySD_CS=13; +const int mySD_SCLK=14; +const int mySD_MISO=2; +const int mySD_MOSI=15; + +// 12345, 12345, 12345, 12345, 123456, 123456, 123456, 123456, 12345\n +const char csvHeader[]=" secs, co2, temp, humid, pm1, pm2.5, pm4, pm10, batt\n"; +const char csvFormat[]="% 5lu, % 5u, % 5.1f, % 5.1f, % 6.1f, % 6.1f, % 6.1f, % 6.1f, % 5.2f\n"; +const size_t lineLength=66; + 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) {} + DataLog(uint8_t ft=4) : flushThreshold(ft) {} void start() { - SDSPI.begin(SDCARD_SCLK, SDCARD_MISO, SDCARD_MOSI); - bool sdok=SD.begin(SDCARD_CS, SDSPI); + bool sdok=SD.begin(mySD_CS, mySD_MOSI, mySD_MISO, mySD_SCLK); Serial.print("sdok: ");Serial.println(sdok); - Serial.println(SD.cardSize()); - logfile = SD.open("air-quality.csv",FILE_APPEND,true); - Serial.print("file: ");Serial.println(logfile.path()); - linesSinceLastFlush=0; + logfile = SD.open("quality.csv",FILE_WRITE); + Serial.print("file: ");Serial.println(logfile.name()); + uint32_t fileSize = logfile.size(); + logfile.seek(fileSize); + if (fileSize == 0) { + logfile.write((uint8_t*)csvHeader, lineLength); + } + linesSinceLastFlush=1; } 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 - ); - size_t written=logfile.write((uint8_t *)line,65); + snprintf( + line, lineLength+1, // zero terminator! + csvFormat, + 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 + ); + size_t written=logfile.write((uint8_t *)line,lineLength); Serial.write(line); Serial.print("written: ");Serial.println(written); if (++linesSinceLastFlush > flushThreshold) { |