-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrain.ino
50 lines (47 loc) · 1.6 KB
/
rain.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
typedef struct TimeData {
char filename[30];
char timestamp[10];
};
struct TimeData * getFileName(struct TimeData *timeData) {
time_t now = time(NULL);
struct tm *timeinfo = localtime(&now);
sprintf(timeData->timestamp, "%02d:%02d", timeinfo->tm_hour, timeinfo->tm_min);
sprintf(timeData->filename, "/logs/rain%02d%02d%02d.csv", timeinfo->tm_year % 100, timeinfo->tm_mon + 1, timeinfo->tm_mday);
//Serial.printf("File: %s Timestamp: %s\n", timeData->filename, timeData->timestamp);
return timeData;
}
// read dir logs and remove old files
void removeOldFiles() {
// count files
int count = 0;
Dir dir = SPIFFS.openDir("/logs");
while( dir.next()) {
count++;
}
Serial.print("Log files count: "); Serial.println(count);
if( count > 3) {
dir = SPIFFS.openDir("/logs");
while( dir.next() && count > 3) {
Serial.print( "Removing old log file: ");
Serial.println( dir.fileName());
SPIFFS.remove( dir.fileName());
count--;
}
}
}
void writeRainInfo() {
struct TimeData timeData;
getFileName(&timeData);
File f = SPIFFS.open(timeData.filename, "a");
if( !f) {
Serial.print("Can't open status file: ");
Serial.println(timeData.filename);
} else {
int isRain = !digitalRead(PIN_RAIN);
const char* rainInfo = isRain ? "wet" : "dry";
Serial.printf("File: %s Timestamp: %s Rain: %s\n", timeData.filename, timeData.timestamp, rainInfo);
f.printf("%s: %s\n", timeData.timestamp, rainInfo);
f.close();
}
removeOldFiles();
}