diff --git a/FtpServer.cpp b/FtpServer.cpp index b7b8b27..e3390ef 100644 --- a/FtpServer.cpp +++ b/FtpServer.cpp @@ -100,6 +100,22 @@ void FtpServer::begin( const char * _user, const char * _pass, const char * _wel iniVariables(); } +void FtpServer::end() +{ + if(client.connected()) { + disconnectClient(); + } + + ftpServer.end(); + dataServer.end(); + DEBUG_PRINTLN(F("Stop server!")); + + cmdStage = FTP_Init; +} +void FtpServer::setLocalIp(IPAddress localIp) +{ + this->localIp = localIp; +} void FtpServer::credentials( const char * _user, const char * _pass ) { if( strlen( _user ) > 0 && strlen( _user ) < FTP_CRED_SIZE ) @@ -432,8 +448,8 @@ bool FtpServer::processCommand() { data.stop(); dataServer.begin(); - if((((uint32_t) NET_CLASS.localIP()) & ((uint32_t) NET_CLASS.subnetMask())) == - (((uint32_t) client.remoteIP()) & ((uint32_t) NET_CLASS.subnetMask()))) { + if (((((uint32_t) NET_CLASS.localIP()) & ((uint32_t) NET_CLASS.subnetMask())) == + (((uint32_t) client.remoteIP()) & ((uint32_t) NET_CLASS.subnetMask()))) && (uint32_t)localIp <= 0) { dataIp = NET_CLASS.localIP(); } else { dataIp = localIp; diff --git a/FtpServer.h b/FtpServer.h index 8054bf2..f0d4da6 100644 --- a/FtpServer.h +++ b/FtpServer.h @@ -260,8 +260,14 @@ #define FTP_FILE_WRITE_APPEND "a+" #define FTP_FILE_WRITE_CREATE "w+" #else +#if ESP_ARDUINO_VERSION_MAJOR >= 2 + #include "FS.h" + #include "LittleFS.h" + #define STORAGE_MANAGER LittleFS +#else #include "LITTLEFS.h" #define STORAGE_MANAGER LITTLEFS +#endif #define FTP_FILE File #define FTP_DIR File @@ -453,6 +459,8 @@ class FtpServer FtpServer( uint16_t _cmdPort = FTP_CMD_PORT, uint16_t _pasvPort = FTP_DATA_PORT_PASV ); void begin( const char * _user = FTP_USER, const char * _pass = FTP_PASS, const char * welcomeMessage = "Welcome to Simply FTP server" ); + void end(); + void setLocalIp(IPAddress localIp); void credentials( const char * _user, const char * _pass ); uint8_t handleFTP(); diff --git a/FtpServerKey.h b/FtpServerKey.h index fef5076..660be38 100644 --- a/FtpServerKey.h +++ b/FtpServerKey.h @@ -18,7 +18,7 @@ #define FTP_SERVER_CONFIG_H // Uncomment to enable printing out nice debug messages. -//#define FTP_SERVER_DEBUG +// #define FTP_SERVER_DEBUG // Define where debug output will be printed. #define DEBUG_PRINTER Serial @@ -35,6 +35,7 @@ #define NETWORK_ESP8266_ASYNC (0) #define NETWORK_ESP8266 (1) +// && (defined(ESP8266) && ARDUINO_ESP8266_MAJOR<3) #define NETWORK_ESP8266_242 (6) #define NETWORK_W5100 (2) #define NETWORK_ENC28J60 (3) @@ -49,7 +50,7 @@ #endif #ifndef DEFAULT_FTP_SERVER_NETWORK_TYPE_ESP32 #define DEFAULT_FTP_SERVER_NETWORK_TYPE_ESP32 NETWORK_ESP32 - #define DEFAULT_STORAGE_TYPE_ESP32 STORAGE_SPIFFS + #define DEFAULT_STORAGE_TYPE_ESP32 STORAGE_LITTLEFS #endif #ifndef DEFAULT_FTP_SERVER_NETWORK_TYPE_ARDUINO #define DEFAULT_FTP_SERVER_NETWORK_TYPE_ARDUINO NETWORK_W5100 diff --git a/README.md b/README.md index 1a8130c..6e3ea5f 100644 --- a/README.md +++ b/README.md @@ -3,12 +3,15 @@ [Instruction on FTP server on esp8266 and esp32](https://www.mischianti.org/2020/02/08/ftp-server-on-esp8266-and-esp32) [Simple FTP Server library now with support for Wio Terminal and SD](https://www.mischianti.org/2021/07/01/simple-ftp-server-library-now-with-support-for-wio-terminal-and-sd/) -Simple FTP Server for +#### Simple FTP Server for - esp8266 (Flash: SPIFFs, LittleFS. SD: SD, SdFat 2) - - esp32 (SPIFFS, LITTLEFS, FFAT, SdFat) - - Arduino (SD with 8.3 file format, SdFat 2) + - esp32 (SPIFFS, LITTLEFS, FFAT, SD: SD, SdFat) + - Arduino (SD with 8.3 file format, SD: SD, SdFat 2) - Wio Terminal (SdFat 2, and native FAT) +#### Changelog +- 2022-02-01 Add workaround to start FTP server before connection, add end and setLocalIP method. +

When I develop a new solution I'd like to divide the application in layer, and so I'd like focus my attention in only one aspect at time.

diff --git a/examples/FTPServer_esp8266_esp32_LittleFS/FTPServer_esp8266_esp32_LittleFS.ino b/examples/FTPServer_esp8266_esp32_LittleFS/FTPServer_esp8266_esp32_LittleFS.ino new file mode 100644 index 0000000..0d82e76 --- /dev/null +++ b/examples/FTPServer_esp8266_esp32_LittleFS/FTPServer_esp8266_esp32_LittleFS.ino @@ -0,0 +1,110 @@ +/* + * FtpServer esp8266 and esp32 with LittleFS + * + * AUTHOR: Renzo Mischianti + * + * https://www.mischianti.org/2020/02/08/ftp-server-on-esp8266-and-esp32 + * + */ + +#ifdef ESP8266 +#include +#include +#elif defined ESP32 +#include +#include +#include +#endif + +#include + +const char* ssid = ""; +const char* password = ""; + + +FtpServer ftpSrv; //set #define FTP_DEBUG in ESP8266FtpServer.h to see ftp verbose on serial + +void _callback(FtpOperation ftpOperation, unsigned int freeSpace, unsigned int totalSpace){ + switch (ftpOperation) { + case FTP_CONNECT: + Serial.println(F("FTP: Connected!")); + break; + case FTP_DISCONNECT: + Serial.println(F("FTP: Disconnected!")); + break; + case FTP_FREE_SPACE_CHANGE: + Serial.printf("FTP: Free space change, free %u of %u!\n", freeSpace, totalSpace); + break; + default: + break; + } +}; +void _transferCallback(FtpTransferOperation ftpOperation, const char* name, unsigned int transferredSize){ + switch (ftpOperation) { + case FTP_UPLOAD_START: + Serial.println(F("FTP: Upload start!")); + break; + case FTP_UPLOAD: + Serial.printf("FTP: Upload of file %s byte %u\n", name, transferredSize); + break; + case FTP_TRANSFER_STOP: + Serial.println(F("FTP: Finish transfer!")); + break; + case FTP_TRANSFER_ERROR: + Serial.println(F("FTP: Transfer error!")); + break; + default: + break; + } + + /* FTP_UPLOAD_START = 0, + * FTP_UPLOAD = 1, + * + * FTP_DOWNLOAD_START = 2, + * FTP_DOWNLOAD = 3, + * + * FTP_TRANSFER_STOP = 4, + * FTP_DOWNLOAD_STOP = 4, + * FTP_UPLOAD_STOP = 4, + * + * FTP_TRANSFER_ERROR = 5, + * FTP_DOWNLOAD_ERROR = 5, + * FTP_UPLOAD_ERROR = 5 + */ +}; + +void setup(void){ + Serial.begin(115200); + WiFi.begin(ssid, password); + Serial.println(""); + + // Wait for connection + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + Serial.println(""); + Serial.print("Connected to "); + Serial.println(ssid); + Serial.print("IP address: "); + Serial.println(WiFi.localIP()); + + + /////FTP Setup, ensure SPIFFS is started before ftp; ///////// + + /////FTP Setup, ensure SPIFFS is started before ftp; ///////// +#ifdef ESP32 //esp32 we send true to format spiffs if cannot mount + if (LittleFS.begin(true)) { +#elif defined ESP8266 + if (LittleFS.begin()) { +#endif + ftpSrv.setCallback(_callback); + ftpSrv.setTransferCallback(_transferCallback); + + Serial.println("LittleFS opened!"); + ftpSrv.begin("user","password"); //username, password for ftp. (default 21, 50009 for PASV) + } +} +void loop(void){ + ftpSrv.handleFTP(); //make sure in loop you call handleFTP()!! +} diff --git a/examples/FTPServer_esp8266_esp32/FTPServer_esp8266_esp32.ino b/examples/FTPServer_esp8266_esp32_SPIFFS/FTPServer_esp8266_esp32_SPIFFS.ino similarity index 100% rename from examples/FTPServer_esp8266_esp32/FTPServer_esp8266_esp32.ino rename to examples/FTPServer_esp8266_esp32_SPIFFS/FTPServer_esp8266_esp32_SPIFFS.ino diff --git a/keywords.txt b/keywords.txt index e759b08..5cc521a 100644 --- a/keywords.txt +++ b/keywords.txt @@ -9,6 +9,8 @@ SimpleFtpServer KEYWORD1 ####################################### begin KEYWORD2 +end KEYWORD2 +setLocalIp KEYWORD2 credentials KEYWORD2 setCallback KEYWORD2 setTransferCallback KEYWORD2 diff --git a/library.properties b/library.properties index 4403084..8ab1e60 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=SimpleFTPServer -version=1.0.1 +version=1.1.0 author=Renzo Mischianti maintainer=Renzo Mischianti sentence=Simple FTP server for esp8266, esp32 and Arduino