@@ -26,8 +26,10 @@ const char GPRS_APN[] = SECRET_GPRS_APN;
26
26
const char GPRS_LOGIN[] = SECRET_GPRS_LOGIN;
27
27
const char GPRS_PASSWORD[] = SECRET_GPRS_PASSWORD;
28
28
29
+ // this file must be present in the remote directory SECRET_FTP_REMOTE_DIR
30
+ const String c_downloadFileName = " downloadFile" ;
31
+
29
32
// initialize the library instance
30
- GSMFileSytem fileSystem;
31
33
GSMFTP ftp;
32
34
GPRS gprs;
33
35
GSM gsmAccess;
@@ -57,19 +59,22 @@ void setup() {
57
59
}
58
60
59
61
void loop () {
62
+ GSMFileSystemElem localFile;
63
+ GSMFTPElem remoteFile;
60
64
61
65
Serial.println (" Connect to FTP server." );
62
66
if (ftp.connect (SECRET_FTP_HOST, SECRET_FTP_USER, SECRET_FTP_PASSWORD, SECRET_FTP_PORT) == false ) {
63
67
Serial.println (" Failed to Connect to FTP server." );
68
+ ftp.printError ();
64
69
}
65
-
70
+
66
71
Serial.println (" Change of directory" );
67
72
if (ftp.cd (SECRET_FTP_REMOTE_DIR) == false ) {
68
73
Serial.println (" Failed to change of directory." );
69
74
}
70
-
75
+
71
76
Serial.print (" Free space " );
72
- Serial.println (fileSystem .freeSpace ());
77
+ Serial.println (FILESYSTEM .freeSpace ());
73
78
74
79
Serial.println (" Create remote directory : test" );
75
80
if (ftp.mkdir (" test" ) == false ) {
@@ -84,7 +89,7 @@ void loop() {
84
89
Serial.println (" Write a binary file in local memory" );
85
90
double valueWR = -12.5789876 ;
86
91
double valueRD = 0 ;
87
- if (fileSystem .write (" myFile" , &valueWR, sizeof (valueWR)) == false ) {
92
+ if (FILESYSTEM .write (" myFile" , &valueWR, sizeof (valueWR)) == false ) {
88
93
Serial.println (" Failed to write file" );
89
94
}
90
95
@@ -95,39 +100,39 @@ void loop() {
95
100
}
96
101
97
102
Serial.println (" Retreive the file from the server to local memory" );
98
- if (ftp.download (" myFileToServer " , " myFileToLocalMemory " ) == false ) {
103
+ if (ftp.download (" myFileToLocalMemory " , " myFileToServer " ) == false ) {
99
104
Serial.println (" Failed to download the file." );
100
105
ftp.printError ();
101
106
}
102
107
103
108
Serial.println (" Check that the original file is identical to the one that was received" );
104
- if (fileSystem .read (" myFileToLocalMemory" , &valueRD, sizeof (valueRD)) == false ) {
109
+ if (FILESYSTEM .read (" myFileToLocalMemory" , &valueRD, sizeof (valueRD)) == false ) {
105
110
Serial.println (" Failed to read file" );
106
111
}
107
112
else if (valueWR != valueRD) {
108
113
Serial.println (" Failed to read file, value is corrupted" );
109
114
}
110
115
111
116
Serial.print (" Free space " );
112
- Serial.println (fileSystem .freeSpace ());
117
+ Serial.println (FILESYSTEM .freeSpace ());
113
118
114
119
Serial.println (" Display local files" );
115
- if (fileSystem .ls (true ) == false ) {
120
+ if (FILESYSTEM .ls (localFile, true ) == false ) {
116
121
Serial.println (" Failed to display local files" );
117
122
}
118
123
119
124
Serial.println (" Remove local files" );
120
- for ( int i = 0 ; i < fileSystem. fileCount (); ++i ) {
121
- fileSystem. remove (fileSystem. file (i). name );
125
+ if (FILESYSTEM. remove (localFile) == false ) {
126
+ Serial. println ( " Failed to remove file" );
122
127
}
123
-
128
+
124
129
Serial.println (" Display local files" );
125
- if (fileSystem .ls (true ) == false ) {
130
+ if (FILESYSTEM .ls (localFile, true ) == false ) {
126
131
Serial.println (" Failed to display local files" );
127
132
}
128
133
129
134
Serial.println (" Display remote files" );
130
- if (ftp.ls (true ) == false ) {
135
+ if (ftp.ls (remoteFile, true ) == false ) {
131
136
Serial.println (" Failed to display files." );
132
137
}
133
138
@@ -140,16 +145,82 @@ void loop() {
140
145
}
141
146
142
147
Serial.println (" Display remote files" );
143
- if (ftp.ls (true ) == false ) {
148
+ if (ftp.ls (remoteFile, true ) == false ) {
149
+ Serial.println (" Failed to display files." );
150
+ }
151
+
152
+ // --- Test download/upload a large file with non blocking function ---
153
+
154
+ Serial.println ();
155
+ Serial.println (" Download a file with non blocking function" );
156
+ downloadFileNonBlocking (" downloadedFile" , c_downloadFileName);
157
+
158
+ Serial.println (" Display local files" );
159
+ if (FILESYSTEM.ls (localFile, true ) == false ) {
160
+ Serial.println (" Failed to display local files" );
161
+ }
162
+
163
+ Serial.println (" Upload a file with non blocking function" );
164
+ uploadFileNonBlocking (" downloadedFile" , " uploadFile" );
165
+
166
+ Serial.println (" Display remote files" );
167
+ if (ftp.ls (remoteFile, true ) == false ) {
144
168
Serial.println (" Failed to display files." );
145
169
}
146
170
171
+ Serial.println (" Remove local and remote files" );
172
+ if (FILESYSTEM.remove (" downloadedFile" ) == false ) {
173
+ Serial.println (" Failed to remove file" );
174
+ }
175
+ if (ftp.removeFile (" uploadFile" ) == false ) {
176
+ Serial.println (" Failed to remove files : myFileToServer." );
177
+ }
178
+
147
179
Serial.println (" Disconnect to FTP server" );
148
180
if (ftp.disconnect () == false ) {
149
181
Serial.println (" Failed to disconnect." );
150
182
}
151
-
183
+
152
184
for (;;)
153
185
;
154
186
}
155
187
188
+ // Example of non blocking download functions
189
+ void downloadFileNonBlocking (const String localFileName, const String remoteFileName) {
190
+
191
+ Serial.println (" Retreive the file from the server to local memory" );
192
+ // Start download
193
+ if (ftp.downloadStart (localFileName, remoteFileName) == false ) {
194
+ Serial.println (" Failed to start download." );
195
+ ftp.printError ();
196
+ }
197
+
198
+ // update download
199
+ while (ftp.downloadReady (localFileName, true ) == 0 )
200
+ {
201
+ // do some job
202
+ }
203
+ }
204
+
205
+ // Example of non blocking upload functions
206
+ void uploadFileNonBlocking (const String localFileName, const String remoteFileName) {
207
+
208
+ Serial.println (" Send the file to the server from local memory" );
209
+ if (ftp.uploadStart (localFileName, remoteFileName) == false ) {
210
+ Serial.println (" Failed to start upload." );
211
+ ftp.printError ();
212
+ }
213
+
214
+ int res = 0 ;
215
+ while (res == 0 ){
216
+ res = ftp.uploadReady ();
217
+ if (res == 1 ) {
218
+ Serial.println (" Upload finished." );
219
+ }
220
+ else if (res < 0 ) {
221
+ Serial.println (" Upload error." );
222
+ ftp.printError ();
223
+ }
224
+ // do some job
225
+ }
226
+ }
0 commit comments