From 343547262c6d98d24b4c37be6473cb2774e72aef Mon Sep 17 00:00:00 2001 From: dakkar Date: Fri, 1 Apr 2022 11:58:02 +0100 Subject: different sd card library actually works MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit not sure why the one from the arduino-esp32 didn't… --- .gitmodules | 3 +++ Makefile | 2 +- datalog.h | 70 +++++++++++++++++++++++++++++------------------------- esp32-micro-sdcard | 1 + 4 files changed, 43 insertions(+), 33 deletions(-) create mode 160000 esp32-micro-sdcard diff --git a/.gitmodules b/.gitmodules index 40f4491..47f7512 100644 --- a/.gitmodules +++ b/.gitmodules @@ -19,3 +19,6 @@ [submodule "enclosure/threads-scad"] path = enclosure/threads-scad url = https://github.com/rcolyer/threads-scad +[submodule "esp32-micro-sdcard"] + path = esp32-micro-sdcard + url = https://github.com/nhatuan84/esp32-micro-sdcard diff --git a/Makefile b/Makefile index 297087f..0ddaa03 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ SKETCH = main.ino -LIBS = $(abspath arduino-core arduino-i2c-scd4x arduino-sps GxEPD Adafruit-GFX-Library Adafruit_BusIO) +LIBS = $(abspath arduino-core arduino-i2c-scd4x arduino-sps GxEPD Adafruit-GFX-Library Adafruit_BusIO esp32-micro-sdcard) UPLOAD_PORT = /dev/ttyUSB0 # maybe board with eink? diff --git a/datalog.h b/datalog.h index 029ec61..da715e8 100644 --- a/datalog.h +++ b/datalog.h @@ -1,53 +1,59 @@ #pragma once -#include -#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 - */ +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) { diff --git a/esp32-micro-sdcard b/esp32-micro-sdcard new file mode 160000 index 0000000..4675ae7 --- /dev/null +++ b/esp32-micro-sdcard @@ -0,0 +1 @@ +Subproject commit 4675ae7ab61009bbc253e283edccb598e0ca4e25 -- cgit v1.2.3