From 004a374d792affe50fb298a24cf9e4fadf2818e7 Mon Sep 17 00:00:00 2001 From: Nick Reynolds Date: Thu, 13 Oct 2022 19:52:50 +0100 Subject: [PATCH 1/5] Add a little more feedback to the user and make it list the files. --- examples/prepareFilesystem/prepareFilesystem.ino | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/examples/prepareFilesystem/prepareFilesystem.ino b/examples/prepareFilesystem/prepareFilesystem.ino index 7f88204..eae58d8 100644 --- a/examples/prepareFilesystem/prepareFilesystem.ino +++ b/examples/prepareFilesystem/prepareFilesystem.ino @@ -3,7 +3,12 @@ void setup(void) { Serial.begin(115200); - ESPUI.prepareFileSystem(); + ESPUI.setVerbosity(Verbosity::Verbose); //Enable verbose output so you see the files in LittleFS + delay(500); //Delay to allow Serial Monitor to start after a reset + Serial.println(F("\nPreparing filesystem with ESPUI resources")); + ESPUI.prepareFileSystem(); //Copy across current version of ESPUI resources + Serial.println(F("Done, files...")); + ESPUI.list(); //List all files on LittleFS, for info } void loop() From 0c9586730e7d4433382244341482ce68a54fa34a Mon Sep 17 00:00:00 2001 From: Nick Reynolds Date: Thu, 13 Oct 2022 19:53:55 +0100 Subject: [PATCH 2/5] Add feature to optionally _not_ format LittleFS to prepareFileSystem() --- src/ESPUI.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ESPUI.h b/src/ESPUI.h index 4202b51..7fc0917 100644 --- a/src/ESPUI.h +++ b/src/ESPUI.h @@ -104,7 +104,7 @@ public: void beginLITTLEFS(const char* _title, const char* username = nullptr, const char* password = nullptr, uint16_t port = 80); // Setup server and page in LITTLEFS mode - void prepareFileSystem(); // Initially preps the filesystem and loads a lot of + void prepareFileSystem(bool format = true); // Initially preps the filesystem and loads a lot of // stuff into LITTLEFS void list(); // Lists LITTLEFS directory From 4b489c7ce56ced36c3bc8bbd36d1b8f8ef30956b Mon Sep 17 00:00:00 2001 From: Nick Reynolds Date: Thu, 13 Oct 2022 20:52:19 +0100 Subject: [PATCH 3/5] Support ESP-IDF versions prior to 4.4/Arduino Core 2.0 --- src/ESPUI.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/ESPUI.h b/src/ESPUI.h index 7fc0917..36a6239 100644 --- a/src/ESPUI.h +++ b/src/ESPUI.h @@ -7,7 +7,11 @@ #include #include #include -#include +#if (ESP_IDF_VERSION_MAJOR == 4 && ESP_IDF_VERSION_MINOR >= 4) || ESP_IDF_VERSION_MAJOR > 4 + #include +#else + #include +#endif #include #include From 309e53dd1eb047f85b1f2a93aed83f214a9e7693 Mon Sep 17 00:00:00 2001 From: Nick Reynolds Date: Thu, 13 Oct 2022 21:09:05 +0100 Subject: [PATCH 4/5] Sort out includes for LittleFS --- src/ESPUI.h | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/ESPUI.h b/src/ESPUI.h index 36a6239..b68c74d 100644 --- a/src/ESPUI.h +++ b/src/ESPUI.h @@ -7,10 +7,14 @@ #include #include #include -#if (ESP_IDF_VERSION_MAJOR == 4 && ESP_IDF_VERSION_MINOR >= 4) || ESP_IDF_VERSION_MAJOR > 4 - #include +#ifdef ESP32 + #if (ESP_IDF_VERSION_MAJOR == 4 && ESP_IDF_VERSION_MINOR >= 4) || ESP_IDF_VERSION_MAJOR > 4 + #include + #else + #include + #endif #else - #include + #include #endif #include #include From 09a550e6142f32784bfbd938e324747a83c202fe Mon Sep 17 00:00:00 2001 From: Nick Reynolds Date: Thu, 13 Oct 2022 21:10:20 +0100 Subject: [PATCH 5/5] Many LittleFS related changes/fixes --- src/ESPUI.cpp | 264 ++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 237 insertions(+), 27 deletions(-) diff --git a/src/ESPUI.cpp b/src/ESPUI.cpp index b65998c..3497c06 100644 --- a/src/ESPUI.cpp +++ b/src/ESPUI.cpp @@ -24,7 +24,15 @@ void listDir(const char* dirname, uint8_t levels) } #endif +#if defined(ESP32) + #if (ESP_IDF_VERSION_MAJOR == 4 && ESP_IDF_VERSION_MINOR >= 4) || ESP_IDF_VERSION_MAJOR > 4 + File root = LittleFS.open(dirname); + #else + File root = LITTLEFS.open(dirname); + #endif +#else File root = LittleFS.open(dirname); +#endif if (!root) { @@ -66,7 +74,11 @@ void listDir(const char* dirname, uint8_t levels) if (levels) { - listDir(file.name(), levels - 1); + #if (ESP_IDF_VERSION_MAJOR == 4 && ESP_IDF_VERSION_MINOR >= 4) || ESP_IDF_VERSION_MAJOR > 4 + listDir(file.path(), levels - 1); + #else + listDir(file.name(), levels - 1); + #endif } } else @@ -89,18 +101,45 @@ void listDir(const char* dirname, uint8_t levels) void listDir(const char* dirname, uint8_t levels) { - // ignoring levels for esp8266 - Serial.printf_P(PSTR("Listing directory: %s\n"), dirname); +#if defined(DEBUG_ESPUI) + if (ESPUI.verbosity) + { + Serial.printf_P(PSTR("Listing directory: %s\n"), dirname); + } +#endif - String str = ""; - Dir dir = LittleFS.openDir("/"); + Dir dir = LittleFS.openDir(dirname); while (dir.next()) { - Serial.print(F(" FILE: ")); - Serial.print(dir.fileName()); - Serial.print(F(" SIZE: ")); - Serial.println(dir.fileSize()); + if (dir.isDirectory()) + { +#if defined(DEBUG_ESPUI) + if (ESPUI.verbosity) + { + Serial.print(F(" DIR : ")); + Serial.println(dir.fileName()); + } +#endif + if (levels) + { + File file = dir.openFile("r"); + listDir(file.fullName(), levels - 1); + file.close(); + } + } + else + { +#if defined(DEBUG_ESPUI) + if (ESPUI.verbosity) + { + Serial.print(F(" FILE: ")); + Serial.print(dir.fileName()); + Serial.print(F(" SIZE: ")); + Serial.println(dir.fileSize()); + } +#endif + } } } @@ -108,31 +147,63 @@ void listDir(const char* dirname, uint8_t levels) void ESPUIClass::list() { +#if defined(ESP32) + #if (ESP_IDF_VERSION_MAJOR == 4 && ESP_IDF_VERSION_MINOR >= 4) || ESP_IDF_VERSION_MAJOR > 4 + if (!LittleFS.begin()) + #else + if (!LITTLEFS.begin()) + #endif + { + Serial.println(F("LITTLEFS Mount Failed")); + return; + } +#else if (!LittleFS.begin()) { Serial.println(F("LittleFS Mount Failed")); return; } +#endif listDir("/", 1); #if defined(ESP32) - Serial.println(LittleFS.totalBytes()); - Serial.println(LittleFS.usedBytes()); + Serial.print(F("Total KB: ")); + #if (ESP_IDF_VERSION_MAJOR == 4 && ESP_IDF_VERSION_MINOR >= 4) || ESP_IDF_VERSION_MAJOR > 4 + Serial.println(LittleFS.totalBytes()/1024); + #else + Serial.println(LITTLEFS.totalBytes()/1024); + #endif + Serial.print(F("Used KB: ")); + #if (ESP_IDF_VERSION_MAJOR == 4 && ESP_IDF_VERSION_MINOR >= 4) || ESP_IDF_VERSION_MAJOR > 4 + Serial.println(LittleFS.usedBytes()/1024); + #else + Serial.println(LITTLEFS.usedBytes()/1024); + #endif #else FSInfo fs_info; LittleFS.info(fs_info); - Serial.println(fs_info.totalBytes); - Serial.println(fs_info.usedBytes); + Serial.print(F("Total KB: ")); + Serial.println(fs_info.totalBytes/1024); + Serial.print(F("Used KB: ")); + Serial.println(fs_info.usedBytes/1024); #endif } void deleteFile(const char* path) { +#if defined(ESP32) + #if (ESP_IDF_VERSION_MAJOR == 4 && ESP_IDF_VERSION_MINOR >= 4) || ESP_IDF_VERSION_MAJOR > 4 + bool exists = LittleFS.exists(path); + #else + bool exists = LITTLEFS.exists(path); + #endif +#else bool exists = LittleFS.exists(path); +#endif if (!exists) { #if defined(DEBUG_ESPUI) @@ -152,7 +223,15 @@ void deleteFile(const char* path) } #endif +#if defined(ESP32) + #if (ESP_IDF_VERSION_MAJOR == 4 && ESP_IDF_VERSION_MINOR >= 4) || ESP_IDF_VERSION_MAJOR > 4 + bool didRemove = LittleFS.remove(path); + #else + bool didRemove = LITTLEFS.remove(path); + #endif +#else bool didRemove = LittleFS.remove(path); +#endif if (didRemove) { #if defined(DEBUG_ESPUI) @@ -182,7 +261,15 @@ void writeFile(const char* path, const char* data) } #endif +#if defined(ESP32) + #if (ESP_IDF_VERSION_MAJOR == 4 && ESP_IDF_VERSION_MINOR >= 4) || ESP_IDF_VERSION_MAJOR > 4 + File file = LittleFS.open(path, FILE_WRITE); + #else + File file = LITTLEFS.open(path, FILE_WRITE); + #endif +#else File file = LittleFS.open(path, FILE_WRITE); +#endif if (!file) { #if defined(DEBUG_ESPUI) @@ -243,7 +330,7 @@ void writeFile(const char* path, const char* data) // end LITTLEFS functions -void ESPUIClass::prepareFileSystem() +void ESPUIClass::prepareFileSystem(bool format) { // this function should only be used once @@ -255,19 +342,41 @@ void ESPUIClass::prepareFileSystem() #endif #if defined(ESP32) - LittleFS.format(); - - if (!LittleFS.begin(true)) - { -#if defined(DEBUG_ESPUI) - if (verbosity) - { - Serial.println(F("LittleFS Mount Failed")); - } +#if (ESP_IDF_VERSION_MAJOR == 4 && ESP_IDF_VERSION_MINOR >= 4) || ESP_IDF_VERSION_MAJOR > 4 + if(!LittleFS.begin(false)) //Test for an already formatted LittleFS by a mount failure +#else + if(!LITTLEFS.begin(false)) //Test for an already formatted LittleFS by a mount failure +#endif + { +#if (ESP_IDF_VERSION_MAJOR == 4 && ESP_IDF_VERSION_MINOR >= 4) || ESP_IDF_VERSION_MAJOR > 4 + if(!LittleFS.begin(true)) //Attempt to format LittleFS +#else + if(!LITTLEFS.begin(true)) //Attempt to format LittleFS +#endif + { +#if defined(DEBUG_ESPUI) + if (verbosity) + { + Serial.println(F("LittleFS Format Failed")); + } #endif - return; + } } + else if(format) + { +#if (ESP_IDF_VERSION_MAJOR == 4 && ESP_IDF_VERSION_MINOR >= 4) || ESP_IDF_VERSION_MAJOR > 4 + LittleFS.format(); +#else + LITTLEFS.format(); +#endif +#if defined(DEBUG_ESPUI) + if (verbosity) + { + Serial.println(F("LittleFS Formatted")); + } +#endif + } #if defined(DEBUG_ESPUI) if (verbosity) @@ -278,13 +387,45 @@ void ESPUIClass::prepareFileSystem() #endif #else - LittleFS.format(); - LittleFS.begin(); + + if (!LittleFS.begin()) //Test for an already formatted LittleFS by a mount failure + { + if(LittleFS.format()) //Attempt to format LittleFS + { +#if defined(DEBUG_ESPUI) + if (verbosity) + { + Serial.println(F("LittleFS Formatted")); + } +#endif + } + else + { +#if defined(DEBUG_ESPUI) + if (verbosity) + { + Serial.println(F("LittleFS Mount Failed")); + } +#endif + return; + } + } + else if(format) + { + LittleFS.format(); +#if defined(DEBUG_ESPUI) + if (verbosity) + { + Serial.println(F("LittleFS Formatted")); + } +#endif + } #if defined(DEBUG_ESPUI) if (verbosity) { - Serial.println(F("LITTLEFS Mount ESP8266 Done")); + listDir("/", 1); + Serial.println(F("LittleFS Mount ESP8266 Done")); } #endif @@ -309,6 +450,33 @@ void ESPUIClass::prepareFileSystem() #endif // Now write + #ifdef ESP32 + #if (ESP_IDF_VERSION_MAJOR == 4 && ESP_IDF_VERSION_MINOR >= 4) || ESP_IDF_VERSION_MAJOR > 4 + writeFile("/index.htm", HTML_INDEX); + LittleFS.mkdir("/css"); + writeFile("/css/style.css", CSS_STYLE); + writeFile("/css/normalize.css", CSS_NORMALIZE); + LittleFS.mkdir("/js"); + writeFile("/js/zepto.min.js", JS_ZEPTO); + writeFile("/js/controls.js", JS_CONTROLS); + writeFile("/js/slider.js", JS_SLIDER); + writeFile("/js/graph.js", JS_GRAPH); + + writeFile("/js/tabbedcontent.js", JS_TABBEDCONTENT); + #else + writeFile("/index.htm", HTML_INDEX); + LITTLEFS.mkdir("/css"); + writeFile("/css/style.css", CSS_STYLE); + writeFile("/css/normalize.css", CSS_NORMALIZE); + LITTLEFS.mkdir("/js"); + writeFile("/js/zepto.min.js", JS_ZEPTO); + writeFile("/js/controls.js", JS_CONTROLS); + writeFile("/js/slider.js", JS_SLIDER); + writeFile("/js/graph.js", JS_GRAPH); + + writeFile("/js/tabbedcontent.js", JS_TABBEDCONTENT); + #endif + #else writeFile("/index.htm", HTML_INDEX); writeFile("/css/style.css", CSS_STYLE); @@ -320,6 +488,7 @@ void ESPUIClass::prepareFileSystem() writeFile("/js/graph.js", JS_GRAPH); writeFile("/js/tabbedcontent.js", JS_TABBEDCONTENT); + #endif #if defined(DEBUG_ESPUI) if (verbosity) @@ -339,7 +508,16 @@ void ESPUIClass::prepareFileSystem() #endif +#if defined(ESP32) + #if (ESP_IDF_VERSION_MAJOR == 4 && ESP_IDF_VERSION_MINOR >= 4) || ESP_IDF_VERSION_MAJOR > 4 + LittleFS.end(); + #else + LITTLEFS.end(); + #endif +#else LittleFS.end(); +#endif + } // Handle Websockets Communication @@ -970,7 +1148,15 @@ void ESPUIClass::beginLITTLEFS(const char* _title, const char* username, const c server = new AsyncWebServer(port); ws = new AsyncWebSocket("/ws"); +#if defined(ESP32) + #if (ESP_IDF_VERSION_MAJOR == 4 && ESP_IDF_VERSION_MINOR >= 4) || ESP_IDF_VERSION_MAJOR > 4 + bool fsBegin = LittleFS.begin(); + #else + bool fsBegin = LITTLEFS.begin(); + #endif +#else bool fsBegin = LittleFS.begin(); +#endif if (!fsBegin) { #if defined(DEBUG_ESPUI) @@ -991,7 +1177,15 @@ void ESPUIClass::beginLITTLEFS(const char* _title, const char* username, const c } #endif +#if defined(ESP32) + #if (ESP_IDF_VERSION_MAJOR == 4 && ESP_IDF_VERSION_MINOR >= 4) || ESP_IDF_VERSION_MAJOR > 4 + bool indexExists = LittleFS.exists("/index.htm"); + #else + bool indexExists = LITTLEFS.exists("/index.htm"); + #endif +#else bool indexExists = LittleFS.exists("/index.htm"); +#endif if (!indexExists) { #if defined(DEBUG_ESPUI) @@ -1017,11 +1211,27 @@ void ESPUIClass::beginLITTLEFS(const char* _title, const char* username, const c { ws->setAuthentication(basicAuthUsername, basicAuthPassword); } +#if defined(ESP32) + #if (ESP_IDF_VERSION_MAJOR == 4 && ESP_IDF_VERSION_MINOR >= 4) || ESP_IDF_VERSION_MAJOR > 4 + server->serveStatic("/", LittleFS, "/").setDefaultFile("index.htm").setAuthentication(username, password); + #else + server->serveStatic("/", LITTLEFS, "/").setDefaultFile("index.htm").setAuthentication(username, password); + #endif +#else server->serveStatic("/", LittleFS, "/").setDefaultFile("index.htm").setAuthentication(username, password); +#endif } else { +#if defined(ESP32) + #if (ESP_IDF_VERSION_MAJOR == 4 && ESP_IDF_VERSION_MINOR >= 4) || ESP_IDF_VERSION_MAJOR > 4 + server->serveStatic("/", LittleFS, "/").setDefaultFile("index.htm"); + #else + server->serveStatic("/", LITTLEFS, "/").setDefaultFile("index.htm"); + #endif +#else server->serveStatic("/", LittleFS, "/").setDefaultFile("index.htm"); +#endif } // Heap for general Servertest