From 854480a051a791a9972a0bd1451b6b3140f39077 Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Fri, 28 Jan 2022 20:57:31 +0100 Subject: [PATCH] sdcard: timestamp in iso8601 format --- README.md | 12 ++++++------ include/sdcard.h | 2 +- src/sdcard.cpp | 21 +++++++++------------ src/senddata.cpp | 23 ++++++++++------------- 4 files changed, 26 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index 1cf6607c..dc9bae6c 100644 --- a/README.md +++ b/README.md @@ -256,12 +256,12 @@ The data is written to the card and after 3 write-operations the data is flushed And finally: this is the data written to the disk: - date, time, wifi, bluet - 00.00.1970,00:01:09,2,0 - 00.00.1970,00:02:09,1,0 - 00.00.1970,00:03:09,2,0 - -Format of the data is CSV, which can easily imported into LibreOffice, Excel, ..... + timestamp, wifi, ble + 2022-01-28T19:36:35Z,17,48 + 2022-01-28T19:37:35Z,21,52 + 2022-01-28T19:38:35Z,14,49 + +Format of the data is CSV, timestamp is ISO8601, which can easily imported into LibreOffice, Excel, Influx, etc. If you want to change this please look into src/sdcard.cpp and include/sdcard.h. diff --git a/include/sdcard.h b/include/sdcard.h index 5c34dc11..8d20ced5 100644 --- a/include/sdcard.h +++ b/include/sdcard.h @@ -55,7 +55,7 @@ #endif #define SDCARD_FILE_NAME "/paxcount.%02d" -#define SDCARD_FILE_HEADER "date, time, wifi, bluet" +#define SDCARD_FILE_HEADER "timestamp, wifi, ble" #if (COUNT_ENS) #define SDCARD_FILE_HEADER_CWA ",cwa" diff --git a/src/sdcard.cpp b/src/sdcard.cpp index 925ab207..783cf657 100644 --- a/src/sdcard.cpp +++ b/src/sdcard.cpp @@ -42,11 +42,10 @@ bool sdcard_init() { void sdcardWriteData(uint16_t noWifi, uint16_t noBle, __attribute__((unused)) uint16_t noBleCWA) { static int counterWrites = 0; - char tempBuffer[12 + 1]; + char tempBuffer[20 + 1]; time_t t = time(NULL); struct tm tt; - localtime_r(&t, &tt); - mktime(&tt); + gmtime_r(&t, &tt); // make UTC timestamp #if (HAS_SDS011) sdsStatus_t sds; @@ -56,19 +55,17 @@ void sdcardWriteData(uint16_t noWifi, uint16_t noBle, return; ESP_LOGD(TAG, "writing to SD-card"); - strftime(tempBuffer, sizeof(tempBuffer), "%d.%m.%Y", &tt); + strftime(tempBuffer, sizeof(tempBuffer), "%FT%TZ", &tt); fileSDCard.print(tempBuffer); - strftime(tempBuffer, sizeof(tempBuffer), "%H.%M.%S", &tt); - fileSDCard.print(tempBuffer); - sprintf(tempBuffer, "%d,%d", noWifi, noBle); + snprintf(tempBuffer, sizeof(tempBuffer), ",%d,%d", noWifi, noBle); fileSDCard.print(tempBuffer); #if (COUNT_ENS) - sprintf(tempBuffer, ",%d", noBleCWA); + snprintf(tempBuffer, sizeof(tempBuffer), ",%d", noBleCWA); fileSDCard.print(tempBuffer); #endif #if (HAS_SDS011) sds011_store(&sds); - sprintf(tempBuffer, ",%5.1f,%4.1f", sds.pm10, sds.pm25); + snprintf(tempBuffer, sizeof(tempBuffer), ",%5.1f,%4.1f", sds.pm10, sds.pm25); fileSDCard.print(tempBuffer); #endif fileSDCard.println(); @@ -88,7 +85,7 @@ void createFile(void) { for (int i = 0; i < 100; i++) { sprintf(bufferFilename, SDCARD_FILE_NAME, i); - // ESP_LOGD(TAG, "SD: looking for file <%s>", bufferFilename); + ESP_LOGD(TAG, "SD: looking for file <%s>", bufferFilename); #if HAS_SDCARD == 1 bool fileExists = SD.exists(bufferFilename); @@ -97,7 +94,7 @@ void createFile(void) { #endif if (!fileExists) { - // ESP_LOGD(TAG, "SD: file does not exist: opening"); + ESP_LOGD(TAG, "SD: file does not exist: creating"); #if HAS_SDCARD == 1 fileSDCard = SD.open(bufferFilename, FILE_WRITE); @@ -106,7 +103,7 @@ void createFile(void) { #endif if (fileSDCard) { - ESP_LOGD(TAG, "SD: name opened: <%s>", bufferFilename); + ESP_LOGD(TAG, "SD: file opened: <%s>", bufferFilename); fileSDCard.print(SDCARD_FILE_HEADER); #if (COUNT_ENS) fileSDCard.print(SDCARD_FILE_HEADER_CWA); // for Corona-data (CWA) diff --git a/src/senddata.cpp b/src/senddata.cpp index 5a310761..fce1d325 100644 --- a/src/senddata.cpp +++ b/src/senddata.cpp @@ -65,18 +65,6 @@ void SendPayload(uint8_t port) { mqtt_enqueuedata(&SendBuffer); #endif -// write data to sdcard, if present -#if (HAS_SDCARD) - if (port == COUNTERPORT) { - sdcardWriteData(libpax_macs_wifi, libpax_macs_ble -#if (COUNT_ENS) - , - cwa_report() -#endif - ); - } -#endif - } // SendPayload // interrupt triggered function to prepare payload to send @@ -130,7 +118,16 @@ void sendData() { #ifdef HAS_DISPLAY dp_plotCurve(libpax_macs_ble + libpax_macs_wifi, true); #endif - break; +#if (HAS_SDCARD) + sdcardWriteData(libpax_macs_wifi, libpax_macs_ble +#if (COUNT_ENS) + , + cwa_report() +#endif + ); +#endif // HAS_SDCARD + + break; // case COUNTDATA #endif #if (HAS_BME)