Skip to content
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

move SPIFFS to LittleFS for ESP8266 #1226

Merged
merged 17 commits into from
Aug 12, 2020
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
25 changes: 25 additions & 0 deletions examples/IRMQTTServer/IRMQTTServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
#ifndef EXAMPLES_IRMQTTSERVER_IRMQTTSERVER_H_
#define EXAMPLES_IRMQTTSERVER_IRMQTTSERVER_H_

crankyoldgit marked this conversation as resolved.
Show resolved Hide resolved
#if defined(ESP8266)
#include <LittleFS.h>
#else
#include <SPIFFS.h>
#endif
#if defined(ESP8266)
#include <ESP8266WiFi.h>
#endif // ESP8266
Expand All @@ -27,6 +32,26 @@
#define EXAMPLES_ENABLE true
#endif // EXAMPLES_ENABLE

// Uncomment one of the following to manually override what
// type of persistent storage is used.
// Warning: Changing filesystems will cause all previous locally
// saved configuration data to be lost.
// #define FILESYSTEM SPIFFS
// #define FILESYSTEM LittleFS
#ifndef FILESYSTEM
// Set the default filesystem if none was specified.
#ifdef ESP8266
#define FILESYSTEM LittleFS
#else
#define FILESYSTEM SPIFFS
#endif // defined(ESP8266)
#endif // FILESYSTEM

#if (FILESYSTEM == LittleFS)
#define FILESYSTEMSTR "LittleFS"
#else
#define FILESYSTEMSTR "SPIFFS"
#endif
crankyoldgit marked this conversation as resolved.
Show resolved Hide resolved
// ---------------------- Board Related Settings -------------------------------
// NOTE: Make sure you set your Serial Monitor to the same speed.
#define BAUD_RATE 115200 // Serial port Baud rate.
Expand Down
38 changes: 19 additions & 19 deletions examples/IRMQTTServer/IRMQTTServer.ino
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
* o You MUST change <PubSubClient.h> to have the following (or larger) value:
* (with REPORT_RAW_UNKNOWNS 1024 or more is recommended)
* #define MQTT_MAX_PACKET_SIZE 768
* o Use the smallest non-zero SPIFFS size you can for your board.
* o Use the smallest non-zero FILESYSTEM size you can for your board.
* (See the Tools -> Flash Size menu)
*
* - PlatformIO IDE:
Expand Down Expand Up @@ -333,7 +333,7 @@

#include "IRMQTTServer.h"
#include <Arduino.h>
#include <FS.h>

#include <ArduinoJson.h>
#if defined(ESP8266)
#include <ESP8266WiFi.h>
Expand All @@ -344,7 +344,6 @@
#include <ESPmDNS.h>
#include <WebServer.h>
#include <WiFi.h>
#include <SPIFFS.h>
#include <Update.h>
#endif // ESP32
#include <WiFiClient.h>
Expand Down Expand Up @@ -515,18 +514,20 @@ void saveWifiConfigCallback(void) {
flagSaveWifiConfig = true;
}

