Skip to content

Commit

Permalink
Add UTF8 support and file length basic control. Issue xreef#9
Browse files Browse the repository at this point in the history
  • Loading branch information
xreef committed May 17, 2022
1 parent e8123a5 commit 0fc8f03
Show file tree
Hide file tree
Showing 12 changed files with 1,617 additions and 0 deletions.
62 changes: 62 additions & 0 deletions examples/Arduino_Ethernet/Arduino_Ethernet.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* FtpServer Arduino with Ethernet library and w5100 shield
*
* AUTHOR: Renzo Mischianti
*
* https://www.mischianti.org/2020/02/08/ftp-server-on-esp8266-and-esp32
*
*/

#include <SPI.h>
#include <Ethernet.h>
#include "SD.h"

#include <SimpleFtpServer.h>

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xE1 };

// Set the static IP address to use if the DHCP fails to assign
byte macAddr[] = {0x5e, 0xa4, 0x18, 0xf0, 0x8a, 0xf2};
IPAddress arduinoIP(192, 168, 1, 177);
IPAddress dnsIP(192, 168, 1, 1);
IPAddress gatewayIP(192, 168, 1, 1);
IPAddress subnetIP(255, 255, 255, 0);

FtpServer ftpSrv;

void setup(void){
Serial.begin(115200);
delay(2000);
// If other chips are connected to SPI bus, set to high the pin connected
// to their CS before initializing Flash memory
pinMode( 4, OUTPUT );
digitalWrite( 4, HIGH );
pinMode( 10, OUTPUT );
digitalWrite( 10, HIGH );

Serial.print("Starting SD.");
while (!SD.begin(4)) {
Serial.print(".");
}
Serial.println("finish!");

// start the Ethernet connection:
Serial.print("Starting ethernet.");
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
Ethernet.begin(macAddr, arduinoIP, dnsIP, gatewayIP, subnetIP);
}else{
Serial.println("ok to configure Ethernet using DHCP");
}

Serial.print("IP address ");
Serial.println(Ethernet.localIP());

Serial.println("SPIFFS opened!");
ftpSrv.begin("esp8266","esp8266"); //username, password for ftp.
}
void loop(void){
ftpSrv.handleFTP(); //make sure in loop you call handleFTP()!!
}
133 changes: 133 additions & 0 deletions examples/Arduino_Ethernet_SdFat2/Arduino_Ethernet_SdFat2.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
* FtpServer Arduino with Ethernet library and w5100 shield
* With SdFat version > 2 (full name and more size)
*
* #ifndef DEFAULT_FTP_SERVER_NETWORK_TYPE_ARDUINO
* #define DEFAULT_FTP_SERVER_NETWORK_TYPE_ARDUINO NETWORK_W5100
* #define DEFAULT_STORAGE_TYPE_ARDUINO STORAGE_SDFAT2
* #endif
*
* AUTHOR: Renzo Mischianti
*
* https://www.mischianti.org/2020/02/08/ftp-server-on-esp8266-and-esp32
*
*/

#include <SdFat.h>
#include <sdios.h>
#include <FtpServer.h>
#include <FreeStack.h>

// Define Chip Select for your SD card according to hardware
// #define CS_SDCARD 4 // SD card reader of Ehernet shield
#define CS_SDCARD 4 // Chip Select for SD card reader on Due

// Define Reset pin for W5200 or W5500
// set to -1 for other ethernet chip or if Arduino reset board is used
#define W5x00_RESET -1
//#define W5x00_RESET 8 // on Due
// #define W5x00_RESET 3 // on MKR


// Object for File system
SdFat sd;

// Object for FtpServer
// Command port and Data port in passive mode can be defined here
// FtpServer ftpSrv( 221, 25000 );
// FtpServer ftpSrv( 421 ); // Default data port in passive mode is 55600
FtpServer ftpSrv; // Default command port is 21 ( !! without parenthesis !! )

// Mac address of ethernet adapter
// byte mac[] = { 0x90, 0xa2, 0xda, 0x00, 0x00, 0x00 };
byte mac[] = { 0x00, 0xaa, 0xbb, 0xcc, 0xde, 0xef };

// IP address of FTP server
// if set to 0, use DHCP for the routeur to assign IP
// IPAddress serverIp( 192, 168, 1, 40 );
IPAddress serverIp( 0, 0, 0, 0 );

// External IP address of FTP server
// In passive mode, when accessing the serveur from outside his subnet, it can be
// necessary with some clients to reply them with the server's external ip address
// IPAddress externalIp( 192, 168, 1, 2 );

ArduinoOutStream cout( Serial );


