🛠 Gérer la flash 4 Mo : partitions et LittleFS

ESP8266 (ESP-12E module) · avance · Schéma : IMG-esp8266-esp-12e-avance-3-esp8266-partitions-littlefs.svg — Répartition de la flash 4 Mo d'un module ESP-12E : bootloader, deux slots de sketch OTA, NVS et partition LittleFS, avec le câble série branché pour le premier flashage.

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

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

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials