0

I'm using an Arduino UNO, the Extended Database Library (EDB), and an microSD Card to read/write 2 database tables. I've managed to create both ".db" files on the SD (they show up in Finder when I load the microSD directly onto my Mac).

I can also populate them with placeholder data, but this data does not seem to be actually written to the file (and thus doesn't survive reboot).

Also, trying dbFile = SD.open(db_name1, FILE_WRITE); returns false, even though the files do exist on the drive?

Here's my create code:

#include <SPI.h>
#include <SD.h>
#define SD_PIN 10 //SD read/write pin
#define TABLE_SIZE 8192 //256*32
#include "Arduino.h"
#include <EDB.h>
char* db_name1 = "/DB/SENSOR1.db";
char* db_name2 = "/DB/SENSOR2.db";
File dbFile;
struct sensorLog1 {
 unsigned int id;
 float input;
 unsigned int timestamp; 
}
sensorLog1;
struct sensorLog2 {
 unsigned int id;
 float input;
 unsigned int timestamp; 
}
sensorLog2;
// Setting up EDB object
inline void writer (unsigned long address, const byte* data, unsigned int recsize) {
 dbFile.seek(address);
 dbFile.write(data,recsize);
 dbFile.flush();
}
inline void reader (unsigned long address, byte* data, unsigned int recsize) {
 dbFile.seek(address);
 dbFile.read(data,recsize);
}
// Create an EDB object with the appropriate write and read handlers
EDB db(&writer, &reader);
void setup() {
 Serial.begin(9600);
 createDB(db_name1, "sensorLog1"); 
 createDB(db_name2, "sensorLog2");
 dbFile = SD.open(db_name1, FILE_WRITE); // Doesn't work!
 if (!dbFile) { Serial.println("File does not exist"); }
}
void loop() {} 
void createDB(char* db_name, char* table_name) {
 // create file
 dbFile = SD.open(db_name, FILE_WRITE);
 db.create(0, TABLE_SIZE, (unsigned int)sizeof(table_name));
 // populate with placeholder data
 for (unsigned int i = 1; i <= 20; i++) {
 table_name.id = i;
 table_name.input = 0;
 table_name.timestamp = 0;
 EDB_Status result = db.appendRec(EDB_REC table_name);
 }
 // check data
 Serial.println(db.count()); // returns '20'
 // close file to write
 dbFile.close();
}
asked Jul 25, 2018 at 17:46
5
  • does the sample work? the sample uses different code for creating and opening an existing database. Commented Jul 25, 2018 at 18:32
  • @esoterik, yes, the code works if I replace all instances of table_name in the for loop with one of the table names (e.g. sensorLog1), but then obviously the function only works for that particular table... Commented Jul 25, 2018 at 21:18
  • since your structs are identical except for the name, you should be able to do this with only one struct. sizeof doesn't do reflection, it won't do what your trying to do. Commented Jul 25, 2018 at 21:36
  • what for inline? how can you have a reference to inline function? Commented Jul 26, 2018 at 3:13
  • @Juraj, I'm not sure what you're trying to say -- that part comes straight from the EDB code (github.com/jwhiddon/EDB/blob/master/examples/…) Commented Jul 26, 2018 at 9:08

1 Answer 1

1

db.create(0, TABLE_SIZE, (unsigned int)sizeof(table_name));

The third parameter is the record size! sizeof doesn't work on data, it works on types, so your getting the size of a char* instead of the size of your sensorlogN struct.

Your code as written shouldn't compile! a char* doesn't have a .id member.

table_name.id = i;
table_name.input = 0;
table_name.timestamp = 0;
answered Jul 25, 2018 at 21:35
3
  • Hmm I clearly don't have a perfect understanding of the different variable types yet... How could I make this for loop work, when the two structs are not the same size? Commented Jul 25, 2018 at 21:43
  • @user1092247 you would have to pass in the size, and a factory function pointer that would create the the correct objects for you, but I don't think that will actually help you accomplish whatever it is your trying to do. Commented Jul 25, 2018 at 21:48
  • @esoterik, add solution to the answer Commented Jul 26, 2018 at 3:17

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.