From 5b1075151af5e6f4b56dac36a8013cde8deb558b Mon Sep 17 00:00:00 2001 From: Matthias Hertel Date: Fri, 2 Aug 2024 20:20:17 +0200 Subject: [PATCH 1/4] Update WebServer.ino * Enable FAT and LittleFS filesystems as configured. * use new versions of RequestHandler::canHandle and RequestHandler::canUpload --- .../examples/WebServer/WebServer.ino | 95 ++++++++++++++----- 1 file changed, 71 insertions(+), 24 deletions(-) diff --git a/libraries/WebServer/examples/WebServer/WebServer.ino b/libraries/WebServer/examples/WebServer/WebServer.ino index 5757c09cbae..be1b0bbbe4d 100644 --- a/libraries/WebServer/examples/WebServer/WebServer.ino +++ b/libraries/WebServer/examples/WebServer/WebServer.ino @@ -18,14 +18,15 @@ // // Please use the following Arduino IDE configuration // -// * Board: ESP32 Dev Module -// * Partition Scheme: Default 4MB with spiffs (1.2MB APP/1.5MB SPIFFS) +// * Board: "ESP32 Dev Module" or other board with ESP32 +// * Partition Scheme: "Default 4MB with spiffs" or any other scheme with spiffs or FAT // but LittleFS will be used in the partition (not SPIFFS) // * other setting as applicable // // Changelog: // 21.07.2021 creation, first version // 08.01.2023 ESP32 version with ETag +// 02.08.2024 support LitteFS and FAT file systems (on large Flash chips) #include #include @@ -33,8 +34,11 @@ #include "secrets.h" // add WLAN Credentials in here. +#include "esp_partition.h" // to check existing data partitions in Flash memory + #include // File System for Web Server Files -#include // This file system is used. +#include // Use LittleFSThis file system is used. +#include // or.. FAT // mark parameters not used in example #define UNUSED __attribute__((unused)) @@ -51,6 +55,9 @@ // need a WebServer for http access on port 80. WebServer server(80); +// The file system in use... +fs::FS *fsys = nullptr; + // The text of builtin files are in this header file #include "builtinfiles.h" @@ -65,7 +72,7 @@ void handleRedirect() { TRACE("Redirect...\n"); String url = "/index.htm"; - if (!LittleFS.exists(url)) { + if (!fsys->exists(url)) { url = "/$upload.htm"; } @@ -76,7 +83,7 @@ void handleRedirect() { // This function is called when the WebServer was requested to list all existing files in the filesystem. // a JSON array with file information is returned. void handleListFiles() { - File dir = LittleFS.open("/", "r"); + File dir = fsys->open("/", "r"); String result; result += "[\n"; @@ -107,8 +114,15 @@ void handleSysInfo() { result += " \"Chip Revision\": " + String(ESP.getChipRevision()) + ",\n"; result += " \"flashSize\": " + String(ESP.getFlashChipSize()) + ",\n"; result += " \"freeHeap\": " + String(ESP.getFreeHeap()) + ",\n"; - result += " \"fsTotalBytes\": " + String(LittleFS.totalBytes()) + ",\n"; - result += " \"fsUsedBytes\": " + String(LittleFS.usedBytes()) + ",\n"; + + if (fsys == (fs::FS *)&FFat) { + result += " \"fsTotalBytes\": " + String(FFat.totalBytes()) + ",\n"; + result += " \"fsUsedBytes\": " + String(FFat.usedBytes()) + ",\n"; + } else { + result += " \"fsTotalBytes\": " + String(LittleFS.totalBytes()) + ",\n"; + result += " \"fsUsedBytes\": " + String(LittleFS.usedBytes()) + ",\n"; + } + result += "}"; server.sendHeader("Cache-Control", "no-cache"); @@ -132,11 +146,11 @@ public: // @param requestMethod method of the http request line. // @param requestUri request resource from the http request line. // @return true when method can be handled. - bool canHandle(HTTPMethod requestMethod, String UNUSED uri) override { + bool canHandle(WebServer &server, HTTPMethod requestMethod, String uri) override { return ((requestMethod == HTTP_POST) || (requestMethod == HTTP_DELETE)); } // canHandle() - bool canUpload(String uri) override { + bool canUpload(WebServer &server, String uri) override { // only allow upload on root fs level. return (uri == "/"); } // canUpload() @@ -148,15 +162,13 @@ public: fName = "/" + fName; } - TRACE("handle %s\n", fName.c_str()); - if (requestMethod == HTTP_POST) { // all done in upload. no other forms. } else if (requestMethod == HTTP_DELETE) { - if (LittleFS.exists(fName)) { + if (fsys->exists(fName)) { TRACE("DELETE %s\n", fName.c_str()); - LittleFS.remove(fName); + fsys->remove(fName); } } // if @@ -165,7 +177,7 @@ public: } // handle() // uploading process - void upload(WebServer UNUSED &server, String UNUSED _requestUri, HTTPUpload &upload) override { + void upload(WebServer UNUSED &server, String requestUri, HTTPUpload &upload) override { // ensure that filename starts with '/' static size_t uploadSize; @@ -178,10 +190,10 @@ public: } TRACE("start uploading file %s...\n", fName.c_str()); - if (LittleFS.exists(fName)) { - LittleFS.remove(fName); + if (fsys->exists(fName)) { + fsys->remove(fName); } // if - _fsUploadFile = LittleFS.open(fName, "w"); + _fsUploadFile = fsys->open(fName, "w"); uploadSize = 0; } else if (upload.status == UPLOAD_FILE_WRITE) { @@ -198,7 +210,7 @@ public: if (!fName.startsWith("/")) { fName = "/" + fName; } - LittleFS.remove(fName); + fsys->remove(fName); } uploadSize += upload.currentSize; // TRACE("free:: %d of %d\n", LittleFS.usedBytes(), LittleFS.totalBytes()); @@ -207,7 +219,7 @@ public: } // if } else if (upload.status == UPLOAD_FILE_END) { - TRACE("finished.\n"); + TRACE("upload done.\n"); // Close the file if (_fsUploadFile) { _fsUploadFile.close(); @@ -231,14 +243,49 @@ void setup(void) { TRACE("Starting WebServer example...\n"); + // ----- check partitions for finding the fileystem type ----- + esp_partition_iterator_t i; + + i = esp_partition_find(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_FAT, nullptr); + if (i) { + TRACE("FAT partition found."); + fsys = &FFat; + + } else { + i = esp_partition_find(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_SPIFFS, nullptr); + if (i) { + TRACE("SPIFFS partition found."); + fsys = &LittleFS; + + } else { + TRACE("no partition found."); + } + } + esp_partition_iterator_release(i); + + // mount and format as needed TRACE("Mounting the filesystem...\n"); - if (!LittleFS.begin()) { + if (!fsys) { + TRACE("need to change the board configuration to include a partition for files.\n"); + delay(30000); + + } else if ((fsys == (fs::FS *)&FFat) && (!FFat.begin())) { + TRACE("could not mount the filesystem...\n"); + delay(2000); + TRACE("formatting FAT...\n"); + FFat.format(); + delay(2000); + TRACE("restarting...\n"); + delay(2000); + ESP.restart(); + + } else if ((fsys == (fs::FS *)&LittleFS) && (!LittleFS.begin())) { TRACE("could not mount the filesystem...\n"); delay(2000); - TRACE("formatting...\n"); + TRACE("formatting LittleFS...\n"); LittleFS.format(); delay(2000); - TRACE("restart.\n"); + TRACE("restarting...\n"); delay(2000); ESP.restart(); } @@ -286,7 +333,7 @@ void setup(void) { // UPLOAD and DELETE of files in the file system using a request handler. server.addHandler(new FileServerHandler()); - // // enable CORS header in webserver results + // enable CORS header in webserver results server.enableCORS(true); // enable ETAG header in webserver results (used by serveStatic handler) @@ -306,7 +353,7 @@ void setup(void) { #endif // serve all static files - server.serveStatic("/", LittleFS, "/"); + server.serveStatic("/", *fsys, "/"); TRACE("Register default (not found) answer...\n"); From 59c2b8c8eec3b0084147290a2542cc246c76b854 Mon Sep 17 00:00:00 2001 From: Matthias Hertel Date: Fri, 2 Aug 2024 20:33:07 +0200 Subject: [PATCH 2/4] Update Documentation --- .../WebServer/examples/WebServer/README.md | 26 ++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/libraries/WebServer/examples/WebServer/README.md b/libraries/WebServer/examples/WebServer/README.md index 2af4e379433..46f29ae3bc6 100644 --- a/libraries/WebServer/examples/WebServer/README.md +++ b/libraries/WebServer/examples/WebServer/README.md @@ -10,6 +10,12 @@ This example requires some space for a filesystem and runs fine boards with 4 MB * Partition Scheme: Default 4MB with spiffs (1.2MB APP/1.5MB SPIFFS) but LittleFS will be used in the partition (not SPIFFS) +or + +* Board: Arduino Nano ESP32 +* Partition Scheme: "With FAT partition" + + It features * Setup a web server @@ -28,9 +34,9 @@ It features Currently, this example supports the following targets. -| Supported Targets | ESP32 | ESP32-S2 | ESP32-C3 | -| ----------------- | ----- | -------- | -------- | -| | yes | yes | yes | +| Supported Targets | ESP32 | ESP32-S2 | ESP32-S3 | ESP32-C3 | +| ----------------- | ----- | -------- | -------- | -------- | +| | yes | no | yes | yes | ## Use the Example @@ -200,6 +206,7 @@ This class has to implements several functions and works in a more detailed way: * The `canUpload()`and `upload()` methods work similar while the `upload()` method is called multiple times to create, append data and close the new file. + ## File upload By opening you can easily upload files by dragging them over the drop area. @@ -238,6 +245,7 @@ You can see on the Serial output that one filesystem write error is reported. Please be patient and wait for the upload ending even when writing to the filesystem is disabled it maybe take more than a minute. + ## Registering a special handler for "file not found" Any other incoming request that was not handled by the registered plug-ins above can be detected by registering @@ -253,6 +261,7 @@ Any other incoming request that was not handled by the registered plug-ins above This allows sending back an "friendly" result for the browser. Here a simple html page is created from a static string. You can easily change the html code in the file `builtinfiles.h`. + ## customizations You may like to change the hostname and the timezone in the lines: @@ -262,10 +271,19 @@ You may like to change the hostname and the timezone in the lines: > #define TIMEZONE "CET-1CEST,M3.5.0,M10.5.0/3" > ``` + ## Troubleshooting Have a look in the Serial output for some additional runtime information. + +## Changes + +* 2024-08-02 -- Fixing for board implementation 3.0.4 ff. +* 2024-08-02 -- Support for FAT +* 2024-08-02 -- Tested with Arduino Nano ESP32 + + ## Contribute To know how to contribute to this project, see [How to contribute.](https://github.com/espressif/arduino-esp32/blob/master/CONTRIBUTING.rst) @@ -274,11 +292,13 @@ If you have any **feedback** or **issue** to report on this example/library, ple Before creating a new issue, be sure to try Troubleshooting and check if the same issue was already created by someone else. + ## Resources * Official ESP32 Forum: [Link](https://esp32.com) * Arduino-ESP32 Official Repository: [espressif/arduino-esp32](https://github.com/espressif/arduino-esp32) * ESP32 Datasheet: [Link to datasheet](https://www.espressif.com/sites/default/files/documentation/esp32_datasheet_en.pdf) * ESP32-S2 Datasheet: [Link to datasheet](https://www.espressif.com/sites/default/files/documentation/esp32-s2_datasheet_en.pdf) +* ESP32-S3 Datasheet: [Link to datasheet](https://www.espressif.com/sites/default/files/documentation/esp32-s3_datasheet_en.pdf) * ESP32-C3 Datasheet: [Link to datasheet](https://www.espressif.com/sites/default/files/documentation/esp32-c3_datasheet_en.pdf) * Official ESP-IDF documentation: [ESP-IDF](https://idf.espressif.com) From 8800468f46a4b05b128c4e9147288652f2d34ff7 Mon Sep 17 00:00:00 2001 From: Matthias Hertel Date: Tue, 6 Aug 2024 15:54:38 +0200 Subject: [PATCH 3/4] Documentation changed accoring review comments. --- .../WebServer/examples/WebServer/README.md | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/libraries/WebServer/examples/WebServer/README.md b/libraries/WebServer/examples/WebServer/README.md index 46f29ae3bc6..58c2137ca1c 100644 --- a/libraries/WebServer/examples/WebServer/README.md +++ b/libraries/WebServer/examples/WebServer/README.md @@ -6,14 +6,8 @@ It is a small project in it's own and has some files to use on the web server to This example requires some space for a filesystem and runs fine boards with 4 MByte flash using the following options: -* Board: ESP32 Dev Module -* Partition Scheme: Default 4MB with spiffs (1.2MB APP/1.5MB SPIFFS) - but LittleFS will be used in the partition (not SPIFFS) - -or - -* Board: Arduino Nano ESP32 -* Partition Scheme: "With FAT partition" +* For using SPIFFS(LittleFS): Default 4MB with spiffs (1.2MB APP/1.5MB SPIFFS) +* For using FATFS: Default 4MB with ffat (1.2MB APP/1.5MB FATFS) It features @@ -34,9 +28,9 @@ It features Currently, this example supports the following targets. -| Supported Targets | ESP32 | ESP32-S2 | ESP32-S3 | ESP32-C3 | -| ----------------- | ----- | -------- | -------- | -------- | -| | yes | no | yes | yes | +| Supported Targets | ESP32 | ESP32-S2 | ESP32-S3 | ESP32-C3 | ESP32-C6 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | +| | yes | yes | yes | yes | yes | ## Use the Example @@ -281,7 +275,6 @@ Have a look in the Serial output for some additional runtime information. * 2024-08-02 -- Fixing for board implementation 3.0.4 ff. * 2024-08-02 -- Support for FAT -* 2024-08-02 -- Tested with Arduino Nano ESP32 ## Contribute From b57b35faf83125e6a109e74a85bc6324469384b7 Mon Sep 17 00:00:00 2001 From: Matthias Hertel Date: Wed, 7 Aug 2024 09:31:08 +0200 Subject: [PATCH 4/4] README.md changed accoring to review comments. --- libraries/WebServer/examples/WebServer/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/WebServer/examples/WebServer/README.md b/libraries/WebServer/examples/WebServer/README.md index 58c2137ca1c..dada705b447 100644 --- a/libraries/WebServer/examples/WebServer/README.md +++ b/libraries/WebServer/examples/WebServer/README.md @@ -4,7 +4,7 @@ This example shows different techniques on how to use and extend the WebServer f It is a small project in it's own and has some files to use on the web server to show how to use simple REST based services. -This example requires some space for a filesystem and runs fine boards with 4 MByte flash using the following options: +This example requires some space for a filesystem and runs fine on supported SoCs with 4 MByte or more flash by selecting the proper partition table: * For using SPIFFS(LittleFS): Default 4MB with spiffs (1.2MB APP/1.5MB SPIFFS) * For using FATFS: Default 4MB with ffat (1.2MB APP/1.5MB FATFS)