Flash has limited erase cycles and writing it costs time and energy. A sensor that wakes every minute and appends one reading to a file, or updates an NVS key each time, wears the same flash area over and over and spends battery on it.
Where to put what
| Data | Put it in | Why |
|---|---|---|
| Readings that only need to survive deep sleep | RTC slow memory (RTC_DATA_ATTR) |
real RAM: no wear, cheap to write; lost on power loss |
| Settings that change rarely (Wi-Fi, calibration) | NVS / Preferences | built for many small, rarely changed values |
| Logs and files | SPIFFS / LittleFS, written in batches | file system with wear levelling |
| Large or long-term logs | external storage or upload to a server | keeps flash writes low |
Pattern: RTC ring buffer, flush in batches
- On each wake-up, append the reading to a small ring buffer in RTC memory (
RTC_DATA_ATTRarray plus index). - When the buffer reaches a high-water mark (not when it's completely full), bring up Wi-Fi and send the batch, or append the batch to flash in one write.
- Clear the buffer only after the upload or write succeeded.
Starting the flush early matters: connecting and transmitting takes time, and new samples keep arriving meanwhile.
SPIFFS headroom
ESP-IDF documents that SPIFFS reliably uses only about 75 % of its partition, and that near-full file systems trigger garbage collection that can take seconds per write. Size the partition for your data plus that headroom, rotate log files, and never let the logger fill the partition.
Sources: Stack Exchange (CC BY-SA 4.0) — see links.