Daily-file SD datalogging that survives network outages
SD card datalogging is the insurance policy of every industrial IoT node: when WiFi or the broker goes down, the data has to land somewhere. This example for the ESP32 PLC appends every JSON record to a file named after the current date — /2026-06-10.json — so rotation happens automatically at midnight. The pattern comes from a real textile machine monitoring deployment where months of production data survived every network incident.
One file per day, named by the RTC
The filename is built from the hardware RTC — year, month, day — so a new file starts at midnight with zero extra logic. Daily files keep each file small enough to upload over HTTP in one request, make manual recovery trivial (pull the card, read the day you need), and give the backend a natural partitioning key. If the RTC has no valid time, records fall back to a rescue file instead of being lost.
Append-only writes with hot remount
Every record is a single JSON line appended with FILE_APPEND, and the file is closed immediately after each write — so a sudden power cut costs you at most the one line in flight. If a write fails for any reason, the code marks the card as unavailable and quietly retries SD.begin() on the next attempt, meaning a card that recovers (or gets swapped by a technician) remounts on the fly without ever rebooting the PLC.
An inventory for later recovery
A small directory listing function walks the root of the card and reports every .json file together with its size — the device's own inventory of what is still sitting locally. In the production system this is exactly what the remote MQTT "list" command returns to the operator, and exactly what the HTTP upload module iterates over to push pending days to the Node-RED concentrator once the network comes back.
A snippet from the implementation
Straight from the example as deployed on the ESP32 PLC — copy it freely:
void setup() {
Serial.begin(115200);
RTC.begin(); // in production: synchronized via NTP
sdAvailable = SD.begin();
Serial.println(sdAvailable ? "SD mounted" : "SD NOT available");
listFiles(); // which days are still pending upload
}
The full example is a complete program — wiring header, setup and main loop — ready to adapt to your application.
Frequently asked questions
How much history fits on a microSD card?
A busy machine generating a JSON line every few seconds produces a few megabytes per day. Even a small 8 GB industrial card stores years of data, so capacity is never the limiting factor.
What happens if power is cut during a write?
Because each record is appended and the file is closed straight away, the worst case is losing the line being written. FAT32 corruption is rare with this pattern, and the daily-file layout limits any damage to a single day.
How do I get the buffered files off the card remotely?
Pair this module with an HTTP POST upload routine, where the PLC streams the requested daily file to a server endpoint with basic auth, triggered by an MQTT command, so nobody has to touch the card on the factory floor.