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();
}
1 Answer 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;
-
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 twostructs
are not the same size?user1092247– user10922472018年07月25日 21:43:09 +00:00Commented 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.esoterik– esoterik2018年07月25日 21:48:33 +00:00Commented Jul 25, 2018 at 21:48
-
@esoterik, add solution to the answer2018年07月26日 03:17:17 +00:00Commented Jul 26, 2018 at 3:17
table_name
in thefor
loop with one of the table names (e.g.sensorLog1
), but then obviously the function only works for that particular table...inline
? how can you have a reference to inline function?