8000 Migrate to LittleFS under ESP8266 · thisiscam/esp8266-react@c16f769 · GitHub
[go: up one dir, main page]

Skip to content

Commit c16f769

Browse files
authored
Migrate to LittleFS under ESP8266
Make ESP8266 use LittleFS instead of deprecated SPIFFS Make framework use the correct filesystem automatically and handle the call the FS.begin() Change default MQTT keepalive to 60 seconds Fix lodash security issue
1 parent 6ef5df2 commit c16f769

13 files changed

+52
-46
lines changed

README.md

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,13 @@ platformio run -t upload
7676

7777
The interface has been configured with create-react-app and react-app-rewired so the build can customized for the target device. The large artefacts are gzipped and source maps and service worker are excluded from the production build. This reduces the production build to around ~150k, which easily fits on the device.
7878

79-
The interface will be automatically built by PlatformIO before it builds the firmware. The project can be configured to serve the interface from either PROGMEM or SPIFFS as your project requires. The default configuration is to serve the content from PROGMEM, serving from SPIFFS requires an additional upload step which is documented below.
79+
The interface will be automatically built by PlatformIO before it builds the firmware. The project can be configured to serve the interface from either PROGMEM or the filesystem as your project requires. The default configuration is to serve the content from PROGMEM, serving from the filesystem requires an additional upload step which is [documented below](#serving-the-interface-from-the-filesystem).
8080

8181
#### Serving the interface from PROGMEM
8282

8383
By default, the project is configured to serve the interface from PROGMEM.
8484

85-
> **Tip**: You do not need to upload a file system image unless you configure the framework to [serve the interface from SPIFFS](#serving-the-interface-from-spiffs).
85+
> **Tip**: You do not need to upload a file system image unless you configure the framework to [serve the interface from the filesystem](#serving-the-interface-from-the-filesystem).
8686
8787
The interface will consume ~150k of program space which can be problematic if you already have a large binary artefact or if you have added large dependencies to the interface. The ESP32 binaries are fairly large in there simplest form so the addition of the interface resources requires us to use special partitioning for the ESP32.
8888

@@ -96,9 +96,9 @@ platform = espressif32
9696
board = node32s
9797
```
9898

99-
#### Serving the interface from SPIFFS
99+
#### Serving the interface from the filesystem
100100

101-
If you choose to serve the interface from SPIFFS you will need to change the default configuration and upload the file system image manually.
101+
If you choose to serve the interface from the filesystem you will need to change the default configuration and upload the file system image manually.
102102

103103
Disable `-D PROGMEM_WWW build` flag in ['platformio.ini'](platformio.ini) and re-build the firmware. The build process will now copy the compiled interface to the `data/` directory and it may be uploaded to the device by pressing the "Upload File System image" button:
104104

@@ -340,7 +340,7 @@ The following code creates the web server and esp8266React framework:
340340

341341
```cpp
342342
AsyncWebServer server(80);
343-
ESP8266React esp8266React(&server, &SPIFFS);
343+
ESP8266React esp8266React(&server);
344344
```
345345
346346
Now in the `setup()` function the initialization is performed:
@@ -350,13 +350,6 @@ void setup() {
350350
// start serial and filesystem
351351
Serial.begin(SERIAL_BAUD_RATE);
352352
353-
// start the file system (must be done before starting the framework)
354-
#ifdef ESP32
355-
SPIFFS.begin(true);
356-
#elif defined(ESP8266)
357-
SPIFFS.begin();
358-
#endif
359-
360353
// start the framework and demo project
361354
esp8266React.begin();
362355
@@ -615,6 +608,7 @@ The framework supplies access to various features via getter functions:
615608

616609
SettingsService | Description
617610
---------------------------- | ----------------------------------------------
611+
getFS() | The filesystem used by the framework
618612
getSecurityManager() | The security manager - detailed above
619613
getSecuritySettingsService() | Configures the users and other security settings
620614
getWiFiSettingsService() | Configures and manages the WiFi network connection

factory_settings.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ build_flags =
3838
-D FACTORY_MQTT_PASSWORD=\"\"
3939
; if unspecified the devices hardware ID will be used
4040
;-D FACTORY_MQTT_CLIENT_ID=\"esp-react\"
41-
-D FACTORY_MQTT_KEEP_ALIVE=16
41+
-D FACTORY_MQTT_KEEP_ALIVE=60
4242
-D FACTORY_MQTT_CLEAN_SESSION=true
4343
-D FACTORY_MQTT_MAX_TOPIC_LENGTH=128
4444

interface/config-overrides.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const fs = require('fs');
99

1010
module.exports = function override(config, env) {
1111
if (env === "production") {
12-
// rename the ouput file, we need it's path to be short, for SPIFFS
12+
// rename the ouput file, we need it's path to be short, for embedded FS
1313
config.output.filename = 'js/[id].[chunkhash:4].js';
1414
config.output.chunkFilename = 'js/[id].[chunkhash:4].js';
1515

interface/package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

interface/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"@material-ui/core": "^4.9.8",
77
"@material-ui/icons": "^4.9.1",
88
"@types/jwt-decode": "^2.2.1",
9-
"@types/lodash": "^4.14.149",
9+
"@types/lodash": "^4.14.157",
1010
"@types/node": "^12.12.32",
1111
"@types/react": "^16.9.27",
1212
"@types/react-dom": "^16.9.5",
@@ -15,7 +15,7 @@
1515
"@types/react-router-dom": "^5.1.3",
1616
"compression-webpack-plugin": "^4.0.0",
1717
"jwt-decode": "^2.2.0",
18-
"lodash": "^4.17.15",
18+
"lodash": "^4.17.19",
1919
"mime-types": "^2.1.25",
2020
"moment": "^2.26.0",
2121
"notistack": "^0.9.17",

lib/framework/ESP8266React.cpp

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
11
#include <ESP8266React.h>
22

3-
ESP8266React::ESP8266React(AsyncWebServer* server, FS* fs) :
3+
ESP8266React::ESP8266React(AsyncWebServer* server) :
44
_featureService(server),
5-
_securitySettingsService(server, fs),
6-
_wifiSettingsService(server, fs, &_securitySettingsService),
5+
_securitySettingsService(server, &ESPFS),
6+
_wifiSettingsService(server, &ESPFS, &_securitySettingsService),
77
_wifiScanner(server, &_securitySettingsService),
88
_wifiStatus(server, &_securitySettingsService),
9-
_apSettingsService(server, fs, &_securitySettingsService),
9+
_apSettingsService(server, &ESPFS, &_securitySettingsService),
1010
_apStatus(server, &_securitySettingsService, &_apSettingsService),
1111
#if FT_ENABLED(FT_NTP)
12-
_ntpSettingsService(server, fs, &_securitySettingsService),
12+
_ntpSettingsService(server, &ESPFS, &_securitySettingsService),
1313
_ntpStatus(server, &_securitySettingsService),
1414
#endif
1515
#if FT_ENABLED(FT_OTA)
16-
_otaSettingsService(server, fs, &_securitySettingsService),
16+
_otaSettingsService(server, &ESPFS, &_securitySettingsService),
1717
#endif
1818
#if FT_ENABLED(FT_UPLOAD_FIRMWARE)
1919
_uploadFirmwareService(server, &_securitySettingsService),
2020
#endif
2121
#if FT_ENABLED(FT_MQTT)
22-
_mqttSettingsService(server, fs, &_securitySettingsService),
22+
_mqttSettingsService(server, &ESPFS, &_securitySettingsService),
2323
_mqttStatus(server, &_mqttSettingsService, &_securitySettingsService),
2424
#endif
2525
#if FT_ENABLED(FT_SECURITY)
2626
_authenticationService(server, &_securitySettingsService),
2727
#endif
2828
_restartService(server, &_securitySettingsService),
29-
_factoryResetService(server, fs, &_securitySettingsService),
29+
_factoryResetService(server, &ESPFS, &_securitySettingsService),
3030
_systemStatus(server, &_securitySettingsService) {
3131
#ifdef PROGMEM_WWW
3232
// Serve static resources from PROGMEM
@@ -63,7 +63,7 @@ ESP8266React::ESP8266React(AsyncWebServer* server, FS* fs) :
6363
// OPTIONS get a straight up 200 response
6464
server->onNotFound([](AsyncWebServerRequest* request) {
6565
if (request->method() == HTTP_GET) {
66-
request->send(SPIFFS, "/www/index.html");
66+
request->send(ESPFS, "/www/index.html");
6767
} else if (request->method() == HTTP_OPTIONS) {
6868
request->send(200);
6969
} else {
@@ -81,6 +81,11 @@ ESP8266React::ESP8266React(AsyncWebServer* server, FS* fs) :
8181
}
8282

8383
void ESP8266React::begin() {
84+
#ifdef ESP32
85+
ESPFS.begin(true);
86+
#elif defined(ESP8266)
87+
ESPFS.begin();
88+
#endif
8489
_wifiSettingsService.begin();
8590
_apSettingsService.begin();
8691
#if FT_ENABLED(FT_NTP)

lib/framework/ESP8266React.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,23 @@
2828
#include <WiFiScanner.h>
2929
#include <WiFiSettingsService.h>
3030
#include <WiFiStatus.h>
31+
#include <ESPFS.h>
3132

3233
#ifdef PROGMEM_WWW
3334
#include <WWWData.h>
3435
#endif
3536

3637
class ESP8266React {
3738
public:
38-
ESP8266React(AsyncWebServer* server, FS* fs);
39+
ESP8266React(AsyncWebServer* server);
3940

4041
void begin();
4142
void loop();
4243

44+
FS* getFS() {
45+
return &ESPFS;
46+
}
47+
4348
SecurityManager* getSecurityManager() {
4449
return &_securitySettingsService;
4550
}

lib/framework/ESPFS.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#ifdef ESP32
2+
#include <SPIFFS.h>
3+
#define ESPFS SPIFFS
4+
#elif defined(ESP8266)
5+
#include <LittleFS.h>
6+
#define ESPFS LittleFS
7+
#endif

lib/framework/FactoryResetService.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ void FactoryResetService::handleRequest(AsyncWebServerRequest* request) {
1515
}
1616

1717
/**
18-
* Delete function assumes that all files are stored flat, within the config directory
18+
* Delete function assumes that all files are stored flat, within the config directory.
1919
*/
2020
void FactoryResetService::factoryReset() {
2121
#ifdef ESP32
@@ -27,7 +27,10 @@ void FactoryResetService::factoryReset() {
2727
#elif defined(ESP8266)
2828
Dir configDirectory = fs->openDir(FS_CONFIG_DIRECTORY);
2929
while (configDirectory.next()) {
30-
fs->remove(configDirectory.fileName());
30+
String path = FS_CONFIG_DIRECTORY;
31+
path.concat("/");
32+
path.concat(configDirectory.fileName());
33+
fs->remove(path);
3134
}
3235
#endif
3336
RestartService::restartNow();

lib/framework/SystemStatus.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ void SystemStatus::systemStatus(AsyncWebServerRequest* request) {
3131
// TODO - Ideally this class will take an *FS and extract the file system information from there.
3232
// ESP8266 and ESP32 do not have feature parity in FS.h which currently makes that difficult.
3333
#ifdef ESP32
34-
root["fs_total"] = SPIFFS.totalBytes();
35-
root["fs_used"] = SPIFFS.usedBytes();
34+
root["fs_total"] = ESPFS.totalBytes();
35+
root["fs_used"] = ESPFS.usedBytes();
3636
#elif defined(ESP8266)
3737
FSInfo fs_info;
38-
SPIFFS.info(fs_info);
38+
ESPFS.info(fs_info);
3939
root["fs_total"] = fs_info.totalBytes;
4040
root["fs_used"] = fs_info.usedBytes;
4141
#endif

lib/framework/SystemStatus.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,16 @@
44
#ifdef ESP32
55
#include <WiFi.h>
66
#include <AsyncTCP.h>
7-
#include <SPIFFS.h>
87
#elif defined(ESP8266)
98
#include <ESP8266WiFi.h>
109
#include <ESPAsyncTCP.h>
11-
#include <FS.h>
1210
#endif
1311

1412
#include <ArduinoJson.h>
1513
#include <AsyncJson.h>
1614
#include <ESPAsyncWebServer.h>
1715
#include <SecurityManager.h>
16+
#include <ESPFS.h>
1817

1918
#define MAX_ESP_STATUS_SIZE 1024
2019
#define SYSTEM_STATUS_SERVICE_PATH "/rest/systemStatus"

platformio.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ lib_deps =
4040
platform = espressif8266
4141
board = esp12e
4242
board_build.f_cpu = 160000000L
43+
board_build.filesystem = littlefs
4344

4445
[env:node32s]
4546
; Comment out min_spiffs.csv setting if disabling PROGMEM_WWW with ESP32

src/main.cpp

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
#include <ESP8266React.h>
22
#include <LightMqttSettingsService.h>
33
#include <LightStateService.h>
4-
#include <FS.h>
54

65
#define SERIAL_BAUD_RATE 115200
76

87
AsyncWebServer server(80);
9-
ESP8266React esp8266React(&server, &SPIFFS);
8+
ESP8266React esp8266React(&server);
109
LightMqttSettingsService lightMqttSettingsService =
11-
LightMqttSettingsService(&server, &SPIFFS, esp8266React.getSecurityManager());
10+
LightMqttSettingsService(&server, esp8266React.getFS(), esp8266React.getSecurityManager());
1211
LightStateService lightStateService = LightStateService(&server,
1312
esp8266React.getSecurityManager(),
1413
esp8266React.getMqttClient(),
@@ -18,13 +17,6 @@ void setup() {
1817
// start serial and filesystem
1918
Serial.begin(SERIAL_BAUD_RATE);
2019

21-
// start the file system (must be done before starting the framework)
22-
#ifdef ESP32
23-
SPIFFS.begin(true);
24-
#elif defined(ESP8266)
25-
SPIFFS.begin();
26-
#endif
27-
2820
// start the framework and demo project
2921
esp8266React.begin();
3022

0 commit comments

Comments
 (0)
0