Skip to content

Commit

Permalink
#25 Add LittleFS support
Browse files Browse the repository at this point in the history
  • Loading branch information
lbussy committed Jul 25, 2020
1 parent a0cf8b8 commit e5525ac
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 43 deletions.
7 changes: 5 additions & 2 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ lib_deps =
https://github.com/lbussy/OneWire.git
https://github.com/lbussy/DS18B20.git
https://github.com/lbussy/Arduino-Log.git
https://github.com/lbussy/ESPAsyncWebServer.git
https://github.com/lbussy/ESPAsyncWebServer.git#littlefs
https://github.com/lbussy/AsyncWiFiManager.git
https://github.com/lbussy/ESPAsyncTCP
https://github.com/lbussy/asyncHTTPrequest.git
Expand All @@ -43,12 +43,15 @@ lib_deps =
build_type = release

[env:d1_mini]
board_build.filesystem = littlefs
upload_speed = ${common_env_data.upload_speed}
monitor_speed = ${common_env_data.monitor_speed}
framework = ${common_env_data.framework}
platform = ${common_env_data.platform}
build_unflags = ${common_env_data.build_unflags}
build_flags = ${common_env_data.build_flags}
build_flags =
${common_env_data.build_flags}
-D USE_LITTLEFS
extra_scripts = ${common_env_data.extra_scripts}
lib_deps = ${common_env_data.lib_deps}
build_type = ${common_env_data.build_type}
Expand Down
2 changes: 1 addition & 1 deletion src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ SOFTWARE. */

