SD 04: Read file
This example shows how to turn on the SD card. Append data at the end of the SD file and then how to read it.

Required Materials

1 x Waspmote 1 x Battery 1 x SD card 1 x MiniUSB wire

Notes

- Remember that only 8.3 SHORT FILE NAME are valid for files and directories - Make sure Waspmote is switched off before inserting or removing the SD card. Otherwise, the SD card could be damaged. - Waspmote must not be switched off or reseted while there are ongoing read or write operations in the SD card. Otherwise, the SD card could be damaged and data could be lost. If you suspect that there may be some ongoing SD operations, wait a while until they are completed. - This example can be executed in Waspmote v12 and Waspmote v15

Code

1
/*
2
* ------ [SD_04] - Read data from SD file --------
3
*
4
* Explanation: Turn on the SD card. Append data at the end of
5
* the SD file and then show how to read it.
6
*
7
* Copyright (C) 2016 Libelium Comunicaciones Distribuidas S.L.
8
* http://www.libelium.com
9
*
10
* This program is free software: you can redistribute it and/or modify
11
* it under the terms of the GNU General Public License as published by
12
* the Free Software Foundation, either version 3 of the License, or
13
* (at your option) any later version.
14
*
15
* This program is distributed in the hope that it will be useful,
16
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
* GNU General Public License for more details.
19
*
20
* You should have received a copy of the GNU General Public License
21
* along with this program. If not, see .
22
*
23
* Version: 3.0
24
* Design: David Gascón
25
* Implementation: Yuri Carmona
26
*/
27
28
// define file name: MUST be 8.3 SHORT FILE NAME
29
char filename[]="FILE4.TXT";
30
31
// array in order to read data
32
char output[101];
33
34
// define variable
35
uint8_t sd_answer;
36
37
38
void setup()
39
{
40
// open USB port
41
USB.ON();
42
USB.println(F("SD_4 example"));
43
44
// Set SD ON
45
SD.ON();
46
47
// Delete file
48
sd_answer = SD.del(filename);
49
50
if( sd_answer == 1 )
51
{
52
USB.println(F("file deleted"));
53
}
54
else
55
{
56
USB.println(F("file NOT deleted"));
57
}
58
59
// Create file
60
sd_answer = SD.create(filename);
61
62
if( sd_answer == 1 )
63
{
64
USB.println(F("file created"));
65
}
66
else
67
{
68
USB.println(F("file NOT created"));
69
}
70
71
// Append data to file
72
SD.appendln(filename,"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
73
SD.appendln(filename,"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
74
SD.appendln(filename,"cccccccccccccccccccccccccccccc");
75
SD.appendln(filename,"dddddddddddddddddddddddddddddd");
76
SD.appendln(filename,"eeeeeeeeeeeeeeeeeeeeeeeeeeeeee");
77
SD.appendln(filename,"ffffffffffffffffffffffffffffff");
78
SD.appendln(filename,"gggggggggggggggggggggggggggggg");
79
SD.appendln(filename,"hhhhhhhhhhhhhhhhhhhhhhhhhhhhhh");
80
SD.appendln(filename,"iiiiiiiiiiiiiiiiiiiiiiiiiiiiii");
81
82
USB.println(F("\n-------------------"));
83
USB.print(F("SHOW THE FILE CONTENTS"));
84
SD.showFile(filename);
85
86
delay(3000);
87
88
}
89
90
91
void loop()
92
{
93
94
// 1 - SD.cat
95
// Read file bytes using different values of
96
// offset (first position to be read) and
97
// scope (number of bytes to be read)
98
USB.println(F("\n\n-----------------------------"));
99
USB.println(F("1 - Use of SD.cat function"));
100
USB.println(F("-----------------------------"));
101
USB.println(F("Read file in bytes (offset:0; scope:10):"));
102
SD.cat(filename,0,10);
103
USB.println(SD.buffer);
104
USB.println(F("-----------------------------"));
105
delay(1000);
106
107
USB.println(F("Read file in bytes (offset:25; scope:10):"));
108
SD.cat( filename, 25, 10);
109
USB.println( SD.buffer );
110
USB.println(F("-----------------------------"));
111
delay(1000);
112
113
USB.println(F("Read file in bytes (offset:0; scope:1000) [256 bytes max]:"));
114
SD.cat( filename, 0, 1000);
115
USB.println( SD.buffer);
116
USB.println(F("-----------------------------"));
117
delay(3000);
118
119
120
// 2 - SD.catBin
121
// Read file hexadecimal data using different values of
122
// offset (first position to be read) and
123
// scope (number of bytes to be read)
124
USB.println(F("\n\n-----------------------------"));
125
USB.println(F("2 - Use of SD.catBin function"));
126
USB.println(F("-----------------------------"));
127
USB.println(F("Read binary data (offset:0; scope:10):"));
128
SD.catBin(filename,0,10);
129
Utils.hex2str( SD.bufferBin, output, 10);
130
USB.println( output );
131
USB.println(F("-----------------------------"));
132
delay(1000);
133
134
USB.println(F("Read binary data (offset:25; scope:10):"));
135
SD.catBin(filename,25,10);
136
Utils.hex2str( SD.bufferBin, output, 10);
137
USB.println( output );
138
USB.println(F("-----------------------------"));
139
delay(1000);
140
141
USB.println(F("Read binary data (offset:0; scope:50):"));
142
SD.catBin( filename, 0, 50);
143
Utils.hex2str( SD.bufferBin, output, 50);
144
USB.println( output );
145
USB.println(F("-----------------------------"));
146
delay(3000);
147
148
149
// 3 - SD.catln
150
// Read file lines using different values of
151
// offset (first line to be read) and
152
// scope (number of lines to be read)
153
USB.println(F("\n\n-----------------------------"));
154
USB.println(F("3 - Use of SD.catln function"));
155
USB.println(F("-----------------------------"));
156
USB.println(F("Read file lines (offset:0; scope:1):"));
157
SD.catln(filename,0,1);
158
USB.println( SD.buffer );
159
USB.println(F("-----------------------------"));
160
delay(1000);
161
162
USB.println(F("Read file lines (offset:1; scope:1):"));
163
SD.catln(filename,1,1);
164
USB.println( SD.buffer );
165
USB.println(F("-----------------------------"));
166
delay(1000);
167
168
USB.println(F("Read file lines (offset:2; scope:1):"));
169
SD.catln(filename,2,1);
170
USB.println( SD.buffer );
171
USB.println(F("-----------------------------"));
172
delay(1000);
173
174
USB.println(F("Read file lines (offset:0; scope:5):"));
175
SD.catln(filename,0,5);
176
USB.println( SD.buffer );
177
USB.println(F("-----------------------------"));
178
delay(1000);
179
180
USB.println(F("Read file lines (offset:0; scope:25) [256 bytes max]:"));
181
SD.catln(filename,0,25);
182
USB.println( SD.buffer );
183
USB.println(F("-----------------------------"));
184
185
USB.println(F("*****************************"));
186
187
delay(10000);
188
189
}
190
191
Copied!

Output

1
H#
2
SD_4 example
3
file deleted
4
file created
5
-------------------
6
SHOW THE FILE CONTENTS
7
-------------------
8
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
9
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
10
cccccccccccccccccccccccccccccc
11
dddddddddddddddddddddddddddddd
12
eeeeeeeeeeeeeeeeeeeeeeeeeeeeee
13
ffffffffffffffffffffffffffffff
14
gggggggggggggggggggggggggggggg
15
hhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
16
iiiiiiiiiiiiiiiiiiiiiiiiiiiiii
17
-------------------
18
-----------------------------
19
1 - Use of SD.cat function
20
-----------------------------
21
Read file in bytes (offset:0; scope:10):
22
aaaaaaaaaa
23
-----------------------------
24
Read file in bytes (offset:25; scope:10):
25
aaaaa
26
bbbb
27
-----------------------------
28
Read file in bytes (offset:0; scope:1000) [256 bytes max]:
29
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
30
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
31
cccccccccccccccccccccccccccccc
32
dddddddddddddddddddddddddddddd
33
eeeeeeeeeeeeeeeeeeeeeeeeeeeeee
34
ffffffffffffffffffffffffffffff
35
gggggggggggggggggggggggggggggg
36
hhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
37
iiii>>
38
-----------------------------
39
-----------------------------
40
2 - Use of SD.catBin function
41
-----------------------------
42
Read binary data (offset:0; scope:10):
43
61616161616161616161
44
-----------------------------
45
Read binary data (offset:25; scope:10):
46
61616161610A62626262
47
-----------------------------
48
Read binary data (offset:0; scope:50):
49
6161616161616161616161616161616161616161616161616161616161610A62626262626262626262626262626262626262
50
-----------------------------
51
-----------------------------
52
3 - Use of SD.catln function
53
-----------------------------
54
Read file lines (offset:0; scope:1):
55
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
56
-----------------------------
57
Read file lines (offset:1; scope:1):
58
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
59
-----------------------------
60
Read file lines (offset:2; scope:1):
61
cccccccccccccccccccccccccccccc
62
-----------------------------
63
Read file lines (offset:0; scope:5):
64
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
65
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
66
cccccccccccccccccccccccccccccc
67
dddddddddddddddddddddddddddddd
68
eeeeeeeeeeeeeeeeeeeeeeeeeeeeee
69
-----------------------------
70
Read file lines (offset:0; scope:25) [256 bytes max]:
71
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
72
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
73
cccccccccccccccccccccccccccccc
74
dddddddddddddddddddddddddddddd
75
eeeeeeeeeeeeeeeeeeeeeeeeeeeeee
76
ffffffffffffffffffffffffffffff
77
gggggggggggggggggggggggggggggg
78
hhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
79
iiii>>
80
-----------------------------
81
*****************************
Copied!
Last modified 1yr ago