change CD logging to single file

This commit is contained in:
cyberman54 2022-01-30 15:25:46 +01:00
parent dffb5dee75
commit bce80542a8
2 changed files with 77 additions and 46 deletions

View File

@ -1,15 +1,17 @@
#ifndef _SDCARD_H #ifndef _SDCARD_H
#define _SDCARD_H #define _SDCARD_H
#include <globals.h> #include "globals.h"
#include <stdio.h> #include <stdio.h>
#include <SPI.h> #include <SPI.h>
#if (HAS_SDCARD) #if (HAS_SDCARD)
#if HAS_SDCARD == 1 #if HAS_SDCARD == 1
#include <mySD.h> #include <mySD.h>
#define MYSD SD
#elif HAS_SDCARD == 2 #elif HAS_SDCARD == 2
#include <SD_MMC.h> #include <SD_MMC.h>
#define MYSD SD_MMC
#else #else
#error HAS_SDCARD unknown card reader value, must be either 1 or 2 #error HAS_SDCARD unknown card reader value, must be either 1 or 2
#endif #endif
@ -54,7 +56,7 @@
#define SDCARD_DATA3 13 #define SDCARD_DATA3 13
#endif #endif
#define SDCARD_FILE_NAME "/paxcount.%02d" #define SDCARD_FILE_NAME clientId
#define SDCARD_FILE_HEADER "timestamp,wifi,ble" #define SDCARD_FILE_HEADER "timestamp,wifi,ble"
#if (defined BAT_MEASURE_ADC || defined HAS_PMU) #if (defined BAT_MEASURE_ADC || defined HAS_PMU)
@ -62,7 +64,7 @@
#endif #endif
bool sdcard_init(bool create = true); bool sdcard_init(bool create = true);
bool sdcard_close(void); void sdcard_close(void);
void sdcardWriteData(uint16_t, uint16_t, uint16_t = 0); void sdcardWriteData(uint16_t, uint16_t, uint16_t = 0);
#endif // _SDCARD_H #endif // _SDCARD_H

View File

@ -1,4 +1,7 @@
// routines for writing data to an SD-card, if present // 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 // Local logging tag
static const char TAG[] = __FILE__; static const char TAG[] = __FILE__;
@ -7,19 +10,16 @@ static const char TAG[] = __FILE__;
#ifdef HAS_SDCARD #ifdef HAS_SDCARD
static bool useSDCard; static bool useSDCard = false;
static void openFile(void);
static void createFile(void);
File fileSDCard; File fileSDCard;
bool sdcard_close(void) {
ESP_LOGD(TAG, "unmounting SD-card");
fileSDCard.flush();
fileSDCard.end();
}
bool sdcard_init(bool create) { bool sdcard_init(bool create) {
uint8_t cardType;
uint64_t cardSize;
ESP_LOGI(TAG, "looking for SD-card..."); ESP_LOGI(TAG, "looking for SD-card...");
// for usage of SD drivers on ESP32 platform see // 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 // 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 #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 #elif HAS_SDCARD == 2 // use SD MMC host driver
// enable internal pullups of sd-data lines // 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_DATA0), GPIO_PULLUP_ONLY);
gpio_set_pull_mode(gpio_num_t(SDCARD_DATA1), 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_DATA2), GPIO_PULLUP_ONLY);
gpio_set_pull_mode(gpio_num_t(SDCARD_DATA3), GPIO_PULLUP_ONLY); gpio_set_pull_mode(gpio_num_t(SDCARD_DATA3), GPIO_PULLUP_ONLY);
useSDCard = SD_MMC.begin(); useSDCard = MYSD.begin();
#endif #endif
if (useSDCard) { if (useSDCard) {
ESP_LOGI(TAG, "SD-card found"); ESP_LOGI(TAG, "SD-card found");
createFile(); cardType = MYSD.cardType();
} else cardSize = MYSD.cardSize() / (1024 * 1024);
} else {
ESP_LOGI(TAG, "SD-card not found"); 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, void sdcardWriteData(uint16_t noWifi, uint16_t noBle,
@ -60,7 +89,7 @@ void sdcardWriteData(uint16_t noWifi, uint16_t noBle,
if (!useSDCard) if (!useSDCard)
return; return;
ESP_LOGD(TAG, "writing to SD-card"); ESP_LOGI(TAG, "SD: writing data");
strftime(tempBuffer, sizeof(tempBuffer), "%FT%TZ", &tt); strftime(tempBuffer, sizeof(tempBuffer), "%FT%TZ", &tt);
fileSDCard.print(tempBuffer); fileSDCard.print(tempBuffer);
snprintf(tempBuffer, sizeof(tempBuffer), ",%d,%d", noWifi, noBle); snprintf(tempBuffer, sizeof(tempBuffer), ",%d,%d", noWifi, noBle);
@ -78,51 +107,51 @@ void sdcardWriteData(uint16_t noWifi, uint16_t noBle,
if (++counterWrites > 2) { if (++counterWrites > 2) {
// force writing to SD-card // force writing to SD-card
ESP_LOGD(TAG, "flushing data to card"); ESP_LOGI(TAG, "SD: flushing data");
fileSDCard.flush(); fileSDCard.flush();
counterWrites = 0; counterWrites = 0;
} }
} }
void createFile(void) { void openFile(void) {
char bufferFilename[8 + 1 + 3 + 1]; char bufferFilename[30];
useSDCard = false; useSDCard = false;
for (int i = 0; i < 100; i++) { snprintf(bufferFilename, sizeof(bufferFilename), "/%s.csv", SDCARD_FILE_NAME);
sprintf(bufferFilename, SDCARD_FILE_NAME, i); ESP_LOGI(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); if (MYSD.exists(bufferFilename)) {
#elif HAS_SDCARD == 2 if (MYSD.open(bufferFilename, FILE_APPEND))
bool fileExists = SD_MMC.exists(bufferFilename); useSDCard = true;
#endif } else {
ESP_LOGI(TAG, "SD: file does not exist, creating it");
if (MYSD.open(bufferFilename, FILE_WRITE))
useSDCard = true;
}
*/
if (!fileExists) { if (!MYSD.exists(bufferFilename))
ESP_LOGD(TAG, "SD: file does not exist: creating"); ESP_LOGI(TAG, "SD: file does not exist, creating it");
#if HAS_SDCARD == 1 if (MYSD.open(bufferFilename, FILE_WRITE))
fileSDCard = SD.open(bufferFilename, FILE_WRITE); useSDCard = true;
#elif HAS_SDCARD == 2
fileSDCard = SD_MMC.open(bufferFilename, FILE_WRITE);
#endif
if (fileSDCard) { if (useSDCard) {
ESP_LOGD(TAG, "SD: file opened: <%s>", bufferFilename); ESP_LOGI(TAG, "SD: file opened: <%s>", bufferFilename);
fileSDCard.print(SDCARD_FILE_HEADER); fileSDCard.print(SDCARD_FILE_HEADER);
#if (defined BAT_MEASURE_ADC || defined HAS_PMU) #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 #endif
#if (HAS_SDS011) #if (HAS_SDS011)
fileSDCard.print(SDCARD_FILE_HEADER_SDS011); fileSDCard.print(SDCARD_FILE_HEADER_SDS011);
#endif #endif
fileSDCard.println(); fileSDCard.println();
useSDCard = true; } else {
break; ESP_LOGE(TAG, "SD: file not opened error");
}
}
} }
return; return;
} }