From c6ab7bf13e691e870eff8d3799a5ffa6675dda13 Mon Sep 17 00:00:00 2001 From: Daniel Plasa Date: Thu, 28 May 2020 22:12:54 +0200 Subject: add Client example --- examples/FTPClientSample/FTPClientSample.ino | 164 +++++++++++++++++++++ .../LittleFSSample/LittleFSSample.ino | 124 ++++++++++++++++ .../FTPServerSample/SPIFFSSample/SPIFFSSample.ino | 115 +++++++++++++++ examples/LittleFSSample/LittleFSSample.ino | 124 ---------------- examples/SPIFFSSample/SPIFFSSample.ino | 115 --------------- 5 files changed, 403 insertions(+), 239 deletions(-) create mode 100644 examples/FTPClientSample/FTPClientSample.ino create mode 100644 examples/FTPServerSample/LittleFSSample/LittleFSSample.ino create mode 100644 examples/FTPServerSample/SPIFFSSample/SPIFFSSample.ino delete mode 100644 examples/LittleFSSample/LittleFSSample.ino delete mode 100644 examples/SPIFFSSample/SPIFFSSample.ino diff --git a/examples/FTPClientSample/FTPClientSample.ino b/examples/FTPClientSample/FTPClientSample.ino new file mode 100644 index 0000000..c456d11 --- /dev/null +++ b/examples/FTPClientSample/FTPClientSample.ino @@ -0,0 +1,164 @@ +/* + This is an example sketch to show the use of the FTP Client. + + Please replace + YOUR_SSID and YOUR_PASS + with your WiFi's values and compile. + + If you want to see debugging output of the FTP Client, 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 + Send G via Serial Monitor, to GET a file from a FTP Server + + This example is provided as Public Domain + Daniel Plasa + +*/ +#include +#include +#include + +const char *ssid PROGMEM = "YOUR_SSID"; +const char *password PROGMEM = "YOUR_PASS"; + +// tell the FTP Client to use LittleFS +FTPClient ftpClient(LittleFS); + +// provide FTP servers credentials and servername +FTPClient::ServerInfo ftpServerInfo("user", "password", "hostname_or_ip"); + +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()); + + ftpClient.begin(ftpServerInfo); +} + +enum consoleaction +{ + show, + get, + put, + wait, + format, + list +}; +consoleaction action = show; + +String fileName; +bool transferStarted = false; +uint32_t startTime; + +void loop() +{ + // this is all you need + // make sure to call handleFTP() frequently + ftpClient.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, 'G'/'P' + File to GET/PUT from/to FTP Server\n")); + action = wait; + } + else if (action == wait) + { + if (transferStarted ) + { + const FTPClient::Status &r = ftpClient.check(); + if (r.result == FTPClient::OK) + { + Serial.printf_P(PSTR("Transfer complete, took %lu ms\n"), millis() - startTime); + transferStarted = false; + } + else if (r.result == FTPClient::ERROR) + { + Serial.printf_P(PSTR("Transfer failed after %lu ms, code: %u, descr=%s\n"), millis() - startTime, ftpClient.check().code, ftpClient.check().desc.c_str()); + transferStarted = false; + } + } + + if (Serial.available()) + { + char c = Serial.read(); + if (c == 'F') + action = format; + else if (c == 'L') + action = list; + else if (c == 'G') + { + action = get; + fileName = Serial.readStringUntil('\n'); + fileName.trim(); + } + else if (c == 'P') + { + action = put; + fileName = Serial.readStringUntil('\n'); + fileName.trim(); + } + else if (!(c == '\n' || c == '\r')) + action = show; + } + } + + else if (action == format) + { + startTime = millis(); + LittleFS.format(); + Serial.printf_P(PSTR("FS format done, took %lu ms!\n"), millis() - startTime); + action = show; + } + + else if ((action == get) || (action == put)) + { + startTime = millis(); + ftpClient.transfer(fileName, fileName, action == get ? + FTPClient::FTP_GET_NONBLOCKING : FTPClient::FTP_PUT_NONBLOCKING); + Serial.printf_P(PSTR("transfer started, took %lu ms!\n"), millis() - startTime); + transferStarted = true; + 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/FTPServerSample/LittleFSSample/LittleFSSample.ino b/examples/FTPServerSample/LittleFSSample/LittleFSSample.ino new file mode 100644 index 0000000..197c730 --- /dev/null +++ b/examples/FTPServerSample/LittleFSSample/LittleFSSample.ino @@ -0,0 +1,124 @@ +/* + 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 + +*/ +#ifdef ESP8266 +#include +#elif defined ESP32 +#include +#endif + +#include +#include + +const char *ssid PROGMEM = "YOUR_SSID"; +const char *password PROGMEM = "YOUR_PASS"; + +// 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/FTPServerSample/SPIFFSSample/SPIFFSSample.ino b/examples/FTPServerSample/SPIFFSSample/SPIFFSSample.ino new file mode 100644 index 0000000..358404c --- /dev/null +++ b/examples/FTPServerSample/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 + +*/ +#ifdef ESP8266 +#include +#include +#elif defined ESP32 +#include +#include +#endif + +#include + +const char *ssid PROGMEM = "YOUR_SSID"; +const char *password PROGMEM = "YOUR_PASS"; + +// 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; + } +} diff --git a/examples/LittleFSSample/LittleFSSample.ino b/examples/LittleFSSample/LittleFSSample.ino deleted file mode 100644 index b5dc797..0000000 --- a/examples/LittleFSSample/LittleFSSample.ino +++ /dev/null @@ -1,124 +0,0 @@ -/* - 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 - -*/ -#ifdef ESP8266 -#include -#elif defined ESP32 -#include -#endif - -#include -#include - -const char *ssid PROGMEM = "YOUR_SSID"; -const char *password PROGMEM = "YOUR_PASS"; - -// 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 deleted file mode 100644 index dce703c..0000000 --- a/examples/SPIFFSSample/SPIFFSSample.ino +++ /dev/null @@ -1,115 +0,0 @@ -/* - 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 - -*/ -#ifdef ESP8266 -#include -#include -#elif defined ESP32 -#include -#include -#endif - -#include - -const char *ssid PROGMEM = "YOUR_SSID"; -const char *password PROGMEM = "YOUR_PASS"; - -// 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; - } -} -- cgit v1.2.3