-
Notifications
You must be signed in to change notification settings - Fork 41
Description
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.