|
| 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 | +} |
0 commit comments