diff options
Diffstat (limited to 'examples')
-rw-r--r-- | examples/FTPServerSample/FTPServerSample.ino | 50 | ||||
-rw-r--r-- | examples/LittleFSSample/LittleFSSample.ino | 125 | ||||
-rw-r--r-- | examples/SPIFFSSample/SPIFFSSample.ino | 115 |
3 files changed, 240 insertions, 50 deletions
diff --git a/examples/FTPServerSample/FTPServerSample.ino b/examples/FTPServerSample/FTPServerSample.ino deleted file mode 100644 index b8fa5f6..0000000 --- a/examples/FTPServerSample/FTPServerSample.ino +++ /dev/null @@ -1,50 +0,0 @@ -#ifdef ESP8266 -#include <ESP8266WiFi.h> -#elif defined ESP32 -#include <WiFi.h> -#include "SPIFFS.h" -#endif - -#include <ESP8266FtpServer.h> - -const char* ssid = "YOUR_SSID"; -const char* password = "YOUR_PASS"; - - -FtpServer ftpSrv; //set #define FTP_DEBUG in ESP8266FtpServer.h to see ftp verbose on serial - - -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 (SPIFFS.begin(true)) { -#elif defined ESP8266 - if (SPIFFS.begin()) { -#endif - Serial.println("SPIFFS opened!"); - ftpSrv.begin("esp8266","esp8266"); //username, password for ftp. set ports in ESP8266FtpServer.h (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(); - -}
diff --git a/examples/LittleFSSample/LittleFSSample.ino b/examples/LittleFSSample/LittleFSSample.ino new file mode 100644 index 0000000..b6bfdd6 --- /dev/null +++ b/examples/LittleFSSample/LittleFSSample.ino @@ -0,0 +1,125 @@ +/* + This is an example sketch to show the use of the espFTP server. + + Please replace + YOUR_SSID and YOUR_PASS + with your WiFi's values and compile. + + If you want to see debugging output of the FTP server, please + select select an Serial Port in the Arduino IDE menu Tools->Debug Port + + Send L via Serial Monitor, to display the contents of the FS + Send F via Serial Monitor, to fromat the FS + + This example is provided as Public Domain + Daniel Plasa <dplasa@gmail.com> + +*/ +#ifdef ESP8266 +#include <LittleFS.h> +#include <ESP8266WiFi.h> +#elif defined ESP32 +#include <WiFi.h> +#include <SPIFFS.h> +#endif + +#include <espFtpServer.h> + +const char *ssid PROGMEM = ""; +const char *password PROGMEM = ""; + +// tell the FtpServer to use LittleFS +FtpServer ftpSrv(LittleFS); + +void setup(void) +{ + Serial.begin(74880); + WiFi.begin(ssid, password); + + bool fsok = LittleFS.begin(); + Serial.printf_P(PSTR("FS init: %S\n"), fsok ? PSTR("ok") : PSTR("fail!")); + + // Wait for connection + while (WiFi.status() != WL_CONNECTED) + { + delay(500); + Serial.printf_P(PSTR(".")); + } + Serial.printf_P(PSTR("\nConnected to %S, IP address is %s\n"), ssid, WiFi.localIP().toString().c_str()); + + // setup the ftp server with username and password + // ports are defined in esFTP.h, default is + // 21 for the control connection + // 50009 for the data connection (passive mode by default) + ftpSrv.begin(F("ftp"), F("ftp")); //username, password for ftp. set ports in ESP8266FtpServer.h (default 21, 50009 for PASV) +} + +enum consoleaction +{ + show, + wait, + format, + list +}; +consoleaction action = show; + +void loop(void) +{ + // this is all you need + // make sure to call handleFTP() frequently + ftpSrv.handleFTP(); + + // + // Code below just to debug in Serial Monitor + // + if (action == show) + { + Serial.printf_P(PSTR("Enter 'F' to format, 'L' to list the contents of the FS\n")); + action = wait; + } + else if (action == wait) + { + if (Serial.available()) + { + char c = Serial.read(); + if (c == 'F') + action = format; + else if (c == 'L') + action = list; + else if (!(c == '\n' || c == '\r')) + action = show; + } + } + else if (action == format) + { + uint32_t startTime = millis(); + LittleFS.format(); + Serial.printf_P(PSTR("FS format done, took %lu ms!\n"), millis() - startTime); + action = show; + } + else if (action == list) + { + Serial.printf_P(PSTR("Listing contents...\n")); + uint16_t fileCount = listDir("", "/"); + Serial.printf_P(PSTR("%d files/dirs total\n"), fileCount); + action = show; + } +} + +uint16_t listDir(String indent, String path) +{ + uint16_t dirCount = 0; + Dir dir = LittleFS.openDir(path); + while (dir.next()) + { + ++dirCount; + if (dir.isDirectory()) + { + Serial.printf_P(PSTR("%s%s [Dir]\n"), indent.c_str(), dir.fileName().c_str()); + dirCount += listDir(indent + " ", path + dir.fileName() + "/"); + } + else + Serial.printf_P(PSTR("%s%-16s (%ld Bytes)\n"), indent.c_str(), dir.fileName().c_str(), (uint32_t)dir.fileSize()); + } + return dirCount; +} diff --git a/examples/SPIFFSSample/SPIFFSSample.ino b/examples/SPIFFSSample/SPIFFSSample.ino new file mode 100644 index 0000000..6438afe --- /dev/null +++ b/examples/SPIFFSSample/SPIFFSSample.ino @@ -0,0 +1,115 @@ +/* + This is an example sketch to show the use of the espFTP server. + + Please replace + YOUR_SSID and YOUR_PASS + with your WiFi's values and compile. + + If you want to see debugging output of the FTP server, please + select select an Serial Port in the Arduino IDE menu Tools->Debug Port + + Send L via Serial Monitor, to display the contents of the FS + Send F via Serial Monitor, to fromat the FS + + This example is provided as Public Domain + Daniel Plasa <dplasa@gmail.com> + +*/ +#ifdef ESP8266 +#include <FS.h> +#include <ESP8266WiFi.h> +#elif defined ESP32 +#include <WiFi.h> +#include <SPIFFS.h> +#endif + +#include <espFtpServer.h> + +const char *ssid PROGMEM = ""; +const char *password PROGMEM = ""; + +// Since SPIFFS is becoming deprecated but might still be in +// use in your Projects, tell the FtpServer to use SPIFFS +FtpServer ftpSrv(SPIFFS); + +void setup(void) +{ + Serial.begin(74880); + WiFi.begin(ssid, password); + + bool fsok = SPIFFS.begin(); + Serial.printf_P(PSTR("FS init: %S\n"), fsok ? PSTR("ok") : PSTR("fail!")); + + // Wait for connection + while (WiFi.status() != WL_CONNECTED) + { + delay(500); + Serial.printf_P(PSTR(".")); + } + Serial.printf_P(PSTR("\nConnected to %S, IP address is %s\n"), ssid, WiFi.localIP().toString().c_str()); + + // setup the ftp server with username and password + // ports are defined in esFTP.h, default is + // 21 for the control connection + // 50009 for the data connection (passive mode by default) + ftpSrv.begin(F("ftp"), F("ftp")); +} + +enum consoleaction +{ + show, + wait, + format, + list +}; + +consoleaction action = show; + +void loop(void) +{ + // this is all you need + // make sure to call handleFTP() frequently + ftpSrv.handleFTP(); + + // + // Code below just to debug in Serial Monitor + // + if (action == show) + { + Serial.printf_P(PSTR("Enter 'F' to format, 'L' to list the contents of the FS\n")); + action = wait; + } + else if (action == wait) + { + if (Serial.available()) + { + char c = Serial.read(); + if (c == 'F') + action = format; + else if (c == 'L') + action = list; + else if (!(c == '\n' || c == '\r')) + action = show; + } + } + else if (action == format) + { + uint32_t startTime = millis(); + SPIFFS.format(); + Serial.printf_P(PSTR("FS format done, took %lu ms!\n"), millis() - startTime); + action = show; + } + else if (action == list) + { + Serial.printf_P(PSTR("Listing contents...\n")); + uint16_t dirCount = 0; + Dir dir = SPIFFS.openDir(F("/")); + while (dir.next()) + { + ++dirCount; + Serial.printf_P(PSTR("%6ld %s\n"), (uint32_t)dir.fileSize(), dir.fileName().c_str()); + } + Serial.printf_P(PSTR("%d files total\n"), dirCount); + action = show; + } +} |