void setup()
{
Serial.begin( 115200 );
cout << F("=== Test of FTP Server with SdFat ") << SD_FAT_VERSION << F(" file system ===") << endl;

// If other chips are connected to SPI bus, set to high the pin connected
// to their CS before initializing Flash memory
pinMode( 4, OUTPUT );
digitalWrite( 4, HIGH );
pinMode( 10, OUTPUT );
digitalWrite( 10, HIGH );

// Mount the SD card memory
cout << F("Mount the SD card memory... ");
if( ! sd.begin( CS_SDCARD, SD_SCK_MHZ( 50 )))
{
cout << F("Unable to mount SD card") << endl;
while( true ) ;
}
pinMode( CS_SDCARD, OUTPUT );
digitalWrite( CS_SDCARD, HIGH );
cout << F("ok") << endl;

// Show capacity and free space of SD card
cout << F("Capacity of card: ") << long( sd.card()->sectorCount() >> 1 )
<< F(" kBytes") << endl;
cout << F("Free space on card: ")
<< long( sd.vol()->freeClusterCount() * sd.vol()->sectorsPerCluster() >> 1 )
<< F(" kBytes") << endl;

// Send reset to Ethernet module
if( W5x00_RESET > -1 )
{
pinMode( W5x00_RESET, OUTPUT );
digitalWrite( W5x00_RESET, LOW );
delay( 200 );
digitalWrite( W5x00_RESET, HIGH );
delay( 200 );
}

// Initialize the network
cout << F("Initialize ethernet module ... ");
if( serverIp[0] != 0 )
Ethernet.begin( mac, serverIp );
else if( Ethernet.begin( mac ) == 0 )
{
cout << F("failed!") << endl;
while( true ) ;
}
uint16_t wizModule[] = { 0, 5100, 5200, 5500 };
cout << F("W") << wizModule[ Ethernet.hardwareStatus()] << F(" ok") << endl;
serverIp = Ethernet.localIP();
cout << F("IP address of server: ")
<< int( serverIp[0]) << "." << int( serverIp[1]) << "."
<< int( serverIp[2]) << "." << int( serverIp[3]) << endl;

// Initialize the FTP server
ftpSrv.begin("user","password");
// ftpSrv.init( externalIp );
// ftpSrv.init( IPAddress( 11, 22, 33, 44 ));

// Default username and password are set to 'arduino' and 'test'
// but can then be changed by calling ftpSrv.credentials()
// ftpSrv.credentials( "myname", "123" );

cout << F("Free stack: ") << FreeStack() << endl;

cout << "Viaaa!";
}

void loop()
{
ftpSrv.handleFTP();

// more processes...
}
96 changes: 96 additions & 0 deletions examples/Arduino_esp32_SD/Arduino_esp32_SD.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* FtpServer esp8266 and esp32 with SD
*
* AUTHOR: Renzo Mischianti
*
* https://www.mischianti.org/2020/02/08/ftp-server-on-esp8266-and-esp32
*
*/

#include <WiFi.h>
#include "SD.h"

#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){
Serial.print(">>>>>>>>>>>>>>> _callback " );
Serial.print(ftpOperation);
/* FTP_CONNECT,
* FTP_DISCONNECT,
* FTP_FREE_SPACE_CHANGE
*/
Serial.print(" ");
Serial.print(freeSpace);
Serial.print(" ");
Serial.println(totalSpace);

// freeSpace : totalSpace = x : 360

if (ftpOperation == FTP_CONNECT) Serial.println(F("CONNECTED"));
if (ftpOperation == FTP_DISCONNECT) Serial.println(F("DISCONNECTED"));
};
void _transferCallback(FtpTransferOperation ftpOperation, const char* name, unsigned int transferredSize){
Serial.print(">>>>>>>>>>>>>>> _transferCallback " );
Serial.print(ftpOperation);
/* 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
*/
Serial.print(" ");
Serial.print(name);
Serial.print(" ");
Serial.println(transferredSize);
};

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; /////////
SPI.begin(14, 2, 15, 13); //SCK, MISO, MOSI,SS

if (SD.begin(13, SPI)) {
Serial.println("SD opened!");

ftpSrv.setCallback(_callback);
ftpSrv.setTransferCallback(_transferCallback);

ftpSrv.begin("esp8266","esp8266"); //username, password for ftp. (default 21, 50009 for PASV)
}
}
void loop(void){
ftpSrv.handleFTP(); //make sure in loop you call handleFTP()!!
// server.handleClient(); //example if running a webserver you still need to call .handleClient();

}
Loading

0 comments on commit 0fc8f03

Please sign in to comment.