//////////////////////////////////////////////////////////////////////////
//
// SPIFFS URL
// File System URL
//
#ifndef SPIFFSURL
#define SPIFFSURL "http://www.brewbubbles.com/firmware/spiffs.bin"
Expand Down
16 changes: 8 additions & 8 deletions src/execota.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ void execfw() {

void execspiffs() {
if (config.dospiffs1) {
Log.notice(F("Rebooting a second time before SPIFFS OTA pull." CR));
Log.notice(F("Rebooting a second time before File System OTA pull." CR));
config.dospiffs1 = false;
config.dospiffs2 = true;
config.didupdate = false;
Expand All @@ -80,33 +80,33 @@ void execspiffs() {
ESP.restart();
_delay(1000);
} else if (config.dospiffs2) {
Log.notice(F("Starting the SPIFFS OTA pull." CR));
Log.notice(F("Starting the File System OTA pull." CR));

// Stop web server before OTA update - will restart on reset
stopWebServer();

ESPhttpUpdate.setLedPin(LED, LOW);
// "http://www.brewbubbles.com/firmware/spiffs.bin"
WiFiClient client;
t_httpUpdate_return ret = ESPhttpUpdate.updateSpiffs(client, F(SPIFFSURL), "");
t_httpUpdate_return ret = ESPhttpUpdate.updateFS(client, F(SPIFFSURL), "");

switch(ret) {
case HTTP_UPDATE_FAILED:
Log.error(F("HTTP SPIFFS OTA Update failed error (%d): %s" CR), ESPhttpUpdate.getLastError(), ESPhttpUpdate.getLastErrorString().c_str());
Log.error(F("HTTP File System OTA Update failed error (%d): %s" CR), ESPhttpUpdate.getLastError(), ESPhttpUpdate.getLastErrorString().c_str());
break;

case HTTP_UPDATE_NO_UPDATES:
Log.notice(F("HTTP SPIFFS OTA Update: No updates." CR));
Log.notice(F("HTTP File System OTA Update: No updates." CR));
break;

case HTTP_UPDATE_OK:
// Reset SPIFFS update flag
// Reset File System update flag
config.dospiffs1 = false;
config.dospiffs2 = false;
config.didupdate = true;
saveConfig(); // This not only saves the flags, it (re)saves the whole config after SPIFFS wipes it
saveConfig(); // This not only saves the flags, it (re)saves the whole config after File System wipes it
_delay(1000);
Log.notice(F("HTTP SPIFFS OTA Update complete, restarting." CR));
Log.notice(F("HTTP File System OTA Update complete, restarting." CR));
ESP.restart();
_delay(1000);
break;
Expand Down
18 changes: 9 additions & 9 deletions src/jsonconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ extern const size_t capacityDeserial = 3*JSON_OBJECT_SIZE(2) + 3*JSON_OBJECT_SIZ
extern const size_t capacitySerial = 3*JSON_OBJECT_SIZE(2) + 3*JSON_OBJECT_SIZE(3) + JSON_OBJECT_SIZE(10);

bool deleteConfigFile() {
if (!SPIFFS.begin()) {
if (!LittleFS.begin()) {
return false;
}
return SPIFFS.remove(filename);
return LittleFS.remove(filename);
}

bool loadConfig()
Expand All @@ -47,12 +47,12 @@ bool loadConfig()

bool loadFile()
{
if (!SPIFFS.begin()) {
if (!LittleFS.begin()) {
return false;
}
// Loads the configuration from a file on SPIFFS
File file = SPIFFS.open(filename, "r");
if (!SPIFFS.exists(filename) || !file) {
// Loads the configuration from a file on File System
File file = LittleFS.open(filename, "r");
if (!LittleFS.exists(filename) || !file) {
// File does not exist or unable to read file
} else {
// Existing configuration present
Expand All @@ -74,8 +74,8 @@ bool saveConfig()

bool saveFile()
{
// Saves the configuration to a file on SPIFFS
File file = SPIFFS.open(filename, "w");
// Saves the configuration to a file on File System
File file = LittleFS.open(filename, "w");
if (!file) {
file.close();
return false;
Expand Down Expand Up @@ -126,7 +126,7 @@ bool serializeConfig(Print &dst)
bool printFile()
{
// Prints the content of a file to the Serial
File file = SPIFFS.open(filename, "r");
File file = LittleFS.open(filename, "r");
if (!file)
return false;

Expand Down
2 changes: 1 addition & 1 deletion src/jsonconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ SOFTWARE. */

#include "config.h"
#include <ArduinoJson.h>
#include <FS.h>
#include <LittleFS.h>

struct ApConfig
{
Expand Down
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ void setup() {
doWiFi();
}

execspiffs(); // Check for pending SPIFFS update
execspiffs(); // Check for pending File System update
setClock(); // Set NTP Time
loadBpm() ; // Get last BPM reading if it was a controlled reboot
mdnssetup(); // Set up mDNS responder
Expand Down
14 changes: 7 additions & 7 deletions src/tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ void loadBpm() {
DynamicJsonDocument doc(capacity);
const char * bpmFileName = "lastBpm.json";

// Mount SPIFFS
if (!SPIFFS.begin()) {
Log.error(F("CONFIG: Failed to mount SPIFFS." CR));
// Mount File System
if (!LittleFS.begin()) {
Log.error(F("CONFIG: Failed to mount File System." CR));
return;
}

// Open file for reading
File file = SPIFFS.open(bpmFileName, "r");
if (!SPIFFS.exists(bpmFileName) || !file) {
File file = LittleFS.open(bpmFileName, "r");
if (!LittleFS.exists(bpmFileName) || !file) {
Log.notice(F("No lastBpm available." CR));
} else {
// Parse the JSON object in the file
Expand All @@ -68,7 +68,7 @@ void loadBpm() {
Log.notice(F("Loaded lastBpm." CR));
}
// Delete file
SPIFFS.remove(bpmFileName);
LittleFS.remove(bpmFileName);
}
}

Expand All @@ -80,7 +80,7 @@ void saveBpm() {
doc["lastBpm"] = bubbles.getAvgBpm();

// Open file for writing
File file = SPIFFS.open(bpmFileName, "w");
File file = LittleFS.open(bpmFileName, "w");
if (!file) {
Log.error(F("Failed to open lastBpm file." CR));
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ SOFTWARE. */
#define _TOOLS_H

#include "bubbles.h"
#include <FS.h>
#include <LittleFS.h>
#include <ArduinoLog.h>
#include <ArduinoJson.h>
#include <EEPROM.h>
Expand Down
24 changes: 12 additions & 12 deletions src/webserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ void setRegPageAliases()
{
// Regular page aliases

server.serveStatic("/", SPIFFS, "/").setDefaultFile("index.htm").setCacheControl("max-age=600");
server.serveStatic("/index.htm", SPIFFS, "/").setDefaultFile("index.htm").setCacheControl("max-age=600");
server.serveStatic("/about/", SPIFFS, "/").setDefaultFile("about.htm").setCacheControl("max-age=600");
server.serveStatic("/help/", SPIFFS, "/").setDefaultFile("help.htm").setCacheControl("max-age=600");
server.serveStatic("/ota/", SPIFFS, "/").setDefaultFile("ota.htm").setCacheControl("max-age=600");
server.serveStatic("/ota2/", SPIFFS, "/").setDefaultFile("ota2.htm").setCacheControl("max-age=600");
server.serveStatic("/settings/", SPIFFS, "/").setDefaultFile("settings.htm").setCacheControl("max-age=600");
server.serveStatic("/wifi/", SPIFFS, "/").setDefaultFile("wifi.htm").setCacheControl("max-age=600");
server.serveStatic("/", LittleFS, "/").setDefaultFile("index.htm").setCacheControl("max-age=600");
server.serveStatic("/index.htm", LittleFS, "/").setDefaultFile("index.htm").setCacheControl("max-age=600");
server.serveStatic("/about/", LittleFS, "/").setDefaultFile("about.htm").setCacheControl("max-age=600");
server.serveStatic("/help/", LittleFS, "/").setDefaultFile("help.htm").setCacheControl("max-age=600");
server.serveStatic("/ota/", LittleFS, "/").setDefaultFile("ota.htm").setCacheControl("max-age=600");
server.serveStatic("/ota2/", LittleFS, "/").setDefaultFile("ota2.htm").setCacheControl("max-age=600");
server.serveStatic("/settings/", LittleFS, "/").setDefaultFile("settings.htm").setCacheControl("max-age=600");
server.serveStatic("/wifi/", LittleFS, "/").setDefaultFile("wifi.htm").setCacheControl("max-age=600");
}

void setActionPageHandlers()
Expand All @@ -72,14 +72,14 @@ void setActionPageHandlers()

server.on("/wifi2/", HTTP_GET, [](AsyncWebServerRequest *request) {
Log.verbose(F("Processing /wifi2/." CR));
request->send(SPIFFS, "/wifi2.htm");
request->send(LittleFS, "/wifi2.htm");
resetWifi(); // Wipe settings, reset controller
});

server.on("/reset/", HTTP_GET, [](AsyncWebServerRequest *request) {
Log.verbose(F("Processing /reset/." CR));
// Redirect to Reset page
request->send(SPIFFS, "/reset.htm");
request->send(LittleFS, "/reset.htm");
setDoReset();
});

Expand Down Expand Up @@ -510,15 +510,15 @@ void setSettingsAliases()
config.brewersfriend.freq = bffreq;
}

// Parse SPIFFS OTA update choice
// Parse File System OTA update choice
JsonVariant dospiffs1 = doc["dospiffs1"];
if ((!dospiffs1.isNull()) && (!dospiffs1 == config.dospiffs1))
{
updated = true;
config.dospiffs1 = dospiffs1;
}

// Parse SPIFFS OTA update choice
// Parse File System OTA update choice
JsonVariant dospiffs2 = doc["dospiffs2"];
if ((!dospiffs2.isNull()) && (!dospiffs2 == config.dospiffs2))
{
Expand Down
6 changes: 5 additions & 1 deletion src/webserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ SOFTWARE. */
#ifndef _WEBSERVER_H
#define _WEBSERVER_H

#ifndef USE_LITTLEFS
#define USE_LITTLEFS
#endif

#include "wifi.h"
#include "execota.h"
#include "bubbles.h"
Expand All @@ -34,7 +38,7 @@ SOFTWARE. */
#include <ArduinoLog.h>
#include <ArduinoJson.h>
#include <AsyncJson.h>
#include <FS.h>
#include <LittleFS.h>
#include <ESPAsyncWebServer.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266mDNS.h>
Expand Down

0 comments on commit e5525ac

Please sign in to comment.