Changed from SPIFFS to LittleFS

This commit is contained in:
Gunnar Skjold 2021-11-18 19:40:20 +01:00
parent 5c38d1cf3e
commit 933246eae8
5 changed files with 82 additions and 76 deletions

View File

@ -20,7 +20,7 @@ build_flags =
[env:esp32]
platform = espressif32@3.3.2
platform = https://github.com/platformio/platform-espressif32.git#feature/arduino-upstream
board = esp32dev
framework = arduino
lib_deps = ${common.lib_deps}

View File

@ -18,8 +18,9 @@
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <ESPmDNS.h>
#include "SPIFFS.h"
#include "Update.h"
#endif
#include "LittleFS.h"
#endif

View File

@ -202,18 +202,18 @@ void setup() {
bool hasFs = false;
#if defined(ESP32)
debugD("ESP32 SPIFFS");
hasFs = SPIFFS.begin(true);
debugD(" size: %d", SPIFFS.totalBytes());
debugD("ESP32 LittleFS");
hasFs = LittleFS.begin(true);
debugD(" size: %d", LittleFS.totalBytes());
#else
debugD("ESP8266 SPIFFS");
hasFs = SPIFFS.begin();
debugD("ESP8266 LittleFS");
hasFs = LittleFS.begin();
#endif
delay(1);
if(hasFs) {
bool flashed = false;
if(SPIFFS.exists(FILE_FIRMWARE)) {
if(LittleFS.exists(FILE_FIRMWARE)) {
if(Debug.isActive(RemoteDebug::INFO)) debugI("Found firmware");
#if defined(ESP8266)
WiFi.setSleepMode(WIFI_LIGHT_SLEEP);
@ -232,7 +232,7 @@ void setup() {
}
debugI(" flashing");
File firmwareFile = SPIFFS.open(FILE_FIRMWARE, "r");
File firmwareFile = LittleFS.open(FILE_FIRMWARE, "r");
debugD(" firmware size: %d", firmwareFile.size());
uint32_t maxSketchSpace = (ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000;
debugD(" available: %d", maxSketchSpace);
@ -250,9 +250,9 @@ void setup() {
flashed = Update.end(true);
}
firmwareFile.close();
SPIFFS.remove(FILE_FIRMWARE);
LittleFS.remove(FILE_FIRMWARE);
}
SPIFFS.end();
LittleFS.end();
if(flashed) {
if(Debug.isActive(RemoteDebug::INFO)) {
debugI("Firmware update complete, restarting");
@ -776,16 +776,20 @@ void WiFi_connect() {
WiFi.config(ip, gw, sn, dns1, dns2);
} else {
#if defined(ESP32)
WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE); // Workaround to make DHCP hostname work for ESP32. See: https://github.com/espressif/arduino-esp32/issues/2537
// Changed from INADDR_NONE to INADDR_ANY for last ESP32-Arduino version
WiFi.config(INADDR_ANY, INADDR_ANY, INADDR_ANY); // Workaround to make DHCP hostname work for ESP32. See: https://github.com/espressif/arduino-esp32/issues/2537
#endif
}
#if defined(ESP8266)
if(strlen(wifi.hostname) > 0) {
#if defined(ESP8266)
WiFi.hostname(wifi.hostname);
#elif defined(ESP32)
WiFi.setHostname(wifi.hostname);
#endif
}
#endif
#if defined(ESP32)
if(strlen(wifi.hostname) > 0) {
WiFi.setHostname(wifi.hostname);
}
#endif
WiFi.begin(wifi.ssid, wifi.psk);
yield();
}
@ -849,15 +853,15 @@ void MQTT_connect() {
secureClient->setBufferSizes(512, 512);
#endif
if(SPIFFS.begin()) {
if(LittleFS.begin()) {
char *ca = NULL;
char *cert = NULL;
char *key = NULL;
File file;
if(SPIFFS.exists(FILE_MQTT_CA)) {
if(LittleFS.exists(FILE_MQTT_CA)) {
debugI("Found MQTT CA file");
file = SPIFFS.open(FILE_MQTT_CA, "r");
file = LittleFS.open(FILE_MQTT_CA, "r");
#if defined(ESP8266)
char caStr[MAX_PEM_SIZE];
file.readBytes(caStr, file.size());
@ -868,28 +872,28 @@ void MQTT_connect() {
#endif
}
if(SPIFFS.exists(FILE_MQTT_CERT) && SPIFFS.exists(FILE_MQTT_KEY)) {
if(LittleFS.exists(FILE_MQTT_CERT) && LittleFS.exists(FILE_MQTT_KEY)) {
#if defined(ESP8266)
char certStr[MAX_PEM_SIZE];
file = SPIFFS.open(FILE_MQTT_CERT, "r");
file = LittleFS.open(FILE_MQTT_CERT, "r");
file.readBytes(certStr, file.size());
BearSSL::X509List *serverCertList = new BearSSL::X509List(certStr);
char keyStr[MAX_PEM_SIZE];
file = SPIFFS.open(FILE_MQTT_KEY, "r");
file = LittleFS.open(FILE_MQTT_KEY, "r");
file.readBytes(keyStr, file.size());
BearSSL::PrivateKey *serverPrivKey = new BearSSL::PrivateKey(keyStr);
secureClient->setClientRSACert(serverCertList, serverPrivKey);
#elif defined(ESP32)
debugI("Found MQTT certificate file");
file = SPIFFS.open(FILE_MQTT_CERT, "r");
file = LittleFS.open(FILE_MQTT_CERT, "r");
secureClient->loadCertificate(file, file.size());
debugI("Found MQTT key file");
file = SPIFFS.open(FILE_MQTT_KEY, "r");
file = LittleFS.open(FILE_MQTT_KEY, "r");
secureClient->loadPrivateKey(file, file.size());
#endif
}
SPIFFS.end();
LittleFS.end();
}
client = secureClient;
} else {
@ -926,14 +930,14 @@ void MQTT_connect() {
}
} else {
if (Debug.isActive(RemoteDebug::ERROR)) {
debugE("Failed to connect to MQTT");
#if defined(ESP8266)
if(secureClient) {
char buf[64];
secureClient->getLastSSLError(buf,64);
Debug.println(buf);
}
#endif
debugE("Failed to connect to MQTT: %d", mqtt.lastError());
#if defined(ESP8266)
if(secureClient) {
char buf[64];
secureClient->getLastSSLError(buf,64);
Debug.println(buf);
}
#endif
}
}
yield();

View File

@ -525,14 +525,14 @@ void AmsWebServer::configMqttHtml() {
html.replace("{s}", mqtt.ssl ? "checked" : "");
if(SPIFFS.begin()) {
html.replace("{dcu}", SPIFFS.exists(FILE_MQTT_CA) ? "none" : "");
html.replace("{dcf}", SPIFFS.exists(FILE_MQTT_CA) ? "" : "none");
html.replace("{deu}", SPIFFS.exists(FILE_MQTT_CERT) ? "none" : "");
html.replace("{def}", SPIFFS.exists(FILE_MQTT_CERT) ? "" : "none");
html.replace("{dku}", SPIFFS.exists(FILE_MQTT_KEY) ? "none" : "");
html.replace("{dkf}", SPIFFS.exists(FILE_MQTT_KEY) ? "" : "none");
SPIFFS.end();
if(LittleFS.begin()) {
html.replace("{dcu}", LittleFS.exists(FILE_MQTT_CA) ? "none" : "");
html.replace("{dcf}", LittleFS.exists(FILE_MQTT_CA) ? "" : "none");
html.replace("{deu}", LittleFS.exists(FILE_MQTT_CERT) ? "none" : "");
html.replace("{def}", LittleFS.exists(FILE_MQTT_CERT) ? "" : "none");
html.replace("{dku}", LittleFS.exists(FILE_MQTT_KEY) ? "none" : "");
html.replace("{dkf}", LittleFS.exists(FILE_MQTT_KEY) ? "" : "none");
LittleFS.end();
} else {
html.replace("{dcu}", "");
html.replace("{dcf}", "none");
@ -1273,9 +1273,9 @@ void AmsWebServer::uploadFile(const char* path) {
printE("Upload already in progress");
String html = "<html><body><h1>Upload already in progress!</h1></body></html>";
server.send(500, "text/html", html);
} else if (!SPIFFS.begin()) {
printE("An Error has occurred while mounting SPIFFS");
String html = "<html><body><h1>Unable to mount SPIFFS!</h1></body></html>";
} else if (!LittleFS.begin()) {
printE("An Error has occurred while mounting LittleFS");
String html = "<html><body><h1>Unable to mount LittleFS!</h1></body></html>";
server.send(500, "text/html", html);
} else {
uploading = true;
@ -1285,12 +1285,12 @@ void AmsWebServer::uploadFile(const char* path) {
#if defined(ESP32)
if(debugger->isActive(RemoteDebug::DEBUG)) {
debugger->printf("handleFileUpload Free heap: %lu\n", ESP.getFreeHeap());
debugger->printf("handleFileUpload SPIFFS size: %lu\n", SPIFFS.totalBytes());
debugger->printf("handleFileUpload SPIFFS used: %lu\n", SPIFFS.usedBytes());
debugger->printf("handleFileUpload SPIFFS free: %lu\n", SPIFFS.totalBytes()-SPIFFS.usedBytes());
debugger->printf("handleFileUpload LittleFS size: %lu\n", LittleFS.totalBytes());
debugger->printf("handleFileUpload LittleFS used: %lu\n", LittleFS.usedBytes());
debugger->printf("handleFileUpload LittleFS free: %lu\n", LittleFS.totalBytes()-LittleFS.usedBytes());
}
#endif
file = SPIFFS.open(path, "w");
file = LittleFS.open(path, "w");
if(debugger->isActive(RemoteDebug::DEBUG)) {
debugger->printf("handleFileUpload Open file and write: %lu\n", upload.currentSize);
}
@ -1313,16 +1313,16 @@ void AmsWebServer::uploadFile(const char* path) {
#if defined(ESP32)
if(debugger->isActive(RemoteDebug::DEBUG)) {
debugger->printf("handleFileUpload Free heap: %lu\n", ESP.getFreeHeap());
debugger->printf("handleFileUpload SPIFFS size: %lu\n", SPIFFS.totalBytes());
debugger->printf("handleFileUpload SPIFFS used: %lu\n", SPIFFS.usedBytes());
debugger->printf("handleFileUpload SPIFFS free: %lu\n", SPIFFS.totalBytes()-SPIFFS.usedBytes());
debugger->printf("handleFileUpload LittleFS size: %lu\n", LittleFS.totalBytes());
debugger->printf("handleFileUpload LittleFS used: %lu\n", LittleFS.usedBytes());
debugger->printf("handleFileUpload LittleFS free: %lu\n", LittleFS.totalBytes()-LittleFS.usedBytes());
}
#endif
file.flush();
file.close();
SPIFFS.remove(path);
SPIFFS.end();
LittleFS.remove(path);
LittleFS.end();
printE("An Error has occurred while writing file");
String html = "<html><body><h1>Unable to write file!</h1></body></html>";
@ -1333,13 +1333,13 @@ void AmsWebServer::uploadFile(const char* path) {
if(file) {
file.flush();
file.close();
file = SPIFFS.open(path, "r");
file = LittleFS.open(path, "r");
if(debugger->isActive(RemoteDebug::DEBUG)) {
debugger->printf("handleFileUpload Size: %lu\n", upload.totalSize);
debugger->printf("handleFileUpload File size: %lu\n", file.size());
}
file.close();
SPIFFS.end();
LittleFS.end();
} else {
server.send(500, "text/plain", "500: couldn't create file");
}
@ -1347,9 +1347,9 @@ void AmsWebServer::uploadFile(const char* path) {
}
void AmsWebServer::deleteFile(const char* path) {
if(SPIFFS.begin()) {
SPIFFS.remove(path);
SPIFFS.end();
if(LittleFS.begin()) {
LittleFS.remove(path);
LittleFS.end();
}
}
@ -1409,17 +1409,17 @@ void AmsWebServer::firmwareDownload() {
int status = https.GET();
if(status == HTTP_CODE_OK) {
printD("Received OK from server");
if(SPIFFS.begin()) {
printI("Downloading firmware to SPIFFS");
file = SPIFFS.open(FILE_FIRMWARE, "w");
if(LittleFS.begin()) {
printI("Downloading firmware to LittleFS");
file = LittleFS.open(FILE_FIRMWARE, "w");
int len = https.writeToStream(&file);
file.close();
SPIFFS.end();
LittleFS.end();
performRestart = true;
server.sendHeader("Location","/restart-wait");
server.send(303);
} else {
printE("Unable to open SPIFFS for writing");
printE("Unable to open LittleFS for writing");
server.sendHeader("Location","/");
server.send(303);
}
@ -1506,7 +1506,7 @@ void AmsWebServer::restartWaitHtml() {
yield();
if(performRestart) {
SPIFFS.end();
LittleFS.end();
printI("Rebooting");
delay(1000);
#if defined(ESP8266)
@ -1549,13 +1549,13 @@ void AmsWebServer::mqttCa() {
if(!checkSecurity(1))
return;
if(SPIFFS.begin()) {
if(SPIFFS.exists(FILE_MQTT_CA)) {
if(LittleFS.begin()) {
if(LittleFS.exists(FILE_MQTT_CA)) {
deleteHtml("CA file", "/mqtt-ca/delete", "mqtt");
} else {
uploadHtml("CA file", "/mqtt-ca", "mqtt");
}
SPIFFS.end();
LittleFS.end();
} else {
server.sendHeader("Location","/config-mqtt");
server.send(303);
@ -1603,13 +1603,13 @@ void AmsWebServer::mqttCert() {
if(!checkSecurity(1))
return;
if(SPIFFS.begin()) {
if(SPIFFS.exists(FILE_MQTT_CERT)) {
if(LittleFS.begin()) {
if(LittleFS.exists(FILE_MQTT_CERT)) {
deleteHtml("Certificate", "/mqtt-cert/delete", "mqtt");
} else {
uploadHtml("Certificate", "/mqtt-cert", "mqtt");
}
SPIFFS.end();
LittleFS.end();
} else {
server.sendHeader("Location","/config-mqtt");
server.send(303);
@ -1656,13 +1656,13 @@ void AmsWebServer::mqttKey() {
if(!checkSecurity(1))
return;
if(SPIFFS.begin()) {
if(SPIFFS.exists(FILE_MQTT_KEY)) {
if(LittleFS.begin()) {
if(LittleFS.exists(FILE_MQTT_KEY)) {
deleteHtml("Private key", "/mqtt-key/delete", "mqtt");
} else {
uploadHtml("Private key", "/mqtt-key", "mqtt");
}
SPIFFS.end();
LittleFS.end();
} else {
server.sendHeader("Location","/config-mqtt");
server.send(303);
@ -1721,8 +1721,8 @@ void AmsWebServer::factoryResetPost() {
printD("Performing factory reset");
if(server.hasArg("perform") && server.arg("perform") == "true") {
printD("Formatting SPIFFS");
SPIFFS.format();
printD("Formatting LittleFS");
LittleFS.format();
printD("Clearing configuration");
config->clear();
printD("Setting restart flag and redirecting");

View File

@ -20,11 +20,12 @@
#include <WiFi.h>
#include <WebServer.h>
#include <HTTPClient.h>
#include "SPIFFS.h"
#else
#warning "Unsupported board type"
#endif
#include "LittleFS.h"
class AmsWebServer {
public:
AmsWebServer(RemoteDebug* Debug, HwTools* hw);