2019-12-25 23:07:34 +01:00
|
|
|
// routines for writing data to an SD-card, if present
|
|
|
|
|
2019-12-30 18:10:44 +01:00
|
|
|
#if (HAS_SDCARD)
|
2019-12-30 17:25:05 +01:00
|
|
|
|
2019-12-25 23:07:34 +01:00
|
|
|
// Local logging tag
|
|
|
|
static const char TAG[] = __FILE__;
|
|
|
|
|
|
|
|
#include "sdcard.h"
|
|
|
|
|
|
|
|
static bool useSDCard;
|
|
|
|
|
|
|
|
static void createFile(void);
|
|
|
|
|
|
|
|
File fileSDCard;
|
|
|
|
|
2020-02-03 15:27:49 +01:00
|
|
|
bool sdcard_init() {
|
2019-12-25 23:07:34 +01:00
|
|
|
ESP_LOGD(TAG, "looking for SD-card...");
|
|
|
|
useSDCard = SD.begin(SDCARD_CS, SDCARD_MOSI, SDCARD_MISO, SDCARD_SCLK);
|
|
|
|
if (useSDCard)
|
|
|
|
createFile();
|
2020-02-25 15:44:56 +01:00
|
|
|
else
|
2020-02-25 22:18:20 +01:00
|
|
|
ESP_LOGD(TAG, "SD-card not found");
|
2019-12-25 23:07:34 +01:00
|
|
|
return useSDCard;
|
|
|
|
}
|
|
|
|
|
|
|
|
void sdcardWriteData(uint16_t noWifi, uint16_t noBle) {
|
|
|
|
static int counterWrites = 0;
|
|
|
|
char tempBuffer[12 + 1];
|
|
|
|
time_t t = now();
|
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");
|
|
|
|
sprintf(tempBuffer, "%02d.%02d.%4d,", day(t), month(t), year(t));
|
|
|
|
fileSDCard.print(tempBuffer);
|
|
|
|
sprintf(tempBuffer, "%02d:%02d:%02d,", hour(t), minute(t), second(t));
|
|
|
|
fileSDCard.print(tempBuffer);
|
|
|
|
sprintf(tempBuffer, "%d,%d", noWifi, noBle);
|
2020-02-25 22:18:20 +01:00
|
|
|
fileSDCard.print(tempBuffer);
|
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);
|
|
|
|
ESP_LOGD(TAG, "SD: looking for file <%s>", bufferFilename);
|
|
|
|
bool fileExists = SD.exists(bufferFilename);
|
|
|
|
if (!fileExists) {
|
|
|
|
ESP_LOGD(TAG, "SD: file does not exist: opening");
|
|
|
|
fileSDCard = SD.open(bufferFilename, FILE_WRITE);
|
|
|
|
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-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)
|