2019-12-25 23:07:34 +01:00
|
|
|
// routines for writing data to an SD-card, if present
|
|
|
|
|
|
|
|
// Local logging tag
|
|
|
|
static const char TAG[] = __FILE__;
|
|
|
|
|
|
|
|
#include "sdcard.h"
|
|
|
|
|
2020-06-06 15:09:12 +02:00
|
|
|
#ifdef HAS_SDCARD
|
|
|
|
|
2019-12-25 23:07:34 +01:00
|
|
|
static bool useSDCard;
|
|
|
|
|
2020-09-01 11:52:07 +02:00
|
|
|
static void createFile(void);
|
|
|
|
|
2019-12-25 23:07:34 +01:00
|
|
|
File fileSDCard;
|
|
|
|
|
2020-02-03 15:27:49 +01:00
|
|
|
bool sdcard_init() {
|
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
|
2019-12-25 23:07:34 +01:00
|
|
|
useSDCard = SD.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
|
2020-10-31 12:05:26 +01:00
|
|
|
// 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);
|
2020-05-16 15:30:05 +02:00
|
|
|
useSDCard = SD_MMC.begin();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (useSDCard) {
|
|
|
|
ESP_LOGI(TAG, "SD-card found");
|
2019-12-25 23:07:34 +01:00
|
|
|
createFile();
|
2020-05-16 15:30:05 +02:00
|
|
|
} else
|
|
|
|
ESP_LOGI(TAG, "SD-card not found");
|
2019-12-25 23:07:34 +01:00
|
|
|
return useSDCard;
|
|
|
|
}
|
|
|
|
|
2020-10-31 12:05:26 +01:00
|
|
|
void sdcardWriteData(uint16_t noWifi, uint16_t noBle,
|
|
|
|
__attribute__((unused)) uint16_t noBleCWA) {
|
2019-12-25 23:07:34 +01:00
|
|
|
static int counterWrites = 0;
|
|
|
|
char tempBuffer[12 + 1];
|
2022-01-13 23:30:18 +01:00
|
|
|
time_t t = time(NULL);
|
2022-01-20 17:37:34 +01:00
|
|
|
struct tm tt;
|
|
|
|
localtime_r(&t, &tt);
|
|
|
|
mktime(&tt);
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
ESP_LOGD(TAG, "writing to SD-card");
|
2022-01-20 17:37:34 +01:00
|
|
|
strftime(tempBuffer, sizeof(tempBuffer), "%d.%m.%Y", &tt);
|
2019-12-25 23:07:34 +01:00
|
|
|
fileSDCard.print(tempBuffer);
|
2022-01-20 17:37:34 +01:00
|
|
|
strftime(tempBuffer, sizeof(tempBuffer), "%H.%M.%S", &tt);
|
2019-12-25 23:07:34 +01:00
|
|
|
fileSDCard.print(tempBuffer);
|
|
|
|
sprintf(tempBuffer, "%d,%d", noWifi, noBle);
|
2020-02-25 22:18:20 +01:00
|
|
|
fileSDCard.print(tempBuffer);
|
2020-09-02 11:21:56 +02:00
|
|
|
#if (COUNT_ENS)
|
2020-09-01 11:52:07 +02:00
|
|
|
sprintf(tempBuffer, ",%d", noBleCWA);
|
|
|
|
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);
|
|
|
|
sprintf(tempBuffer, ",%5.1f,%4.1f", sds.pm10, sds.pm25);
|
|
|
|
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
|
|
|
|
ESP_LOGD(TAG, "flushing data to card");
|
|
|
|
fileSDCard.flush();
|
|
|
|
counterWrites = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void createFile(void) {
|
|
|
|
char bufferFilename[8 + 1 + 3 + 1];
|
|
|
|
|
|
|
|
useSDCard = false;
|
|
|
|
|
|
|
|
for (int i = 0; i < 100; i++) {
|
|
|
|
sprintf(bufferFilename, SDCARD_FILE_NAME, i);
|
2020-05-21 19:42:42 +02:00
|
|
|
// ESP_LOGD(TAG, "SD: looking for file <%s>", bufferFilename);
|
2020-05-16 15:30:05 +02:00
|
|
|
|
|
|
|
#if HAS_SDCARD == 1
|
2019-12-25 23:07:34 +01:00
|
|
|
bool fileExists = SD.exists(bufferFilename);
|
2020-05-16 15:30:05 +02:00
|
|
|
#elif HAS_SDCARD == 2
|
|
|
|
bool fileExists = SD_MMC.exists(bufferFilename);
|
|
|
|
#endif
|
|
|
|
|
2019-12-25 23:07:34 +01:00
|
|
|
if (!fileExists) {
|
2020-05-21 19:42:42 +02:00
|
|
|
// ESP_LOGD(TAG, "SD: file does not exist: opening");
|
2020-05-16 15:30:05 +02:00
|
|
|
|
|
|
|
#if HAS_SDCARD == 1
|
2019-12-25 23:07:34 +01:00
|
|
|
fileSDCard = SD.open(bufferFilename, FILE_WRITE);
|
2020-05-16 15:30:05 +02:00
|
|
|
#elif HAS_SDCARD == 2
|
|
|
|
fileSDCard = SD_MMC.open(bufferFilename, FILE_WRITE);
|
|
|
|
#endif
|
|
|
|
|
2019-12-25 23:07:34 +01:00
|
|
|
if (fileSDCard) {
|
2020-01-21 14:54:35 +01:00
|
|
|
ESP_LOGD(TAG, "SD: name opened: <%s>", bufferFilename);
|
2020-02-25 22:18:20 +01:00
|
|
|
fileSDCard.print(SDCARD_FILE_HEADER);
|
2020-09-02 11:21:56 +02:00
|
|
|
#if (COUNT_ENS)
|
2020-10-31 12:05:26 +01:00
|
|
|
fileSDCard.print(SDCARD_FILE_HEADER_CWA); // for Corona-data (CWA)
|
2020-09-01 11:52:07 +02:00
|
|
|
#endif
|
2020-01-21 14:54:35 +01:00
|
|
|
#if (HAS_SDS011)
|
2020-02-25 22:18:20 +01:00
|
|
|
fileSDCard.print(SDCARD_FILE_HEADER_SDS011);
|
2020-01-21 14:54:35 +01:00
|
|
|
#endif
|
|
|
|
fileSDCard.println();
|
2019-12-25 23:07:34 +01:00
|
|
|
useSDCard = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
2019-12-30 17:25:05 +01:00
|
|
|
}
|
|
|
|
|
2020-02-03 15:27:49 +01:00
|
|
|
#endif // (HAS_SDCARD)
|