From dffb5dee751529525ff2e708f3594814a38e9e5c Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Sun, 30 Jan 2022 15:25:16 +0100 Subject: [PATCH 1/2] move payloadmask define to paxcounter.conf --- src/configmanager.cpp | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/configmanager.cpp b/src/configmanager.cpp index 256c9f2d..6112444d 100644 --- a/src/configmanager.cpp +++ b/src/configmanager.cpp @@ -6,12 +6,6 @@ // Local logging tag static const char TAG[] = __FILE__; -// default settings for device data to be sent -#define PAYLOADMASK \ - ((GPS_DATA | MEMS_DATA | COUNT_DATA | SENSOR1_DATA | SENSOR2_DATA | \ - SENSOR3_DATA) & \ - (~BATT_DATA) & (~RESERVED_DATA)) - // namespace for device runtime preferences #define DEVCONFIG "paxcntcfg" From bce80542a84fec67b8a29fd289d4c6c5336d73f7 Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Sun, 30 Jan 2022 15:25:46 +0100 Subject: [PATCH 2/2] change CD logging to single file --- include/sdcard.h | 8 ++-- src/sdcard.cpp | 115 +++++++++++++++++++++++++++++------------------ 2 files changed, 77 insertions(+), 46 deletions(-) diff --git a/include/sdcard.h b/include/sdcard.h index 6718616b..5a88ea5d 100644 --- a/include/sdcard.h +++ b/include/sdcard.h @@ -1,15 +1,17 @@ #ifndef _SDCARD_H #define _SDCARD_H -#include +#include "globals.h" #include #include #if (HAS_SDCARD) #if HAS_SDCARD == 1 #include +#define MYSD SD #elif HAS_SDCARD == 2 #include +#define MYSD SD_MMC #else #error HAS_SDCARD unknown card reader value, must be either 1 or 2 #endif @@ -54,7 +56,7 @@ #define SDCARD_DATA3 13 #endif -#define SDCARD_FILE_NAME "/paxcount.%02d" +#define SDCARD_FILE_NAME clientId #define SDCARD_FILE_HEADER "timestamp,wifi,ble" #if (defined BAT_MEASURE_ADC || defined HAS_PMU) @@ -62,7 +64,7 @@ #endif bool sdcard_init(bool create = true); -bool sdcard_close(void); +void sdcard_close(void); void sdcardWriteData(uint16_t, uint16_t, uint16_t = 0); #endif // _SDCARD_H diff --git a/src/sdcard.cpp b/src/sdcard.cpp index da204982..476e4037 100644 --- a/src/sdcard.cpp +++ b/src/sdcard.cpp @@ -1,4 +1,7 @@ // routines for writing data to an SD-card, if present +// use FAT32 formatted card +// check whether your card reader supports SPI oder SDMMC and select appropriate +// SD low level driver in board hal file // Local logging tag static const char TAG[] = __FILE__; @@ -7,19 +10,16 @@ static const char TAG[] = __FILE__; #ifdef HAS_SDCARD -static bool useSDCard; - -static void createFile(void); +static bool useSDCard = false; +static void openFile(void); File fileSDCard; -bool sdcard_close(void) { - ESP_LOGD(TAG, "unmounting SD-card"); - fileSDCard.flush(); - fileSDCard.end(); -} - bool sdcard_init(bool create) { + + uint8_t cardType; + uint64_t cardSize; + ESP_LOGI(TAG, "looking for SD-card..."); // for usage of SD drivers on ESP32 platform see @@ -27,22 +27,51 @@ bool sdcard_init(bool create) { // https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/sdmmc_host.html #if HAS_SDCARD == 1 // use SD SPI host driver - useSDCard = SD.begin(SDCARD_CS, SDCARD_MOSI, SDCARD_MISO, SDCARD_SCLK); + useSDCard = MYSD.begin(SDCARD_CS, SDCARD_MOSI, SDCARD_MISO, SDCARD_SCLK); #elif HAS_SDCARD == 2 // use SD MMC host driver // enable internal pullups of sd-data lines gpio_set_pull_mode(gpio_num_t(SDCARD_DATA0), GPIO_PULLUP_ONLY); gpio_set_pull_mode(gpio_num_t(SDCARD_DATA1), GPIO_PULLUP_ONLY); gpio_set_pull_mode(gpio_num_t(SDCARD_DATA2), GPIO_PULLUP_ONLY); gpio_set_pull_mode(gpio_num_t(SDCARD_DATA3), GPIO_PULLUP_ONLY); - useSDCard = SD_MMC.begin(); + useSDCard = MYSD.begin(); #endif if (useSDCard) { ESP_LOGI(TAG, "SD-card found"); - createFile(); - } else + cardType = MYSD.cardType(); + cardSize = MYSD.cardSize() / (1024 * 1024); + } else { ESP_LOGI(TAG, "SD-card not found"); - return useSDCard; + return false; + } + + if (cardType == CARD_NONE) { + ESP_LOGI(TAG, "No SD card attached"); + return false; + } + + if (cardType == CARD_MMC) { + ESP_LOGI(TAG, "SD Card type: MMC"); + } else if (cardType == CARD_SD) { + ESP_LOGI(TAG, "SD Card type: SDSC"); + } else if (cardType == CARD_SDHC) { + ESP_LOGI(TAG, "SD Card type: SDHC"); + } else { + ESP_LOGI(TAG, "SD Card type: UNKNOWN"); + } + + ESP_LOGI(TAG, "SD Card Size: %lluMB\n", cardSize); + + openFile(); + + return true; +} + +void sdcard_close(void) { + ESP_LOGI(TAG, "closing SD-card"); + fileSDCard.flush(); + fileSDCard.close(); } void sdcardWriteData(uint16_t noWifi, uint16_t noBle, @@ -60,7 +89,7 @@ void sdcardWriteData(uint16_t noWifi, uint16_t noBle, if (!useSDCard) return; - ESP_LOGD(TAG, "writing to SD-card"); + ESP_LOGI(TAG, "SD: writing data"); strftime(tempBuffer, sizeof(tempBuffer), "%FT%TZ", &tt); fileSDCard.print(tempBuffer); snprintf(tempBuffer, sizeof(tempBuffer), ",%d,%d", noWifi, noBle); @@ -78,51 +107,51 @@ void sdcardWriteData(uint16_t noWifi, uint16_t noBle, if (++counterWrites > 2) { // force writing to SD-card - ESP_LOGD(TAG, "flushing data to card"); + ESP_LOGI(TAG, "SD: flushing data"); fileSDCard.flush(); counterWrites = 0; } } -void createFile(void) { - char bufferFilename[8 + 1 + 3 + 1]; +void openFile(void) { + char bufferFilename[30]; useSDCard = false; - for (int i = 0; i < 100; i++) { - sprintf(bufferFilename, SDCARD_FILE_NAME, i); - ESP_LOGD(TAG, "SD: looking for file <%s>", bufferFilename); + snprintf(bufferFilename, sizeof(bufferFilename), "/%s.csv", SDCARD_FILE_NAME); + ESP_LOGI(TAG, "SD: looking for file <%s>", bufferFilename); -#if HAS_SDCARD == 1 - bool fileExists = SD.exists(bufferFilename); -#elif HAS_SDCARD == 2 - bool fileExists = SD_MMC.exists(bufferFilename); -#endif + /* + if (MYSD.exists(bufferFilename)) { + if (MYSD.open(bufferFilename, FILE_APPEND)) + useSDCard = true; + } else { + ESP_LOGI(TAG, "SD: file does not exist, creating it"); + if (MYSD.open(bufferFilename, FILE_WRITE)) + useSDCard = true; + } + */ - if (!fileExists) { - ESP_LOGD(TAG, "SD: file does not exist: creating"); + if (!MYSD.exists(bufferFilename)) + ESP_LOGI(TAG, "SD: file does not exist, creating it"); -#if HAS_SDCARD == 1 - fileSDCard = SD.open(bufferFilename, FILE_WRITE); -#elif HAS_SDCARD == 2 - fileSDCard = SD_MMC.open(bufferFilename, FILE_WRITE); -#endif + if (MYSD.open(bufferFilename, FILE_WRITE)) + useSDCard = true; - if (fileSDCard) { - ESP_LOGD(TAG, "SD: file opened: <%s>", bufferFilename); - fileSDCard.print(SDCARD_FILE_HEADER); + if (useSDCard) { + ESP_LOGI(TAG, "SD: file opened: <%s>", bufferFilename); + fileSDCard.print(SDCARD_FILE_HEADER); #if (defined BAT_MEASURE_ADC || defined HAS_PMU) - fileSDCard.print(SDCARD_FILE_HEADER_VOLTAGE); // for battery level data + fileSDCard.print(SDCARD_FILE_HEADER_VOLTAGE); // for battery level data #endif #if (HAS_SDS011) - fileSDCard.print(SDCARD_FILE_HEADER_SDS011); + fileSDCard.print(SDCARD_FILE_HEADER_SDS011); #endif - fileSDCard.println(); - useSDCard = true; - break; - } - } + fileSDCard.println(); + } else { + ESP_LOGE(TAG, "SD: file not opened error"); } + return; }