Skip to content

Commit

Permalink
Minor fix to manage NAT and delayed start of Wifi xreef#8. Adding the…
Browse files Browse the repository at this point in the history
… end and

setLocalIP method
  • Loading branch information
xreef committed Feb 2, 2022
1 parent aef4713 commit 60eac8d
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 8 deletions.
20 changes: 18 additions & 2 deletions FtpServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down Expand Up @@ -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;
Expand Down
8 changes: 8 additions & 0 deletions FtpServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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();

Expand Down
5 changes: 3 additions & 2 deletions FtpServerKey.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand Down
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

<!-- wp:paragraph -->
<p>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. </p>
<!-- /wp:paragraph -->
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <ESP8266WiFi.h>
#include <LittleFS.h>
#elif defined ESP32
#include <WiFi.h>
#include <FS.h>
#include <LittleFS.h>
#endif

#include <SimpleFTPServer.h>

const char* ssid = "<YOUR-SSID>";
const char* password = "<YOUR-PASSWD>";


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()!!
}
2 changes: 2 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ SimpleFtpServer KEYWORD1
#######################################

begin KEYWORD2
end KEYWORD2
setLocalIp KEYWORD2
credentials KEYWORD2
setCallback KEYWORD2
setTransferCallback KEYWORD2
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=SimpleFTPServer
version=1.0.1
version=1.1.0
author=Renzo Mischianti <[email protected]>
maintainer=Renzo Mischianti <[email protected]>
sentence=Simple FTP server for esp8266, esp32 and Arduino
Expand Down

0 comments on commit 60eac8d

Please sign in to comment.