Skip to content

Commit 0fc8f03

Browse files
committed
Add UTF8 support and file length basic control. Issue #9
1 parent e8123a5 commit 0fc8f03

File tree

12 files changed

+1617
-0
lines changed

12 files changed

+1617
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* FtpServer Arduino with Ethernet library and w5100 shield
3+
*
4+
* AUTHOR: Renzo Mischianti
5+
*
6+
* https://www.mischianti.org/2020/02/08/ftp-server-on-esp8266-and-esp32
7+
*
8+
*/
9+
10+
#include <SPI.h>
11+
#include <Ethernet.h>
12+
#include "SD.h"
13+
14+
#include <SimpleFtpServer.h>
15+
16+
// Enter a MAC address for your controller below.
17+
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
18+
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xE1 };
19+
20+
// Set the static IP address to use if the DHCP fails to assign
21+
byte macAddr[] = {0x5e, 0xa4, 0x18, 0xf0, 0x8a, 0xf2};
22+
IPAddress arduinoIP(192, 168, 1, 177);
23+
IPAddress dnsIP(192, 168, 1, 1);
24+
IPAddress gatewayIP(192, 168, 1, 1);
25+
IPAddress subnetIP(255, 255, 255, 0);
26+
27+
FtpServer ftpSrv;
28+
29+
void setup(void){
30+
Serial.begin(115200);
31+
delay(2000);
32+
// If other chips are connected to SPI bus, set to high the pin connected
33+
// to their CS before initializing Flash memory
34+
pinMode( 4, OUTPUT );
35+
digitalWrite( 4, HIGH );
36+
pinMode( 10, OUTPUT );
37+
digitalWrite( 10, HIGH );
38+
39+
Serial.print("Starting SD.");
40+
while (!SD.begin(4)) {
41+
Serial.print(".");
42+
}
43+
Serial.println("finish!");
44+
45+
// start the Ethernet connection:
46+
Serial.print("Starting ethernet.");
47+
if (Ethernet.begin(mac) == 0) {
48+
Serial.println("Failed to configure Ethernet using DHCP");
49+
Ethernet.begin(macAddr, arduinoIP, dnsIP, gatewayIP, subnetIP);
50+
}else{
51+
Serial.println("ok to configure Ethernet using DHCP");
52+
}
53+
54+
Serial.print("IP address ");
55+
Serial.println(Ethernet.localIP());
56+
57+
Serial.println("SPIFFS opened!");
58+
ftpSrv.begin("esp8266","esp8266"); //username, password for ftp.
59+
}
60+
void loop(void){
61+
ftpSrv.handleFTP(); //make sure in loop you call handleFTP()!!
62+
}
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/*
2+
* FtpServer Arduino with Ethernet library and w5100 shield
3+
* With SdFat version > 2 (full name and more size)
4+
*
5+
* #ifndef DEFAULT_FTP_SERVER_NETWORK_TYPE_ARDUINO
6+
* #define DEFAULT_FTP_SERVER_NETWORK_TYPE_ARDUINO NETWORK_W5100
7+
* #define DEFAULT_STORAGE_TYPE_ARDUINO STORAGE_SDFAT2
8+
* #endif
9+
*
10+
* AUTHOR: Renzo Mischianti
11+
*
12+
* https://www.mischianti.org/2020/02/08/ftp-server-on-esp8266-and-esp32
13+
*
14+
*/
15+
16+
#include <SdFat.h>
17+
#include <sdios.h>
18+
#include <FtpServer.h>
19+
#include <FreeStack.h>
20+
21+
// Define Chip Select for your SD card according to hardware
22+
// #define CS_SDCARD 4 // SD card reader of Ehernet shield
23+
#define CS_SDCARD 4 // Chip Select for SD card reader on Due
24+
25+
// Define Reset pin for W5200 or W5500
26+
// set to -1 for other ethernet chip or if Arduino reset board is used
27+
#define W5x00_RESET -1
28+
//#define W5x00_RESET 8 // on Due
29+
// #define W5x00_RESET 3 // on MKR
30+
31+
32+
// Object for File system
33+
SdFat sd;
34+
35+
// Object for FtpServer
36+
// Command port and Data port in passive mode can be defined here
37+
// FtpServer ftpSrv( 221, 25000 );
38+
// FtpServer ftpSrv( 421 ); // Default data port in passive mode is 55600
39+
FtpServer ftpSrv; // Default command port is 21 ( !! without parenthesis !! )
40+
41+
// Mac address of ethernet adapter
42+
// byte mac[] = { 0x90, 0xa2, 0xda, 0x00, 0x00, 0x00 };
43+
byte mac[] = { 0x00, 0xaa, 0xbb, 0xcc, 0xde, 0xef };
44+
45+
// IP address of FTP server
46+
// if set to 0, use DHCP for the routeur to assign IP
47+
// IPAddress serverIp( 192, 168, 1, 40 );
48+
IPAddress serverIp( 0, 0, 0, 0 );
49+
50+
// External IP address of FTP server
51+
// In passive mode, when accessing the serveur from outside his subnet, it can be
52+
// necessary with some clients to reply them with the server's external ip address
53+
// IPAddress externalIp( 192, 168, 1, 2 );
54+
55+
ArduinoOutStream cout( Serial );
56+
57+
58+
void setup()
59+
{
60+
Serial.begin( 115200 );
61+
cout << F("=== Test of FTP Server with SdFat ") << SD_FAT_VERSION << F(" file system ===") << endl;
62+
63+
// If other chips are connected to SPI bus, set to high the pin connected
64+
// to their CS before initializing Flash memory
65+
pinMode( 4, OUTPUT );
66+
digitalWrite( 4, HIGH );
67+
pinMode( 10, OUTPUT );
68+
digitalWrite( 10, HIGH );
69+
70+
// Mount the SD card memory
71+
cout << F("Mount the SD card memory... ");
72+
if( ! sd.begin( CS_SDCARD, SD_SCK_MHZ( 50 )))
73+
{
74+
cout << F("Unable to mount SD card") << endl;
75+
while( true ) ;
76+
}
77+
pinMode( CS_SDCARD, OUTPUT );
78+
digitalWrite( CS_SDCARD, HIGH );
79+
cout << F("ok") << endl;
80+
81+
// Show capacity and free space of SD card
82+
cout << F("Capacity of card: ") << long( sd.card()->sectorCount() >> 1 )
83+
<< F(" kBytes") << endl;
84+
cout << F("Free space on card: ")
85+
<< long( sd.vol()->freeClusterCount() * sd.vol()->sectorsPerCluster() >> 1 )
86+
<< F(" kBytes") << endl;
87+
88+
// Send reset to Ethernet module
89+
if( W5x00_RESET > -1 )
90+
{
91+
pinMode( W5x00_RESET, OUTPUT );
92+
digitalWrite( W5x00_RESET, LOW );
93+
delay( 200 );
94+
digitalWrite( W5x00_RESET, HIGH );
95+
delay( 200 );
96+
}
97+
98+
// Initialize the network
99+
cout << F("Initialize ethernet module ... ");
100+
if( serverIp[0] != 0 )
101+
Ethernet.begin( mac, serverIp );
102+
else if( Ethernet.begin( mac ) == 0 )
103+
{
104+
cout << F("failed!") << endl;
105+
while( true ) ;
106+
}
107+
uint16_t wizModule[] = { 0, 5100, 5200, 5500 };
108+
cout << F("W") << wizModule[ Ethernet.hardwareStatus()] << F(" ok") << endl;
109+
serverIp = Ethernet.localIP();
110+
cout << F("IP address of server: ")
111+
<< int( serverIp[0]) << "." << int( serverIp[1]) << "."
112+
<< int( serverIp[2]) << "." << int( serverIp[3]) << endl;
113+
114+
// Initialize the FTP server
115+
ftpSrv.begin("user","password");
116+
// ftpSrv.init( externalIp );
117+
// ftpSrv.init( IPAddress( 11, 22, 33, 44 ));
118+
119+
// Default username and password are set to 'arduino' and 'test'
120+
// but can then be changed by calling ftpSrv.credentials()
121+
// ftpSrv.credentials( "myname", "123" );
122+
123+
cout << F("Free stack: ") << FreeStack() << endl;
124+
125+
cout << "Viaaa!";
126+
}
127+
128+
void loop()
129+
{
130+
ftpSrv.handleFTP();
131+
132+
// more processes...
133+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* FtpServer esp8266 and esp32 with SD
3+
*
4+
* AUTHOR: Renzo Mischianti
5+
*
6+
* https://www.mischianti.org/2020/02/08/ftp-server-on-esp8266-and-esp32
7+
*
8+
*/
9+
10+
#include <WiFi.h>
11+
#include "SD.h"
12+
13+
#include <SimpleFTPServer.h>
14+
15+
const char* ssid = "<YOUR-SSID>";
16+
const char* password = "<YOUR-PASSWD>";
17+
18+
19+
FtpServer ftpSrv; //set #define FTP_DEBUG in ESP8266FtpServer.h to see ftp verbose on serial
20+
21+
void _callback(FtpOperation ftpOperation, unsigned int freeSpace, unsigned int totalSpace){
22+
Serial.print(">>>>>>>>>>>>>>> _callback " );
23+
Serial.print(ftpOperation);
24+
/* FTP_CONNECT,
25+
* FTP_DISCONNECT,
26+
* FTP_FREE_SPACE_CHANGE
27+
*/
28+
Serial.print(" ");
29+
Serial.print(freeSpace);
30+
Serial.print(" ");
31+
Serial.println(totalSpace);
32+
33+
// freeSpace : totalSpace = x : 360
34+
35+
if (ftpOperation == FTP_CONNECT) Serial.println(F("CONNECTED"));
36+
if (ftpOperation == FTP_DISCONNECT) Serial.println(F("DISCONNECTED"));
37+
};
38+
void _transferCallback(FtpTransferOperation ftpOperation, const char* name, unsigned int transferredSize){
39+
Serial.print(">>>>>>>>>>>>>>> _transferCallback " );
40+
Serial.print(ftpOperation);
41+
/* FTP_UPLOAD_START = 0,
42+
* FTP_UPLOAD = 1,
43+
*
44+
* FTP_DOWNLOAD_START = 2,
45+
* FTP_DOWNLOAD = 3,
46+
*
47+
* FTP_TRANSFER_STOP = 4,
48+
* FTP_DOWNLOAD_STOP = 4,
49+
* FTP_UPLOAD_STOP = 4,
50+
*
51+
* FTP_TRANSFER_ERROR = 5,
52+
* FTP_DOWNLOAD_ERROR = 5,
53+
* FTP_UPLOAD_ERROR = 5
54+
*/
55+
Serial.print(" ");
56+
Serial.print(name);
57+
Serial.print(" ");
58+
Serial.println(transferredSize);
59+
};
60+
61+
void setup(void){
62+
Serial.begin(115200);
63+
WiFi.begin(ssid, password);
64+
Serial.println("");
65+
66+
// Wait for connection
67+
while (WiFi.status() != WL_CONNECTED) {
68+
delay(500);
69+
Serial.print(".");
70+
}
71+
Serial.println("");
72+
Serial.print("Connected to ");
73+
Serial.println(ssid);
74+
Serial.print("IP address: ");
75+
Serial.println(WiFi.localIP());
76+
77+
78+
/////FTP Setup, ensure SPIFFS is started before ftp; /////////
79+
80+
/////FTP Setup, ensure SPIFFS is started before ftp; /////////
81+
SPI.begin(14, 2, 15, 13); //SCK, MISO, MOSI,SS
82+
83+
if (SD.begin(13, SPI)) {
84+
Serial.println("SD opened!");
85+
86+
ftpSrv.setCallback(_callback);
87+
ftpSrv.setTransferCallback(_transferCallback);
88+
89+
ftpSrv.begin("esp8266","esp8266"); //username, password for ftp. (default 21, 50009 for PASV)
90+
}
91+
}
92+
void loop(void){
93+
ftpSrv.handleFTP(); //make sure in loop you call handleFTP()!!
94+
// server.handleClient(); //example if running a webserver you still need to call .handleClient();
95+
96+
}

0 commit comments

Comments
 (0)