ESP32-PaxCounter/src/sdcard.cpp

159 lines
4.1 KiB
C++
Raw Normal View History

2019-12-25 23:07:34 +01:00
// routines for writing data to an SD-card, if present
2022-01-30 15:25:46 +01:00
// use FAT32 formatted card
// check whether your card reader supports SPI oder SDMMC and select appropriate
// SD low level driver in board hal file
2019-12-25 23:07:34 +01:00
// Local logging tag
static const char TAG[] = __FILE__;
#include "sdcard.h"
2020-06-06 15:09:12 +02:00
#ifdef HAS_SDCARD
2022-01-30 15:25:46 +01:00
static bool useSDCard = false;
static void openFile(void);
2020-09-01 11:52:07 +02:00
2019-12-25 23:07:34 +01:00
File fileSDCard;
2022-01-29 13:47:59 +01:00
bool sdcard_init(bool create) {
2022-01-30 15:25:46 +01:00
uint8_t cardType;
uint64_t cardSize;
2020-05-16 15:30:05 +02:00
ESP_LOGI(TAG, "looking for SD-card...");
2020-05-21 19:42:42 +02:00
2020-06-06 15:09:12 +02:00
// for usage of SD drivers on ESP32 platform see
// https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/sdspi_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
2022-01-30 15:25:46 +01:00
useSDCard = MYSD.begin(SDCARD_CS, SDCARD_MOSI, SDCARD_MISO, SDCARD_SCLK);
2020-06-06 15:09:12 +02:00
#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);
2022-01-30 15:25:46 +01:00
useSDCard = MYSD.begin();
2020-05-16 15:30:05 +02:00
#endif
if (useSDCard) {
ESP_LOGI(TAG, "SD-card found");
2022-01-30 15:25:46 +01:00
cardType = MYSD.cardType();
cardSize = MYSD.cardSize() / (1024 * 1024);
} else {
2020-05-16 15:30:05 +02:00
ESP_LOGI(TAG, "SD-card not found");
2022-01-30 15:25:46 +01:00
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();
2019-12-25 23:07:34 +01:00
}
void sdcardWriteData(uint16_t noWifi, uint16_t noBle,
__attribute__((unused)) uint16_t voltage) {
2019-12-25 23:07:34 +01:00
static int counterWrites = 0;
2022-01-28 20:57:31 +01:00
char tempBuffer[20 + 1];
time_t t = time(NULL);
2022-01-20 17:37:34 +01:00
struct tm tt;
2022-01-28 20:57:31 +01:00
gmtime_r(&t, &tt); // make UTC timestamp
2022-01-20 17:37:34 +01:00
2020-02-25 22:18:20 +01:00
#if (HAS_SDS011)
sdsStatus_t sds;
#endif
2019-12-25 23:07:34 +01:00
if (!useSDCard)
return;
2022-01-30 15:25:46 +01:00
ESP_LOGI(TAG, "SD: writing data");
2022-01-28 20:57:31 +01:00
strftime(tempBuffer, sizeof(tempBuffer), "%FT%TZ", &tt);
2019-12-25 23:07:34 +01:00
fileSDCard.print(tempBuffer);
2022-01-28 20:57:31 +01:00
snprintf(tempBuffer, sizeof(tempBuffer), ",%d,%d", noWifi, noBle);
2020-02-25 22:18:20 +01:00
fileSDCard.print(tempBuffer);
#if (defined BAT_MEASURE_ADC || defined HAS_PMU)
snprintf(tempBuffer, sizeof(tempBuffer), ",%d", voltage);
2020-09-01 11:52:07 +02:00
fileSDCard.print(tempBuffer);
#endif
2020-01-21 14:54:35 +01:00
#if (HAS_SDS011)
2020-02-25 22:18:20 +01:00
sds011_store(&sds);
2022-01-28 20:57:31 +01:00
snprintf(tempBuffer, sizeof(tempBuffer), ",%5.1f,%4.1f", sds.pm10, sds.pm25);
2020-02-25 22:18:20 +01:00
fileSDCard.print(tempBuffer);
2020-01-21 14:54:35 +01:00
#endif
2020-02-25 22:18:20 +01:00
fileSDCard.println();
2019-12-25 23:07:34 +01:00
if (++counterWrites > 2) {
// force writing to SD-card
2022-01-30 15:25:46 +01:00
ESP_LOGI(TAG, "SD: flushing data");
2019-12-25 23:07:34 +01:00
fileSDCard.flush();
counterWrites = 0;
}
}
2022-01-30 15:25:46 +01:00
void openFile(void) {
char bufferFilename[30];
2019-12-25 23:07:34 +01:00
useSDCard = false;
2022-01-30 15:25:46 +01:00
snprintf(bufferFilename, sizeof(bufferFilename), "/%s.csv", SDCARD_FILE_NAME);
ESP_LOGI(TAG, "SD: looking for file <%s>", bufferFilename);
2020-05-16 15:30:05 +02:00
2022-01-30 15:25:46 +01:00
/*
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;
}
*/
2020-05-16 15:30:05 +02:00
2022-01-30 15:25:46 +01:00
if (!MYSD.exists(bufferFilename))
ESP_LOGI(TAG, "SD: file does not exist, creating it");
2020-05-16 15:30:05 +02:00
2022-01-30 15:25:46 +01:00
if (MYSD.open(bufferFilename, FILE_WRITE))
useSDCard = true;
2020-05-16 15:30:05 +02:00
2022-01-30 15:25:46 +01:00
if (useSDCard) {
ESP_LOGI(TAG, "SD: file opened: <%s>", bufferFilename);
fileSDCard.print(SDCARD_FILE_HEADER);
#if (defined BAT_MEASURE_ADC || defined HAS_PMU)
2022-01-30 15:25:46 +01:00
fileSDCard.print(SDCARD_FILE_HEADER_VOLTAGE); // for battery level data
2020-09-01 11:52:07 +02:00
#endif
2020-01-21 14:54:35 +01:00
#if (HAS_SDS011)
2022-01-30 15:25:46 +01:00
fileSDCard.print(SDCARD_FILE_HEADER_SDS011);
2020-01-21 14:54:35 +01:00
#endif
2022-01-30 15:25:46 +01:00
fileSDCard.println();
} else {
ESP_LOGE(TAG, "SD: file not opened error");
2019-12-25 23:07:34 +01:00
}
2022-01-30 15:25:46 +01:00
2019-12-25 23:07:34 +01:00
return;
2019-12-30 17:25:05 +01:00
}
2020-02-03 15:27:49 +01:00
#endif // (HAS_SDCARD)