-
Notifications
You must be signed in to change notification settings - Fork 4
/
sd_card.ino
97 lines (75 loc) · 2.38 KB
/
sd_card.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Set the pins used
#define cardSelect 10 // 15 for huzzah
File logfile;
bool setup_sd() {
// see if the card is present and can be initialized:
if (!SD.begin(cardSelect)) {
Serial.println("Card init. failed!");
return false;
}
pinMode(13, OUTPUT);
pinMode(8, OUTPUT);
Serial.println("Ready!");
return true;
}
void logTo_sd(float batV_value, float co2_value, float temp, float humidity) {
DateTime now = rtc.now();
// String todayString = "DATA-";
// todayString += String(now.year(), DEC);
// todayString += '-';
// todayString += String(now.month(), DEC);
// todayString += '-';
// todayString += String(now.day(), DEC);
String dataString = "";
dataString += String(now.year(), DEC);
dataString += '/';
dataString += String(now.month(), DEC);
dataString += '/';
dataString += String(now.day(), DEC);
dataString += ",";
dataString += daysOfTheWeek[now.dayOfTheWeek()];
dataString += ",";
dataString += String(now.hour(), DEC);
dataString += ':';
dataString += String(now.minute(), DEC);
dataString += ':';
dataString += String(now.second(), DEC);
// dataString += '\n';
dataString += ",";
dataString += String(co2_value, 2);
dataString += ",";
dataString += String(temp, 2);
dataString += ",";
dataString += String(humidity, 2);
dataString += ",";
dataString += String(batV_value, 2);
// Serial.println(dataString);
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
// File dataFile = SD.open("datalog.TXT", FILE_WRITE);
// todayString += ".TXT";
// char filename[] = "DATA-2021-01-22.TXT";
// char filename[] = "20210122.txt"
char filename[] = "00000000.txt";;
sprintf(filename, "%02d%02d%02d.txt", now.year(), now.month(), now.day());
if (SD.exists(filename) == false) {
Serial.print("exists == false ");
Serial.println(filename);
}
File dataFile = SD.open(filename, FILE_WRITE);
// File dataFile = SD.open("myData.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
// print to the serial port too:
// Serial.println(todayString);
Serial.println(dataString);
}
// if the file isn't open, pop up an error:
else {
Serial.print("error opening file: ");
Serial.println(filename);
}
delay(100);
}