🛠 Journaliser un capteur dans LittleFS

ESP32 (original, ESP32-WROOM-32 class) · intermediaire · Schéma : IMG-esp32-intermediaire-3-littlefs-logs-capteur.svg — Puce flash 4 Mo de l'ESP32 découpée en partitions, un fichier CSV de logs s'ajoutant dans la partition LittleFS

Objectives

Store measurements on the ESP32's internal 4 MB flash without an SD card, using the LittleFS file system (the official successor to SPIFFS, which is now deprecated). You will learn how to format, create a folder tree, open a file in append mode, and write a CSV log.

Hardware

Complete code

#include <LittleFS.h>

void ecrireLigne(const String& ligne) {
  File f = LittleFS.open("/logs/capteur.csv", "a");
  if (!f) { Serial.println(F("Ouverture en echec")); return; }
  f.println(ligne);
  f.close;
}

void setup {
  Serial.begin(115200);
  if (!LittleFS.begin(true)) {   // true = formater si la partition est vide
    Serial.println(F("Formatage LittleFS..."));
    LittleFS.begin(true);
  }
  if (!LittleFS.exists("/logs")) LittleFS.mkdir("/logs");
}

void loop {
  float t = 20.0f + 5.0f * sin(millis / 30000.0f); // capteur simule
  ecrireLigne(String(millis / 1000) + "," + String(t, 2));
  File f = LittleFS.open("/logs/capteur.csv", "r");
  String derniere = f.readStringUntil('\n');
  f.close;
  Serial.printf("Derniere ligne du CSV : %s\n", derniere.c_str);
  delay(5000);
}

Expected verification

The console displays the first line of the CSV while the file grows in append mode; after several minutes, /logs/capteur.csv contains a "seconds,temperature" line every 5 seconds. The content survives a power cut.

Common errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials