How to makes log rotate in sd card

Thanks for the advice, I really feel like I'm lacking in learning about this.
your explanation is very helpful for me and what steps should i do
first.

I imagine when there is a power loss, the data stored is the real data
The truth is that when the data is obtained, it does not need to be complete.

This is an example of recording data when I do data logging that never stops
image_2021-08-10_003551

It is extremely unlikely that those measurements change by much every second. Save the readings every ten seconds, every minute, or even every 10 minutes, and the problem goes away.

You could also log in binary format, it would cut your data size to about 1/4 of what it is now. You only need 4 bytes to store an Epoch time, which resolves to the second. Most of your data field is just the long winded human readable time. Even just logging the Epoch time as an integer would save a lot.

Not trying to hijack thread but can you tell me the syntax to open an arduino SD file in the indexed mode? I searched by "arduino SD index" but couldn't find any usable info. If I knew the syntax I'm sure I could find more info.

Thanks
John

Did you read the SD library documentation and examples?

I suspect he is referring to the random access method. The OPs problem is as old as there have been disk drives on computers and have been solved many times, only a few 100% successful.
Paul

Yes, using the seek() function to position the file pointer. The logs would have to be redesigned with fixed length fields. Apparently this API doesn't require any different treatment vs. a sequential file when it opens a file.

1 Like

Thanks so the index is in bytes.....interesting.

Yes, it's a byte offset from the file start. To seek a given record, you just multiply the record number by the record size in bytes.

It's fairly easy to implement a circular buffer for this, the "head" is just always the latest time stamp... when you pull the card and read it later. In fact, a safe method of choosing an available slot is to just read the file and look for the oldest time stamp. Then the records wouldn't even have to be in order.

If the library is written properly, this shouldn't cause fragmentation. There is no deletion, just overwrites.

Here is another method of log rotation:
rotating log

1 Like