// Forcibly mount the SPIFFS. Formatting the SPIFFS if needed.
// Forcibly mount the FILESYSTEM. Formatting the FILESYSTEM if needed.
//
// Returns:
// A boolean indicating success or failure.
bool mountSpiffs(void) {
debug("Mounting SPIFFS...");
if (SPIFFS.begin()) return true; // We mounted it okay.
debug("Mounting " FILESYSTEMSTR " ...");
if (FILESYSTEM.begin()) return true; // We mounted it okay.
// We failed the first time.
debug("Failed to mount SPIFFS!\nFormatting SPIFFS and trying again...");
SPIFFS.format();
if (!SPIFFS.begin()) { // Did we fail?
debug("DANGER: Failed to mount SPIFFS even after formatting!");
debug("Failed to mount " FILESYSTEMSTR "!\n"
"Formatting SPIFFS and trying again...");
FILESYSTEM.format();
if (!FILESYSTEM.begin()) { // Did we fail?
debug("DANGER: Failed to mount " FILESYSTEMSTR " even after formatting!");

delay(10000); // Make sure the debug message doesn't just float by.
return false;
}
Expand Down Expand Up @@ -556,7 +557,7 @@ bool saveConfig(void) {
}

if (mountSpiffs()) {
File configFile = SPIFFS.open(kConfigFile, "w");
File configFile = FILESYSTEM.open(kConfigFile, "w");
if (!configFile) {
debug("Failed to open config file for writing.");
} else {
Expand All @@ -566,7 +567,7 @@ bool saveConfig(void) {
debug("Finished writing config file.");
success = true;
}
SPIFFS.end();
FILESYSTEM.end();
}
return success;
}
Expand All @@ -575,10 +576,9 @@ bool loadConfigFile(void) {
bool success = false;
if (mountSpiffs()) {
debug("mounted the file system");
if (SPIFFS.exists(kConfigFile)) {
if (FILESYSTEM.exists(kConfigFile)) {
debug("config file exists");

File configFile = SPIFFS.open(kConfigFile, "r");
File configFile = FILESYSTEM.open(kConfigFile, "r");
if (configFile) {
debug("Opened config file");
size_t size = configFile.size();
Expand Down Expand Up @@ -619,8 +619,8 @@ bool loadConfigFile(void) {
} else {
debug("Config file doesn't exist!");
}
debug("Unmounting SPIFFS.");
SPIFFS.end();
debug("Unmounting " FILESYSTEMSTR);
FILESYSTEM.end();
}
return success;
}
Expand Down Expand Up @@ -1454,8 +1454,8 @@ void handleReset(void) {
#endif // MQTT_ENABLE
if (mountSpiffs()) {
debug("Removing JSON config file");
SPIFFS.remove(kConfigFile);
SPIFFS.end();
FILESYSTEM.remove(kConfigFile);
FILESYSTEM.end();
}
delay(1000);
debug("Reseting wifiManager's settings.");
Expand Down
37 changes: 37 additions & 0 deletions examples/Web-AC-control/Web-AC-control.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* Copyright 2019 Motea Marius

This example code will create a webserver that will provide basic control to AC units using the web application
build with javascript/css. User config zone need to be updated if a different class than Collix need to be used.
Javasctipt file may also require minor changes as in current version it will not allow to set fan speed if Auto mode
is selected (required for Coolix).

*/
#ifndef EXAMPLES_WEB_AC_CONTROL_WEB_AC_CONTROL_H_
#define EXAMPLES_WEB_AC_CONTROL_WEB_AC_CONTROL_H_
#if defined(ESP8266)
#include <LittleFS.h>
#else
#include <SPIFFS.h>
#endif

// Uncomment one of the following to manually override what
// type of persistent storage is used.
// Warning: Changing filesystems will cause all previous locally
// saved configuration data to be lost.
// #define FILESYSTEM SPIFFS
// #define FILESYSTEM LittleFS
#ifndef FILESYSTEM
// Set the default filesystem if none was specified.
#ifdef ESP8266
#define FILESYSTEM LittleFS
#else
#define FILESYSTEM SPIFFS
#endif // defined(ESP8266)
#endif // FILESYSTEM

#if (FILESYSTEM == LittleFS)
#define FILESYSTEMSTR "LittleFS"
#else
#define FILESYSTEMSTR "SPIFFS"
#endif
#endif // EXAMPLES_WEB_AC_CONTROL_WEB_AC_CONTROL_H_
24 changes: 12 additions & 12 deletions examples/Web-AC-control/Web-AC-control.ino
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
is selected (required for Coolix).

*/
#include <FS.h>
#include "Web-AC-control.h"
#if defined(ESP8266)
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
Expand All @@ -17,7 +17,6 @@
#include <ESPmDNS.h>
#include <WebServer.h>
#include <WiFi.h>
#include <SPIFFS.h>
#include <Update.h>
#endif // ESP32
#include <WiFiUdp.h>
Expand Down Expand Up @@ -73,17 +72,18 @@ WebServer server(80);

bool handleFileRead(String path) {
// send the right file to the client (if it exists)
// Serial.println("handleFileRead: " + path);
// Serial.println("handleFileRead: " + path);
if (path.endsWith("/")) path += "index.html";
// If a folder is requested, send the index file
String contentType = getContentType(path);
// Get the MIME type
String pathWithGz = path + ".gz";
if (SPIFFS.exists(pathWithGz) || SPIFFS.exists(path)) {
if (FILESYSTEM.exists(pathWithGz) || FILESYSTEM.exists(path)) {
// If the file exists, either as a compressed archive, or normal
if (SPIFFS.exists(pathWithGz)) // If there's a compressed version available
// If there's a compressed version available
if (FILESYSTEM.exists(pathWithGz))
path += ".gz"; // Use the compressed verion
File file = SPIFFS.open(path, "r");
File file = FILESYSTEM.open(path, "r");
// Open the file
server.streamFile(file, contentType);
// Send it to the client
Expand All @@ -107,14 +107,14 @@ String getContentType(String filename) {
return "text/plain";
}

void handleFileUpload() { // upload a new file to the SPIFFS
void handleFileUpload() { // upload a new file to the FILESYSTEM
HTTPUpload& upload = server.upload();
if (upload.status == UPLOAD_FILE_START) {
String filename = upload.filename;
if (!filename.startsWith("/")) filename = "/" + filename;
// Serial.print("handleFileUpload Name: "); //Serial.println(filename);
fsUploadFile = SPIFFS.open(filename, "w");
// Open the file for writing in SPIFFS (create if it doesn't exist)
fsUploadFile = FILESYSTEM.open(filename, "w");
// Open the file for writing in FILESYSTEM (create if it doesn't exist)
filename = String();
} else if (upload.status == UPLOAD_FILE_WRITE) {
if (fsUploadFile)
Expand Down Expand Up @@ -160,9 +160,9 @@ void setup() {

delay(1000);

// Serial.println("mounting FS...");
Serial.println("mounting " FILESYSTEMSTR "...");

if (!SPIFFS.begin()) {
if (!FILESYSTEM.begin()) {
// Serial.println("Failed to mount file system");
return;
}
Expand Down Expand Up @@ -287,7 +287,7 @@ void setup() {
ESP.restart();
});

server.serveStatic("/", SPIFFS, "/", "max-age=86400");
server.serveStatic("/", FILESYSTEM, "/", "max-age=86400");

server.onNotFound(handleNotFound);

Expand Down
3 changes: 2 additions & 1 deletion examples/Web-AC-control/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ monitor_speed = 115200
build_flags = ; -D_IR_LOCALE_=en-AU

[common]
ldscript_4m = eagle.flash.4m3m.ld
lib_deps_builtin =
lib_deps_external =
ArduinoJson@>=6.0
Expand All @@ -30,7 +31,7 @@ lib_deps_external =
platform = espressif8266
board = nodemcuv2
lib_deps = ${common_esp8266.lib_deps_external}

board_build.ldscript = ${common.ldscript_4m}
[env:esp32dev]
platform = espressif32
board = esp32dev
Expand Down