Skip to content

Commit

Permalink
Implement LITTLEFS as requested by @thomastech in s00500#144
Browse files Browse the repository at this point in the history
Signed-off-by: Lukas Bachschwell <[email protected]>
  • Loading branch information
s00500 committed Jan 4, 2022
1 parent 3cbae2f commit e1fe13b
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 40 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ This library is dependent on the following libraries to function properly.

- (_For ESP8266_) [ESPAsyncTCP](https://github.com/me-no-dev/ESPAsyncTCP)
- (_For ESP32_) [AsyncTCP](https://github.com/me-no-dev/AsyncTCP)
- (_For ESP32_) [lorol/LittleFS_esp32](https://github.com/lorol/LITTLEFS)

## How to Install

Expand Down Expand Up @@ -89,13 +90,13 @@ Go to Sketch>Include Library>Add .zip Library> Select the Downloaded .zip File.
## Getting started

ESPUI serves several files to the browser to build up its web interface. This
can be achieved in 2 ways: _PROGMEM_ or _SPIFFS_
can be achieved in 2 ways: _PROGMEM_ or _LITTLEFS_

_When `ESPUI.begin()` is called the default is serving files from Memory and
ESPUI should work out of the box!_

**OPTIONAL:** But if this causes your program to _use too much memory_ you can
burn the files into the SPIFFS filesystem on the ESP. There are now two ways to
burn the files into the LITTLEFS filesystem on the ESP. There are now two ways to
do this: you can either use the ESP file upload tool or you use the library
function `ESPUI.prepareFileSystem()`

Expand Down Expand Up @@ -296,7 +297,7 @@ Then all widgets for the tab need to be added to it by specifying the tab as the
### Initialisation of the UI

After all the elements are configured you can use `ESPUI.begin("Some Title");`
to start the UI interface. (Or `ESPUI.beginSPIFFS("Some Title");` respectively)
to start the UI interface. (Or `ESPUI.beginLITTLEFS("Some Title");` respectively)
Make sure you setup a working network connection or AccesPoint **before** (See
gui.ino example). The web interface can then be used from multiple devices at once and
also shows an connection status in the top bar.
Expand Down
1 change: 1 addition & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ slider KEYWORD2

begin KEYWORD2
beginSPIFFS KEYWORD2
beginLITTLEFS KEYWORD2
print KEYWORD2
updateSwitcher KEYWORD2
updateSlider KEYWORD2
Expand Down
22 changes: 15 additions & 7 deletions library.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@
"type": "git",
"url": "https://github.com/s00500/ESPUI.git"
},
"authors": [{
"name": "Lukas Bachschwell",
"email": "[email protected]",
"url": "https://lbsfilm.at",
"maintainer": true
}],
"dependencies": [{
"authors": [
{
"name": "Lukas Bachschwell",
"email": "[email protected]",
"url": "https://lbsfilm.at",
"maintainer": true
}
],
"dependencies": [
{
"name": "ESP Async WebServer",
"authors": "Hristo Gochkov",
"frameworks": "arduino"
Expand All @@ -21,6 +24,11 @@
"name": "ArduinoJson",
"authors": "Benoit Blanchon",
"frameworks": "arduino"
},
{
"name": "LittleFS_esp32",
"authors": "lorol",
"frameworks": "arduino"
}
],
"version": "2.0.2",
Expand Down
9 changes: 9 additions & 0 deletions pio_examples/gui/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,12 @@ board = nodemcuv2
framework = arduino

lib_extra_dirs = ../../

[env:esp32]
platform = espressif32
board = esp32dev
framework = arduino

lib_extra_dirs = ../../

lib_deps = lorol/LittleFS_esp32
54 changes: 30 additions & 24 deletions src/ESPUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

uint16_t Control::idCounter = 1;

// ################# Spiffs functions
// ################# LITTLEFS functions
#if defined(ESP32)
void listDir(const char* dirname, uint8_t levels)
{
Expand All @@ -27,7 +27,7 @@ void listDir(const char* dirname, uint8_t levels)
#endif

#if defined(ESP32)
File root = SPIFFS.open(dirname);
File root = LITTLEFS.open(dirname);
#else
File root = LittleFS.open(dirname);
#endif
Expand Down Expand Up @@ -115,9 +115,9 @@ void listDir(const char* dirname, uint8_t levels)
void ESPUIClass::list()
{
#if defined(ESP32)
if (!SPIFFS.begin())
if (!LITTLEFS.begin())
{
Serial.println(F("SPIFFS Mount Failed"));
Serial.println(F("LITTLEFS Mount Failed"));
return;
}
#else
Expand All @@ -131,8 +131,8 @@ void ESPUIClass::list()
listDir("/", 1);
#if defined(ESP32)

Serial.println(SPIFFS.totalBytes());
Serial.println(SPIFFS.usedBytes());
Serial.println(LITTLEFS.totalBytes());
Serial.println(LITTLEFS.usedBytes());

#else
FSInfo fs_info;
Expand All @@ -147,7 +147,7 @@ void ESPUIClass::list()
void deleteFile(const char* path)
{
#if defined(ESP32)
bool exists = SPIFFS.exists(path);
bool exists = LITTLEFS.exists(path);
#else
bool exists = LittleFS.exists(path);
#endif
Expand All @@ -172,7 +172,7 @@ void deleteFile(const char* path)
#endif

#if defined(ESP32)
bool didRemove = SPIFFS.remove(path);
bool didRemove = LITTLEFS.remove(path);
#else
bool didRemove = LittleFS.remove(path);
#endif
Expand Down Expand Up @@ -206,7 +206,7 @@ void writeFile(const char* path, const char* data)
#endif

#if defined(ESP32)
File file = SPIFFS.open(path, FILE_WRITE);
File file = LITTLEFS.open(path, FILE_WRITE);
#else
File file = LittleFS.open(path, FILE_WRITE);
#endif
Expand Down Expand Up @@ -269,7 +269,7 @@ void writeFile(const char* path, const char* data)
file.close();
}

// end Spiffs functions
// end LITTLEFS functions

void ESPUIClass::prepareFileSystem()
{
Expand All @@ -283,14 +283,14 @@ void ESPUIClass::prepareFileSystem()
#endif

#if defined(ESP32)
SPIFFS.format();
LITTLEFS.format();

if (!SPIFFS.begin(true))
if (!LITTLEFS.begin(true))
{
#if defined(DEBUG_ESPUI)
if (this->verbosity)
{
Serial.println(F("SPIFFS Mount Failed"));
Serial.println(F("LITTLEFS Mount Failed"));
}
#endif

Expand All @@ -301,7 +301,7 @@ void ESPUIClass::prepareFileSystem()
if (this->verbosity)
{
listDir("/", 1);
Serial.println(F("SPIFFS Mount ESP32 Done"));
Serial.println(F("LITTLEFS Mount ESP32 Done"));
}
#endif

Expand All @@ -312,7 +312,7 @@ void ESPUIClass::prepareFileSystem()
#if defined(DEBUG_ESPUI)
if (this->verbosity)
{
Serial.println(F("SPIFFS Mount ESP8266 Done"));
Serial.println(F("LITTLEFS Mount ESP8266 Done"));
}
#endif

Expand Down Expand Up @@ -368,7 +368,7 @@ void ESPUIClass::prepareFileSystem()
#endif

#if defined(ESP32)
SPIFFS.end();
LITTLEFS.end();
#else
LittleFS.end();
#endif
Expand Down Expand Up @@ -785,7 +785,7 @@ void ESPUIClass::updateControl(Control* control, int clientId)
// function like this and it's clients array is private
int tryId = 0;

for (int count = 0; count < this->ws->count();)
for (size_t count = 0; count < this->ws->count();)
{
if (this->ws->hasClient(tryId))
{
Expand Down Expand Up @@ -943,7 +943,7 @@ void ESPUIClass::addGraphPoint(uint16_t id, int nValue, int clientId)
// function like this and it's clients array is private
int tryId = 0;

for (int count = 0; count < this->ws->count();)
for (size_t count = 0; count < this->ws->count();)
{
if (this->ws->hasClient(tryId))
{
Expand Down Expand Up @@ -1080,6 +1080,12 @@ void ESPUIClass::jsonReload()
}

void ESPUIClass::beginSPIFFS(const char* _title, const char* username, const char* password, uint16_t port)
{
// Backwards compatibility wrapper
beginLITTLEFS(_title, username, password, port);
}

void ESPUIClass::beginLITTLEFS(const char* _title, const char* username, const char* password, uint16_t port)
{
ui_title = _title;
this->basicAuthUsername = username;
Expand All @@ -1098,7 +1104,7 @@ void ESPUIClass::beginSPIFFS(const char* _title, const char* username, const cha
ws = new AsyncWebSocket("/ws");

#if defined(ESP32)
bool fsBegin = SPIFFS.begin();
bool fsBegin = LITTLEFS.begin();
#else
bool fsBegin = LittleFS.begin();
#endif
Expand All @@ -1107,7 +1113,7 @@ void ESPUIClass::beginSPIFFS(const char* _title, const char* username, const cha
#if defined(DEBUG_ESPUI)
if (ESPUI.verbosity)
{
Serial.println(F("SPIFFS Mount Failed, PLEASE CHECK THE README ON HOW TO "
Serial.println(F("LITTLEFS Mount Failed, PLEASE CHECK THE README ON HOW TO "
"PREPARE YOUR ESP!!!!!!!"));
}
#endif
Expand All @@ -1123,7 +1129,7 @@ void ESPUIClass::beginSPIFFS(const char* _title, const char* username, const cha
#endif

#if defined(ESP32)
bool indexExists = SPIFFS.exists("/index.htm");
bool indexExists = LITTLEFS.exists("/index.htm");
#else
bool indexExists = LittleFS.exists("/index.htm");
#endif
Expand All @@ -1150,15 +1156,15 @@ void ESPUIClass::beginSPIFFS(const char* _title, const char* username, const cha
ws->setAuthentication(ESPUI.basicAuthUsername, ESPUI.basicAuthPassword);
}
#if defined(ESP32)
server->serveStatic("/", SPIFFS, "/").setDefaultFile("index.htm").setAuthentication(username, password);
server->serveStatic("/", LITTLEFS, "/").setDefaultFile("index.htm").setAuthentication(username, password);
#else
server->serveStatic("/", LittleFS, "/").setDefaultFile("index.htm").setAuthentication(username, password);
#endif
}
else
{
#if defined(ESP32)
server->serveStatic("/", SPIFFS, "/").setDefaultFile("index.htm");
server->serveStatic("/", LITTLEFS, "/").setDefaultFile("index.htm");
#else
server->serveStatic("/", LittleFS, "/").setDefaultFile("index.htm");
#endif
Expand All @@ -1171,7 +1177,7 @@ void ESPUIClass::beginSPIFFS(const char* _title, const char* username, const cha
return request->requestAuthentication();
}

request->send(200, "text/plain", String(ESP.getFreeHeap()) + " In SPIFFSmode");
request->send(200, "text/plain", String(ESP.getFreeHeap()) + " In LITTLEFS mode");
});

server->onNotFound([](AsyncWebServerRequest* request) { request->send(404); });
Expand Down
13 changes: 7 additions & 6 deletions src/ESPUI.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
#if defined(ESP32)
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <LITTLEFS.h>

#include "SPIFFS.h"
#include "WiFi.h"

#else
Expand All @@ -24,7 +24,6 @@
#include <ESPAsyncWebServer.h>
#include <Hash.h>
#include <LittleFS.h>
#include <SPIFFSEditor.h>

#define FILE_WRITE "w"

Expand Down Expand Up @@ -222,11 +221,13 @@ class ESPUIClass
void begin(const char* _title, const char* username = nullptr, const char* password = nullptr,
uint16_t port = 80); // Setup server and page in Memorymode
void beginSPIFFS(const char* _title, const char* username = nullptr, const char* password = nullptr,
uint16_t port = 80); // Setup server and page in SPIFFSmode
uint16_t port = 80); // Setup server and page in LITTLEFS mode (DEPRECATED, use beginLITTLEFS)
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
// stuff into SPIFFS
void list(); // Lists SPIFFS directory
// stuff into LITTLEFS
void list(); // Lists LITTLEFS directory

uint16_t addControl(ControlType type, const char* label, const String& value = String(""),
ControlColor color = ControlColor::Turquoise, uint16_t parentControl = Control::noParent,
Expand Down Expand Up @@ -302,7 +303,7 @@ class ESPUIClass
const char* basicAuthPassword = nullptr;
bool basicAuth = true;

Control *prepareJSONChunk(AsyncWebSocketClient* client, Control *control, JsonArray *items);
Control* prepareJSONChunk(AsyncWebSocketClient* client, Control* control, JsonArray* items);
};

extern ESPUIClass ESPUI;
Expand Down

0 comments on commit e1fe13b

Please sign in to comment.