This repository has been archived by the owner on Jan 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
FTPClient_DownloadFile.ino
152 lines (102 loc) · 3.54 KB
/
FTPClient_DownloadFile.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/******************************************************************************
FTPClient_DownloadFile.ino
FTP Client for Generic boards using SD, FS, etc.
Based on and modified from
1) esp32_ftpclient Library https://github.com/ldab/ESP32_FTPClient
Built by Khoi Hoang https://github.com/khoih-prog/FTPClient_Generic
******************************************************************************/
#include "Arduino.h"
#include "defines.h"
#include <FTPClient_Generic.h>
// To use `true` with the following PASV mode asnswer from server, such as `VSFTP`
// 227 Entering Passive Mode (192,168,2,112,157,218)
// Using `false` with old style PASV answer, such as `FTP_Server_Teensy41` library
// 227 Entering Passive Mode (4043483328, port 55600)
#define USING_VSFTP_SERVER true
#if USING_VSFTP_SERVER
// Change according to your FTP server
char ftp_server[] = "192.168.2.112";
char ftp_user[] = "ftp_test";
char ftp_pass[] = "ftp_test";
char dirName[] = "/home/ftp_test";
char newDirName[] = "/home/ftp_test/NewDir";
#else
// Change according to your FTP server
char ftp_server[] = "192.168.2.241";
char ftp_user[] = "teensy4x";
char ftp_pass[] = "ftp_test";
char dirName[] = "/";
char newDirName[] = "/NewDir";
#endif
// FTPClient_Generic(char* _serverAdress, char* _userName, char* _passWord, uint16_t _timeout = 10000);
FTPClient_Generic ftp (ftp_server, ftp_user, ftp_pass, 60000);
char fileName[] = "helloworld.txt";
void setup()
{
Serial.begin( 115200 );
while (!Serial && millis() < 5000);
delay(500);
Serial.print(F("\nStarting FTPClient_DownloadFile on "));
Serial.print(BOARD_NAME);
Serial.print(F(" with "));
Serial.println(SHIELD_TYPE);
Serial.println(FTPCLIENT_GENERIC_VERSION);
WiFi.begin( WIFI_SSID, WIFI_PASS );
Serial.print("Connecting WiFi, SSID = ");
Serial.println(WIFI_SSID);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.print("\nIP address: ");
Serial.println(WiFi.localIP());
#if (ESP32)
Serial.print("Max Free Heap: ");
Serial.println(ESP.getMaxAllocHeap());
#endif
ftp.OpenConnection();
//Change directory
ftp.ChangeWorkDir(dirName);
Serial.println("Creating new file helloworld.txt");
// Create a new file to use as the download example below:
ftp.InitFile(COMMAND_XFER_TYPE_ASCII);
ftp.NewFile(fileName);
String textContent = String("Hi, I'm a new ASCII file created @ millis = ") + millis();
ftp.Write(textContent.c_str());
ftp.CloseFile();
////////////////////////////////////////
ftp.InitFile(COMMAND_XFER_TYPE_ASCII);
ftp.AppendFile(fileName);
textContent = String("\nAdded text @ millis = ") + millis();
ftp.Write(textContent.c_str());
ftp.CloseFile();
////////////////////////////////////////
//Download the text file or read it
String response = "";
ftp.InitFile(COMMAND_XFER_TYPE_ASCII);
ftp.DownloadString("helloworld.txt", response);
Serial.println("The file content is: " + response);
// Get the file size
String list[128];
// Get the directory content in order to allocate buffer
// my server response => type=file;modify=20190101000010;size=18; helloworld.txt
ftp.InitFile(COMMAND_XFER_TYPE_ASCII);
ftp.ContentListWithListCommand("", list);
for (uint16_t i = 0; i < sizeof(list); i++)
{
if (list[i].length() > 0)
{
list[i].toLowerCase();
// Print the directory details
Serial.println(list[i]);
}
else
break;
}
Serial.println("CloseConnection");
ftp.CloseConnection();
}
void loop()
{
}