Skip to content

Use LittleFS + #209

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Oct 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,10 @@ Web App

Patterns are requested by the app from the ESP8266, so as new patterns are added, they're automatically listed in the app.

The web app is stored in SPIFFS (on-board flash memory).
The web app is stored in a file system in on-board flash memory. The file system used is LittleFS *(Note: prior versions used SPIFFS)*.

The web app is a single page app that uses [jQuery](https://jquery.com) and [Bootstrap](http://getbootstrap.com). It has buttons for On/Off, a slider for brightness, a pattern selector, and a color picker (using [jQuery MiniColors](http://labs.abeautifulsite.net/jquery-minicolors)). Event handlers for the controls are wired up, so you don't have to click a 'Send' button after making changes. The brightness slider and the color picker use a delayed event handler, to prevent from flooding the ESP8266 web server with too many requests too quickly.

The only drawback to SPIFFS that I've found so far is uploading the files can be extremely slow, requiring several minutes, sometimes regardless of how large the files are. It can be so slow that I've been just developing the web app and debugging locally on my desktop (with a hard-coded IP for the ESP8266), before uploading to SPIFFS and testing on the ESP8266.

Installing
-----------
The app is installed via the Arduino IDE which can be [downloaded here](https://www.arduino.cc/en/main/software). The ESP8266 boards will need to be added to the Arduino IDE which is achieved as follows. Click File > Preferences and copy and paste the URL "http://arduino.esp8266.com/stable/package_esp8266com_index.json" into the Additional Boards Manager URLs field. Click OK. Click Tools > Boards: ... > Boards Manager. Find and click on ESP8266 (using the Search function may expedite this). Click on Install. After installation, click on Close and then select your ESP8266 board from the Tools > Board: ... menu.
Expand All @@ -74,14 +72,15 @@ Here are the board settings I use:

![image](https://user-images.githubusercontent.com/3598755/135755572-52d4d0db-1dba-4388-a86c-a293e4f13878.png)

The web app needs to be uploaded to the ESP8266's SPIFFS. You can do this within the Arduino IDE after installing the [Arduino ESP8266FS tool](http://esp8266.github.io/Arduino/versions/2.3.0/doc/filesystem.html#uploading-files-to-file-system).
The web app needs to be uploaded to the ESP8266's file system.
You can do this within the Arduino IDE after installing the [Arduino ESP8266 LittleFS](https://github.com/earlephilhower/arduino-esp8266littlefs-plugin).

With ESP8266FS installed upload the web app using `ESP8266 Sketch Data Upload` command in the Arduino Tools menu.
With the upload tool installed, upload the web app using `ESP8266 LittleFS Data Upload` command in the Arduino Tools menu.

Compression
-----------

The web app files can be gzip compressed before uploading to SPIFFS by running the following command:
The web app files can be gzip compressed before uploading to the ESP8266's file system by running the following command:

`gzip -r data/`

Expand Down
23 changes: 13 additions & 10 deletions esp8266-fastled-webserver/FSBrowser.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

//holds the current upload
File fsUploadFile;

Expand Down Expand Up @@ -36,10 +37,10 @@ bool handleFileRead(String path){
if(path.endsWith("/")) path += "index.htm";
String contentType = getContentType(path);
String pathWithGz = path + ".gz";
if(SPIFFS.exists(pathWithGz) || SPIFFS.exists(path)){
if(SPIFFS.exists(pathWithGz))
if(MYFS.exists(pathWithGz) || MYFS.exists(path)){
if(MYFS.exists(pathWithGz))
path += ".gz";
File file = SPIFFS.open(path, "r");
File file = MYFS.open(path, "r");
(void)webServer.streamFile(file, contentType);
file.close();

Expand All @@ -55,7 +56,7 @@ void handleFileUpload(){
String filename = upload.filename;
if(!filename.startsWith("/")) filename = "/"+filename;
Serial.print("handleFileUpload Name: "); Serial.println(filename);
fsUploadFile = SPIFFS.open(filename, "w");
fsUploadFile = MYFS.open(filename, "w");
filename = String();
} else if(upload.status == UPLOAD_FILE_WRITE){
//Serial.print("handleFileUpload Data: "); Serial.println(upload.currentSize);
Expand All @@ -74,9 +75,9 @@ void handleFileDelete(){
Serial.println("handleFileDelete: " + path);
if(path == "/")
return webServer.send(500, "text/plain", "BAD PATH");
if(!SPIFFS.exists(path))
if(!MYFS.exists(path))
return webServer.send(404, "text/plain", "FileNotFound");
SPIFFS.remove(path);
MYFS.remove(path);
webServer.send(200, "text/plain", "");
path = String();
}
Expand All @@ -88,9 +89,9 @@ void handleFileCreate(){
Serial.println("handleFileCreate: " + path);
if(path == "/")
return webServer.send(500, "text/plain", "BAD PATH");
if(SPIFFS.exists(path))
if(MYFS.exists(path))
return webServer.send(500, "text/plain", "FILE EXISTS");
File file = SPIFFS.open(path, "w");
File file = MYFS.open(path, "w");
if(file)
file.close();
else
Expand All @@ -104,18 +105,20 @@ void handleFileList() {

String path = webServer.arg("dir");
Serial.println("handleFileList: " + path);
Dir dir = SPIFFS.openDir(path);
Dir dir = MYFS.openDir(path);
path = String();

String output = "[";
while(dir.next()){
File entry = dir.openFile("r");
if (output != "[") output += ',';
bool isDir = false;
//bool isDir = entry.isDirectory();

output += "{\"type\":\"";
output += (isDir)?"dir":"file";
output += "\",\"name\":\"";
output += String(entry.name()).substring(1);
output += String(entry.name());
output += "\"}";
entry.close();
}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
var address = location.hostname;
var urlBase = "";

// used when hosting the site somewhere other than the ESP8266 (handy for testing without waiting forever to upload to SPIFFS)
// used when hosting the site somewhere other than the ESP8266 (handy for testing without waiting forever to upload to SPIFFS/LittleFS)
// var address = "192.168.86.36";
// var urlBase = "http://" + address + "/";

Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
var address = location.hostname;
var urlBase = "";

// used when hosting the site somewhere other than the ESP8266 (handy for testing without waiting forever to upload to SPIFFS)
// used when hosting the site somewhere other than the ESP8266 (handy for testing without waiting forever to upload to SPIFFS/LittleFS)
// var address = "192.168.1.13";
// var urlBase = "http://" + address + "/";

Expand Down
File renamed without changes.
File renamed without changes.
18 changes: 12 additions & 6 deletions esp8266-fastled-webserver/esp8266-fastled-webserver.ino
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,18 @@ extern "C" {
#include "user_interface.h"
}

// #include <FS.h>
#include <LittleFS.h>
#define MYFS LittleFS


#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPUpdateServer.h>
#include <ESP8266HTTPClient.h>
//#include <WebSocketsServer.h>
#include <FS.h>

#include <EEPROM.h>
//#include <IRremoteESP8266.h>
#include <WiFiManager.h> // https://github.com/tzapu/WiFiManager/tree/development
Expand Down Expand Up @@ -249,11 +254,12 @@ void setup() {
Serial.print( F("MAC Address: ") ); Serial.println(WiFi.macAddress());
Serial.println();

SPIFFS.begin();
{
Serial.println("SPIFFS contents:");
if (!MYFS.begin()) {
Serial.println(F("An error occurred when attempting to mount the flash file system"));
} else {
Serial.println("FS contents:");

Dir dir = SPIFFS.openDir("/");
Dir dir = MYFS.openDir("/");
while (dir.next()) {
String fileName = dir.fileName();
size_t fileSize = dir.fileSize();
Expand Down Expand Up @@ -477,7 +483,7 @@ void setup() {
webServer.send(200, "text/plain", "");
}, handleFileUpload);

webServer.serveStatic("/", SPIFFS, "/", "max-age=86400");
webServer.serveStatic("/", MYFS, "/", "max-age=86400");

MDNS.begin(nameChar);
MDNS.setHostname(nameChar);
Expand Down
6 changes: 6 additions & 0 deletions deployapp.sh → scripts/deployapp.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ declare -a filenames=(
# "images/atom196.png"
# "favicon.ico"

# TODO -- update to be based on script's own directory
# TODO -- update to switch to build directory
pushd ../esp8266-fastled-webserver > /dev/null

for filename in "${filenames[@]}"
do
# add --trace-ascii curl.log for logging
Expand All @@ -31,3 +35,5 @@ do

rm -f data/$filename.gz
done

popd > /dev/null
6 changes: 6 additions & 0 deletions deployfirmware.sh → scripts/deployfirmware.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,10 @@ binFilename=tree-v2.ino.bin
ip=${1:-"192.168.86.36"}
url="http://$ip/update"

# TODO -- update to be based on script's own directory
# TODO -- update to switch to build directory
pushd ../esp8266-fastled-webserver > /dev/null

curl -v --form "file=@$outputDir/$binFilename;filename=$binFilename" $url

popd > /dev/null
7 changes: 7 additions & 0 deletions uploadfile.sh → scripts/uploadfile.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,16 @@ filename=$2

# add --trace-ascii curl.log for logging

# TODO -- update to be based on script's own directory
# TODO -- update to switch to build directory
pushd ../esp8266-fastled-webserver > /dev/null

gzip -kf data/$filename

echo $filename
curl --form "file=@data/$filename.gz;filename=$filename.gz" $url

rm -f data/$filename.gz

popd > /dev/null