ENTS-tock
Environmental NeTworked Sensor firmware on Tock OS
Loading...
Searching...
No Matches
example_microsd.cpp
Go to the documentation of this file.
1
11
#include <Arduino.h>
12
#include <SD.h>
13
14
// The ESP32-C3 IO7 pins:
15
16
// IO4: CLK / SCK / SCLK
17
// IO5: SDO / DO / MISO
18
// IO6: SDI / DI / MOSI
19
// IO7: CS / SS
20
const
uint8_t
chipSelect
= 7;
21
// IO10: CD "card detect" (for insertion detection).
22
// Note: Pin is floating when no card is inserted, grounded when a card is
23
// inserted.
24
const
uint8_t
cardDetect
= 10;
25
26
File
dataFile
;
27
28
const
String
filename
=
"/data.csv"
;
29
30
void
printCardInfo
(
void
);
31
void
printFileInfo
(File f);
32
void
printFileContents
(File f);
33
34
void
setup
() {
35
Serial.begin(115200);
36
37
Serial.printf(
38
"ents-node esp32 example_microsd firmware, compiled at %s %s\r\n"
,
39
__DATE__, __TIME__);
40
Serial.printf(
"Git SHA: %s\r\n\r\n\r\n"
, GIT_REV);
41
42
int
cardIsInserted;
43
pinMode(
cardDetect
, INPUT_PULLUP);
44
cardIsInserted = digitalRead(
cardDetect
);
45
if
(cardIsInserted == LOW) {
46
Serial.printf(
"Card detected (INPUT_PULLUP): %d\r\n"
, cardIsInserted);
47
}
else
{
48
Serial.printf(
"Card NOT detected (INPUT_PULLUP): %d\r\n"
, cardIsInserted);
49
}
50
51
// Note: SD.begin(chipSelect) assumes the default SCLK, MISO, MOSI pins.
52
// For non-default pin assignments, call SPI.begin(SCLK, MISO, MOSI, CS) prior
53
// to SD.begin(CS).
54
if
(!SD.begin(
chipSelect
)) {
55
Serial.printf(
56
"Failed to begin, make sure that a FAT32 formatted SD card is "
57
"inserted.\r\n"
);
58
while
(1);
59
}
60
61
printCardInfo
();
62
63
String dataString =
""
;
64
for
(
int
i
= 0;
i
< 3;
i
++) {
65
// int sensor = analogRead(i);
66
dataString += String(
i
);
67
if
(
i
< 2) {
68
dataString +=
","
;
69
}
70
}
71
Serial.printf(
"dataString to be appended: %s\r\n"
, dataString);
72
73
Serial.printf(
"Checking file existence of '%s'\r\n"
,
filename
);
74
if
(SD.exists(
filename
)) {
75
Serial.printf(
"'%s' exists.\r\n"
,
filename
);
76
}
else
{
77
Serial.printf(
"'%s' does not exist.\r\n"
,
filename
);
78
}
79
80
Serial.printf(
"Opening '%s' with '%s'\r\n"
,
filename
, FILE_READ);
81
dataFile
= SD.open(
filename
, FILE_READ);
82
if
(
dataFile
) {
83
Serial.printf(
"Successfully opened '%s' with '%s'\r\n"
,
filename
,
84
FILE_READ);
85
printFileInfo
(
dataFile
);
86
// printFileContents(dataFile);
87
Serial.printf(
"Closing '%s'\r\n"
,
filename
);
88
dataFile
.close();
89
Serial.printf(
"Closed '%s'\r\n"
,
filename
);
90
}
else
{
91
Serial.printf(
"Error opening '%s' with '%s'\r\n"
,
filename
, FILE_READ);
92
}
93
94
Serial.printf(
"Opening '%s' with '%s'\r\n"
,
filename
, FILE_APPEND);
95
dataFile
= SD.open(
filename
, FILE_APPEND);
// FILE_WRITE
96
if
(
dataFile
) {
97
Serial.printf(
"Successfully opened '%s' with '%s'\r\n"
,
filename
,
98
FILE_APPEND);
99
printFileInfo
(
dataFile
);
100
Serial.printf(
"Writing '%s' to file.\r\n"
, dataString);
101
dataFile
.printf(
"%s\r\n"
, dataString);
102
Serial.printf(
"Closing '%s'\r\n"
,
filename
);
103
dataFile
.close();
104
Serial.printf(
"Closed '%s'\r\n"
,
filename
);
105
}
else
{
106
Serial.printf(
"Error opening '%s' with '%s'\r\n"
,
filename
, FILE_APPEND);
107
}
108
109
Serial.printf(
"Opening '%s' with '%s'\r\n"
,
filename
, FILE_READ);
110
dataFile
= SD.open(
filename
, FILE_READ);
111
if
(
dataFile
) {
112
Serial.printf(
"Successfully opened '%s' with '%s'\r\n"
,
filename
,
113
FILE_READ);
114
printFileInfo
(
dataFile
);
115
printFileContents
(
dataFile
);
116
Serial.printf(
"Closing '%s'\r\n"
,
filename
);
117
dataFile
.close();
118
Serial.printf(
"Closed '%s'\r\n"
,
filename
);
119
}
else
{
120
Serial.printf(
"Error opening '%s' with '%s'\r\n"
,
filename
, FILE_READ);
121
}
122
}
123
124
void
loop
() {}
125
126
void
printCardInfo
(
void
) {
127
Serial.printf(
"\r\n-----Card info START-----\r\n"
);
128
Serial.printf(
"sectors, sector size, total size: %zd * %zd = %llu\r\n"
,
129
SD.numSectors(), SD.sectorSize(), SD.cardSize());
130
String sdcard_type_t_strings[] = {
"CARD_NONE"
,
"CARD_MMC"
,
"CARD_SD"
,
131
"CARD_SDHC"
,
"CARD_UNKNOWN"
};
132
Serial.printf(
"card type: (%d) %s\r\n"
, SD.cardType(),
133
sdcard_type_t_strings[SD.cardType()]);
134
Serial.printf(
"bytes used / total: %llu / %llu\r\n"
, SD.usedBytes(),
135
SD.totalBytes());
136
Serial.printf(
"-----Card info END-----\r\n\r\n"
);
137
}
138
139
void
printFileInfo
(File f) {
140
Serial.printf(
"\r\n-----File info START-----\r\n"
);
141
Serial.printf(
"available: %d\r\n"
, f.available());
142
Serial.printf(
"timeout: %lu\r\n"
, f.getTimeout());
143
Serial.printf(
"name: %s\r\n"
, f.name());
144
Serial.printf(
"path: %s\r\n"
, f.path());
145
// char c = f.peek();
146
// Serial.printf("peek: %c (0x%02x)\r\n", c, c);
147
Serial.printf(
"position: %zd\r\n"
, f.position());
148
Serial.printf(
"size: %zd\r\n"
, f.size());
149
Serial.printf(
"-----File info END-----\r\n\r\n"
);
150
}
151
152
void
printFileContents
(File f) {
153
Serial.printf(
"\r\n-----%s START-----\r\n"
,
filename
);
154
char
c =
'\n'
;
155
uint32_t line = 0;
156
do
{
157
if
(c ==
'\n'
) {
158
Serial.printf(
"[%d]:\t"
, line++);
159
}
160
c = f.read();
161
Serial.write(c);
162
}
while
(f.available());
163
Serial.printf(
"\r\n-----%s END-----\r\n"
,
filename
);
164
}
i
uint8_t i
Definition
example_i2c.cpp:12
dataFile
File dataFile
Definition
example_microsd.cpp:26
setup
void setup()
Definition
example_microsd.cpp:34
printCardInfo
void printCardInfo(void)
Definition
example_microsd.cpp:126
cardDetect
const uint8_t cardDetect
Definition
example_microsd.cpp:24
chipSelect
const uint8_t chipSelect
Definition
example_microsd.cpp:20
printFileContents
void printFileContents(File f)
Definition
example_microsd.cpp:152
filename
const String filename
Definition
example_microsd.cpp:28
printFileInfo
void printFileInfo(File f)
Definition
example_microsd.cpp:139
loop
void loop()
Definition
example_microsd.cpp:124
embedded
esp32
src
examples
example_microsd.cpp
Generated by
1.9.8