Go Down

Topic: Extended Database Library (Read 3179 times) previous topic - next topic

grandpa

Nov 16, 2009, 09:39 am Last Edit: Nov 16, 2009, 10:08 am by grandpa Reason: 1
This is a re-implementation of the database library originally written by Madhusudana das found here:

http://www.arduino.cc/playground/Code/DatabaseLibrary

This version increases the maximum number of records allowed in a database from 256 records (byte) to 4,294,967,295 theoretical records (unsigned long). When using with an AT24C1024, you are limited to the amount of records that will fit into the maximum available EEPROM storage (128 kilobytes for 1 device, up to 512 kilobytes for 4 devices).  The maximum record size was also increased from 256 bytes (byte) to 32,767 bytes (int).

With these changes, it is now possible to use this library in conjunction with an external EEPROM such as the AT24C1024 via the AT24C1024 EEPROM Library found here:

http://www.arduino.cc/playground/Code/I2CEEPROM24C1024

The Extended Database library project page is here:

http://www.arduino.cc/playground/Code/ExtendedDatabaseLibrary

Both libraries may be downloaded here:

http://code.google.com/p/arduino-at24c1024/downloads/list

Any comments/suggestions/improvements are welcome.




Example sketch:

This sketch will create 2000 records on an AT24C1024 EEPROM and then output them to the serial port.

Code: [Select]

/*
 EDB.pde
 Extended Database Library + AT24C1024 EEPROM Demo Sketch

 The Extended Database library project page is here:
 http://www.arduino.cc/playground/Code/ExtendedDatabaseLibrary

 The AT24C1024 library project page is here:
 http://www.arduino.cc/playground/Code/I2CEEPROM24C1024

*/
#include "WProgram.h"
#include <Wire.h>
#include <E24C1024.h>
#include <EDB.h>
#include "string.h"

EDB db;

#
#define LOG_TABLE 0
#define CREATE_RECORDS true
#define RECORDS_TO_CREATE 2000

struct LogEvent {
 int id;
 char date[11];
 char time[9];
 int temperature;
} logEvent;

void setup()
{
 Serial.begin(9600);
 Serial.println("EEPROM DB Library Demo");
 Serial.println();

 randomSeed(analogRead(0));

 if (CREATE_RECORDS)
 {
   db.create(LOG_TABLE, (int)sizeof(logEvent));

   Serial.print("Creating Records...");
   for (int j = 1; j <= RECORDS_TO_CREATE; j++)
   {
     logEvent.id = j;
     int m = random(1, 12);
     int d = random(1, 31);
     int h = random(1, 12);
     int i = random(59);
     int s = random(59);    
     sprintf(logEvent.date, "2009-%02d-%02d", m, d);
     sprintf(logEvent.time, "%02d:%02d:%02d", h, i, s);
     logEvent.temperature = random(1, 125);
     db.append(EDB_REC logEvent);
     if (!(j % 100)) Serial.print(".");
   }
   Serial.println("DONE");
 }
 db.open(LOG_TABLE);
 Serial.print("Record Size: "); Serial.println(db.recSize());
 Serial.print("Record Count: "); Serial.println(db.nRecs());
 delay(1000);
 selectAll();
}

void loop()
{
}

void selectAll()
{  
 if (db.nRecs()) Serial.println("-----");
 for (unsigned int i = 1; i <= RECORDS_TO_CREATE; i++)
 {
   db.read(i, EDB_REC logEvent);
   Serial.print("RecNum: "); Serial.println(i);
   Serial.print("ID: "); Serial.println(logEvent.id);
   Serial.print("Date: "); Serial.println(logEvent.date);
   Serial.print("Time: "); Serial.println(logEvent.time);
   Serial.print("Temp: "); Serial.println(logEvent.temperature);  
   Serial.println("-----");  
 }
}

alm7100

Hi

Iam trying to use this lib, bout i don't know how to uses it.

I like to use it on a 24FC512 chip from Microchip (Also a I2C chip)

How do i get it to work??

How do i write the #include <E24C1024.h>
file??

Sorry for my English, and i am new to c also..

grandpa

#2
Dec 09, 2009, 04:47 am Last Edit: Dec 09, 2009, 05:16 am by grandpa Reason: 1
Quote
I like to use it on a 24FC512 chip from Microchip (Also a I2C chip). How do i get it to work??


I'll assume that you've verified everything is connected properly by first using something similar to the 24LC512 I2C EEPROM Driver to read and write data to your EEPROM.  I would start here before adding the complexity of the database.  Make sure your chip select pins (A0, A1, A2) are connected as described in the sketch (for 1 device, A0->GND, A1->GND, A2->GND).

You can find the 24LC512 I2C EEPROM Driver here:

http://www.arduino.cc/playground/Code/I2CEEPROM24LC512

Once you've verified that your EEPROM is working, download the two libraries required from the links below.  Both libraries were recently updated so make sure you have the most recent versions.  Then, unzip the download into your Arduino-00xx/hardware/libraries directory. If the Arduino IDE is already running then exit and restart the Arduino IDE.

The Extended Database library project page is here:
http://www.arduino.cc/playground/Code/ExtendedDatabaseLibrary

The E24C1024 library project page is here:
http://www.arduino.cc/playground/Code/I2CEEPROM24C1024

Unfortunately, I don't have a 24XX512 so I can't verify that it works.  Copy and paste the code below into a new sketch window in the Arduino IDE.  Let me know if it works for you.  

Code: [Select]

/*
EDB_24XX512.pde
Extended Database Library + 24XX512 EEPROM Demo Sketch
*/
#include "WProgram.h"
#include <EDB.h>

// Use the 24XX512 EEPROM as storage
#include <Wire.h>
#include <E24C1024.h> // Should be compatible with 24XX512 series in this instance

