Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to make the library work for SD card usage is by doing the following: #39

Open
bitbronze opened this issue Jul 1, 2021 · 0 comments

Comments

@bitbronze
Copy link

bitbronze commented Jul 1, 2021

The Fix

For Arduino (Uno/Mega etc.)

Replace all instances of:
dbFile = SD.open(db_name, FILE_WRITE);
With:
dbFile = SD.open(db_name, O_READ | O_WRITE | O_CREAT);

For ESP32 etc.

Include the FS.h file: #include <FS.h>

Where the database file is first created:
Change dbFile = SD.open(db_name, FILE_WRITE); to dbFile = SD.open(db_name, "w+");

Where an existing database file is opened:
Change dbFile = SD.open(db_name, FILE_WRITE); to dbFile = SD.open(db_name, "r+");

Test with the SD card example to verify that the fix works for you. The temperature values should only be between 1 and 125, not very large or negative numbers.

Explaination

Currently the file.seek() functionality in the Arduino standard SD Library does not work properly with EDB due to the O_APPEND flag when opening a file. Apparently O_APPEND forces the file.write() to always be at last position even if you use the file.seek() function to move the "cursor" around. FILE_WRITE is defined as (O_READ | O_WRITE | O_CREAT | O_APPEND). So, to work with file.seek() without problem, you could open file with every clause except O_APPEND.
(see: here)

A second solution for Arduino is to use the SdFat library. Install SdFat through the Library Manager, then add SdFat SD; after including SdFat: #include <SdFat.h>. Make sure to comment out or remove #include <SD.h>. The rest of the code remains the same.

For the ESP32, it uses a posix compliant file.open() functionality, such that "w+" and "r+" can be supplied as modes of opening files. (see here and here)

Hope this helps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant