🛠 Gérer la flash 4 Mo : partitions et LittleFS
Objectives
Understand where data lives on the ESP-12E's 4 MB QSPI flash and use LittleFS, the modern file system of the esp8266 core (successor to SPIFFS, more robust against power cuts). The "Flash Size" and "Flash Mode" menus in Arduino IDE select the partition scheme burned at the beginning of the flash: bootloader, two sketch slots for OTA, NVS zone (Wi-Fi calibration + parameters), and file partition. An inconsistency between the module's actual flash and the chosen preset causes characteristic boot loops. You will learn to format, write, read back a configuration file, and display the used space.
Hardware
- 1 ESP8266-ESP-12E module with 4 MB flash (check the marking: ESP-12 clones sold as 12E sometimes have 512 KB or 1 MB)
- 1 USB-serial cable (the "LittleFS Data Upload" plugin or the ESP8266FS tool to push files from the IDE)
- Stable 3.3 V power supply ≥ 500 mA
- Arduino IDE + recent esp8266 core (LittleFS is integrated)
Complete Code
#include <ESP8266WiFi.h>
#include <LittleFS.h>
void setup {
Serial.begin(115200);
// Montage ; formatage seulement au premier lancement
if (!LittleFS.begin) {
Serial.println("LittleFS absent -> formatage (efface TOUT)");
if (!LittleFS.format) { Serial.println("Echec format : verifie Flash Size 4MB"); while (true) yield; }
LittleFS.begin;
}
// Etat de la partition
fs::FSInfo info;
LittleFS.info(info);
Serial.printf("Flash totale : %u Ko | LittleFS : %u Ko utilises sur %u Ko\n",
ESP.getFlashChipSize / 1024, info.usedBytes / 1024, info.totalBytes / 1024);
// Ecriture d'un fichier de configuration
{
File f = LittleFS.open("/config.txt", "w");
if (f) { f.println("ssid=mon-reseau"); f.println("seuil=42"); f.close; }
}
// Relecture ligne par ligne
File f = LittleFS.open("/config.txt", "r");
if (f) {
Serial.println("--- config.txt ---");
while (f.available) Serial.write(f.read);
f.close;
} else {
Serial.println("Fichier absent");
}
// Inventaire
File d = LittleFS.open("/");
if (d.isDirectory) {
File e = d.openNextFile;
while (e) { Serial.printf("%s (%u octets)\n", e.name, e.size); e = d.openNextFile; }
}
}
void loop { yield; }
Expected Verification
First launch: "LittleFS missing -> formatting" then the inventory displays config.txt with its two lines. The status line must indicate approximately 4096 KB of total flash (otherwise your preset is wrong). Relaunch without touching the code: no more formatting, the file is preserved — delete it from the monitor or reformat to recheck the cycle. Push a file with the LittleFS Data Upload tool: it appears in the inventory after a restart.
Common Errors
- Boot loop "ets ... load 0x40..." after upload: the declared flash size does not match the actual chip (4 MB preset on a 1 MB clone, or DOUT on a QIO module); test the other Flash Mode (DIO/DOUT) and check the marking.
- Believing that LittleFS and SPIFFS share the partition: they occupy the same area with different formats; use the conversion tool or format, otherwise begin fails.
- Writing at every loop: flash supports ~100,000 write cycles per sector; keep state in RAM and write only rarely (or via file rotation).
- Forgetting f.close: data remains in the buffer and the file appears empty after restart.
- Opening too many files simultaneously: the limit is low (~10), and each open file consumes precious RAM (80 KB of heap in total).
- Formatting to save time at every boot: premature flash wear is paid for with corruption a few months later.
Other tutorials for this board
Code examples for this board
clignotante.inoArduinobouton-led.inoArduinorespiration.inoArduinodht11.inoArduinoluxmetre.inoArduinoserveur-web.inoArduino