// From the 24XX512 datasheet:
//
// The Chip Select bits A2, A1 and A0 can be used to expand the
// contiguous address space for up to 4 Mbit by adding up to eight
// 24XX512 devices on the same bus.
//
// So, each device must be have their address pins wired as listed below to
// create a single, contiguous address space across one or more devices.
//
// Example - 1 device:
// Device connections: A0->GND, A1->GND, A2->GND
// Uncomment only the #define TABLE_SIZE 65536 line below.

// Example - 3 devices:
// Device 1 connections: A0->GND, A1->GND, A2->GND
// Device 2 connections: A0->GND, A1->+5V, A2->GND
// Device 3 connections: A0->+5V, A1->+5V, A2->GND
// Uncomment only the #define TABLE_SIZE 196608 line below.
//
// Uncomment the ONE line appropriate for your platform.  
//#define TABLE_SIZE 65536 // 1 device: A0->GND, A1->GND, A2->GND
//#define TABLE_SIZE 131072 // 2 devices: A0->+5V, A1->1, A2->GND
//#define TABLE_SIZE 196608 // 3 devices: A0->GND, A1->+5V, A2->GND
//#define TABLE_SIZE 262144 // 4 devices: A0->+5V, A1->+5V, A2->GND
//#define TABLE_SIZE 327680 // 5 devices: A0->GND, A1->GND, A2->+5V
//#define TABLE_SIZE 393216 // 6 devices: A0->+5V, A1->GND, A2->+5V
//#define TABLE_SIZE 458752 // 7 devices: A0->GND, A1->+5V, A2->+5V
//#define TABLE_SIZE 524288 // 8 devices: A0->+5V, A1->+5V, A2->+5V

// default to the smallest - 1 device
#ifndef TABLE_SIZE
#define TABLE_SIZE 65536
#endif

// 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 100

// Arbitrary record definition for this table.  
// This should be modified to reflect your record needs.
struct LogEvent {
 int id;
 int temperature;
}
logEvent;

// Create an EDB object with the appropriate write and read handlers
EDB db(&E24C1024::write, &E24C1024::read);

// Run the demo
void setup()
{
 Serial.begin(9600);
 Serial.println("Extended Database Library + 24XX512 EEPROM Demo");

 randomSeed(analogRead(0));
 
 // create a table with starting address 0
 Serial.print("Creating table...");
 db.create(0, TABLE_SIZE, (unsigned int)sizeof(logEvent));
 Serial.println("DONE");

 recordLimit();
 countRecords();
 createRecords(RECORDS_TO_CREATE);
 countRecords();
 selectAll();
 countRecords();
 deleteAll();
 countRecords();
}

void loop()
{
}

void recordLimit()
{
 Serial.print("Record Limit: ");
 Serial.println(db.limit());
}

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 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:
     Serial.println("OK");
     break;
   default:
     Serial.println("Unknown Error");
     break;
 }
}



alm7100

Hi grandpa

Now its works for me :-)

Thanks

Twizted

ok, I am a bit new to this and have yet to use a arduino to store data and later retrieve it but I was presented with a build that requires this function.

I stumbled across your implementation of the database lib and so far so good. I am only using the build in eeprom of my Arduino Duemilanove but the one issue I am running into is it seems to loose all memory on power loss... I was under the impression that this was being stored in the local eeprom of the Arduino (328) but each time I reset or power back on the arduino I have 0 records

This is the sketch I have been tinkering with to learn how to use the library.. it might be chopped up but it is just for experimentation.

Can you let me know why its not storing the db after power loss?

Code: [Select]
/*
EDB_Simple.pde
Extended Database Library + Internal Arduino EEPROM Demo Sketch

The Extended Database library project page is here:
REMOVED LINK PER FORUM RULES

*/
#include "WProgram.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 or greater

// 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);

const int buttonPin1 = 2;
const int buttonPin2 = 3;
const int buttonPin3 = 4;
const int ledPin = 13;

int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;

void setup()
{
 Serial.begin(9600);
 Serial.println("Extended Database Library + Arduino Internal EEPROM Demo");
 Serial.println();
 Serial.print("Record Count: "); Serial.println(db.count());
}

void loop()
{
 buttonState1 = digitalRead(buttonPin1);
 buttonState2 = digitalRead(buttonPin2);
 buttonState3 = digitalRead(buttonPin3);
 
 if (buttonState1 == HIGH){
   digitalWrite(ledPin, HIGH);
   outputRecords();
   delay(500);
 } else {
   digitalWrite(ledPin, LOW);
 }
 
 if (buttonState2 == HIGH){
   digitalWrite(ledPin, HIGH);
   createRecord();
   delay(500);
 } else {
   digitalWrite(ledPin, LOW);
 }
 
 if (buttonState3 == HIGH){
   digitalWrite(ledPin, HIGH);
   createDB();
   delay(500);
 } else {
   digitalWrite(ledPin, LOW);
 }
}

void outputRecords(){
 
   int db_count = db.count();
   int count = 1;
   
   Serial.print("Record Count: "); Serial.println(db.count());
   while (db_count)
   {
     db.readRec(count, EDB_REC logEvent);
     Serial.print("ID: "); Serial.println(logEvent.id);
     Serial.print("Temp: "); Serial.println(logEvent.temperature);  
    db_count--;
    count++;
  }  
}

void createRecord(){
 
 int recno = db.count()+1;
 
   Serial.println("Creating Record...");
   logEvent.id = recno;
   logEvent.temperature = analogRead(0);
   db.appendRec(EDB_REC logEvent);
}

void createDB(){
 db.create(0, TABLE_SIZE, sizeof(logEvent));
}



Go Up