Hello!
I was looking for a database library and came across to the Extended Database Library at Arduino Playground - HomePage. Very handy! It has everything I was looking for.
Except... It doesn't work right for me.
This is the code that came with the example EDB_Internal_EEPROM:
/*
EDB_Internal_EEPROM.pde
Extended Database Library + Internal Arduino EEPROM Demo Sketch
The Extended Database library project page is here:
http://www.arduino.cc/playground/Code/ExtendedDatabaseLibrary
*/
#include "Arduino.h"
#include <EDB.h>
// Use the Internal Arduino EEPROM as storage
#include <EEPROM.h>
// Uncomment the line appropriate for your platform
#define TABLE_SIZE 512 // Arduino 168
//#define TABLE_SIZE 1024 // Arduino 328
//#define TABLE_SIZE 4096 // Arduino Mega
// The number of demo records that should be created. This should be less
// than (TABLE_SIZE - sizeof(EDB_Header)) / sizeof(LogEvent). If it is higher,
// operations will return EDB_OUT_OF_RANGE for all records outside the usable range.
#define RECORDS_TO_CREATE 10
// Arbitrary record definition for this table.
// This should be modified to reflect your record needs.
struct LogEvent {
int id;
int temperature;
}
logEvent;
// The read and write handlers for using the EEPROM Library
void writer(unsigned long address, byte data)
{
EEPROM.write(address, data);
}
byte reader(unsigned long address)
{
return EEPROM.read(address);
}
// Create an EDB object with the appropriate write and read handlers
EDB db(&writer, &reader);
// Run the demo
void setup()
{
Serial.begin(9600);
Serial.println("Extended Database Library + Arduino Internal EEPROM Demo");
Serial.println();
randomSeed(analogRead(0));
Serial.print("Creating table...");
// create table at with starting address 0
db.create(0, TABLE_SIZE, (unsigned int)sizeof(logEvent));
Serial.println("DONE");
recordLimit();
countRecords();
createRecords(RECORDS_TO_CREATE);
countRecords();
selectAll();
deleteOneRecord(RECORDS_TO_CREATE / 2);
countRecords();
selectAll();
appendOneRecord(RECORDS_TO_CREATE + 1);
countRecords();
selectAll();
insertOneRecord(RECORDS_TO_CREATE / 2);
countRecords();
selectAll();
updateOneRecord(RECORDS_TO_CREATE);
selectAll();
countRecords();
deleteAll();
Serial.println("Use insertRec() and deleteRec() carefully, they can be slow");
countRecords();
for (int i = 1; i <= 20; i++) insertOneRecord(1); // inserting from the beginning gets slower and slower
countRecords();
for (int i = 1; i <= 20; i++) deleteOneRecord(1); // deleting records from the beginning is slower than from the end
countRecords();
}
void loop()
{
}
// utility functions
void recordLimit()
{
Serial.print("Record Limit: ");
Serial.println(db.limit());
}
void deleteOneRecord(int recno)
{
Serial.print("Deleting recno: ");
Serial.println(recno);
db.deleteRec(recno);
}
void deleteAll()
{
Serial.print("Truncating table...");
db.clear();
Serial.println("DONE");
}
void countRecords()
{
Serial.print("Record Count: ");
Serial.println(db.count());
}
void createRecords(int num_recs)
{
Serial.print("Creating Records...");
for (int recno = 1; recno <= num_recs; recno++)
{
logEvent.id = recno;
logEvent.temperature = random(1, 125);
EDB_Status result = db.appendRec(EDB_REC logEvent);
if (result != EDB_OK) printError(result);
}
Serial.println("DONE");
}
void selectAll()
{
for (int recno = 1; recno <= db.count(); recno++)
{
EDB_Status result = db.readRec(recno, EDB_REC logEvent);
if (result == EDB_OK)
{
Serial.print("Recno: ");
Serial.print(recno);
Serial.print(" ID: ");
Serial.print(logEvent.id);
Serial.print(" Temp: ");
Serial.println(logEvent.temperature);
}
else printError(result);
}
}
void updateOneRecord(int recno)
{
Serial.print("Updating record at recno: ");
Serial.print(recno);
Serial.print("...");
logEvent.id = 1234;
logEvent.temperature = 4321;
EDB_Status result = db.updateRec(recno, EDB_REC logEvent);
if (result != EDB_OK) printError(result);
Serial.println("DONE");
}
void insertOneRecord(int recno)
{
Serial.print("Inserting record at recno: ");
Serial.print(recno);
Serial.print("...");
logEvent.id = recno;
logEvent.temperature = random(1, 125);
EDB_Status result = db.insertRec(recno, EDB_REC logEvent);
if (result != EDB_OK) printError(result);
Serial.println("DONE");
}
void appendOneRecord(int id)
{
Serial.print("Appending record...");
logEvent.id = id;
logEvent.temperature = random(1, 125);
EDB_Status result = db.appendRec(EDB_REC logEvent);
if (result != EDB_OK) printError(result);
Serial.println("DONE");
}
void printError(EDB_Status err)
{
Serial.print("ERROR: ");
switch (err)
{
case EDB_OUT_OF_RANGE:
Serial.println("Recno out of range");
break;
case EDB_TABLE_FULL:
Serial.println("Table full");
break;
case EDB_OK:
default:
Serial.println("OK");
break;
}
}
So then a deleted some code to just have the database created and saved in the EEPROM but not erased:
/*
void setup()
{
Serial.begin(9600);
Serial.println("Extended Database Library + Arduino Internal EEPROM Demo");
Serial.println();
randomSeed(analogRead(0));
Serial.print("Creating table...");
// create table at with starting address 0
db.create(0, TABLE_SIZE, (unsigned int)sizeof(logEvent));
Serial.println("DONE");
recordLimit();
countRecords();
createRecords(RECORDS_TO_CREATE);
countRecords();
selectAll();
}
And I got the following:
Extended Database Library + Arduino Internal EEPROM Demo
Creating table...DONE
Record Limit: 125
Record Count: 0
Creating Records...DONE
Record Count: 10
Recno: 1 ID: 1 Temp: 69
Recno: 2 ID: 2 Temp: 33
Recno: 3 ID: 3 Temp: 4
Recno: 4 ID: 4 Temp: 91
Recno: 5 ID: 5 Temp: 54
Recno: 6 ID: 6 Temp: 33
Recno: 7 ID: 7 Temp: 6
Recno: 8 ID: 8 Temp: 49
Recno: 9 ID: 9 Temp: 37
Recno: 10 ID: 10 Temp: 50
Then I changed the sketch again so I can just read stuff from the EEPROM and display it:
void setup()
{
Serial.begin(9600);
Serial.println("Extended Database Library + Arduino Internal EEPROM Demo");
Serial.println();
randomSeed(analogRead(0));
Serial.print("Creating table...");
// create table at with starting address 0
db.create(0, TABLE_SIZE, (unsigned int)sizeof(logEvent));
Serial.println("DONE");
recordLimit();
countRecords();
selectAll();
}
But I found no records!
Extended Database Library + Arduino Internal EEPROM Demo
Creating table...DONE
Record Limit: 125
Record Count: 0
I think it's missing some code to actually load the records from the EEPROM but looking at the EDB.cpp file I can't figure out how to get it done. I've looked everywhere and I can't find information about this issue. I found some very old posts from people having the same issue but I couldn't find a solution anywhere.
Any help would be greatly appreciated.
Thanks!
A