Here's a little database library that makes use of the Arduino's EEPROM memory to store records in a table. It is very simple to keep the memory footprint small. You will have to spin your own record locate and other functions like validation, but this is relatively easy to do.

Overview

How to use in a nutshell:

- include Db.h and EEPROM.h
- declare an instance of DB, db
- define a structure for your records
- pick an address in EEPROM for the table to start
- make a little sketch that creates the table by calling db.create(address,sizeof(myRecStruct))
- in your application's sketch open the table with db.open(address)
- use the define DB_REC to cast your structure as a byte pointer when passing it as a parameter
for write and read operations, this helps make the code more readable.

Download

http://arduino-dblib.googlecode.com/files/DB.zip

Installing

Install like any other Arduino library. Unzip the download into your Arduino-00xx/hardware/libraries directory. If the Arduino IDE is already running then exit and restart the Arduino IDE. This will compile and build the library files.

Example

//
// EEPROM DB example
//
// This example creates a database, adds 10 records, and reads the 10 records
// displaying the results.  
//
#include "WProgram.h"
#include <EEPROM.h>
#include <DB.h>
#include <string.h>

DB db;

#define MY_TBL 1

struct MyRec {
  char date[11];
  char time[9];
  int temperature;
} myrec;

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

  randomSeed(analogRead(0));

  Serial.print("Creating Table...");
  db.create(MY_TBL,sizeof(myrec));
  db.open(MY_TBL);
  Serial.println("DONE");

  Serial.print("Creating records...");
  for (int recno = 1; recno <= 5; recno++)
  {
    int m = random(1, 12);
    int d = random(1, 31);
    int h = random(1, 12);
    int i = random(59);
    int s = random(59);

    sprintf(myrec.date, "2009-%02d-%02d", m, d);
    sprintf(myrec.time, "%02d:%02d:%02d", h, i, s);
    myrec.temperature = random(-100, 100);
    Serial.print("CREATING RECNUM: "); Serial.println(recno);
    Serial.print("WRITING Date: "); Serial.println(myrec.date);
    Serial.print("WRITING Time: "); Serial.println(myrec.time);
    Serial.print("WRITING Temperature: "); Serial.println(myrec.temperature);
    db.append(DB_REC myrec);
    Serial.println("DONE");
  }
  Serial.println();
  Serial.println("Reading records from EEPROM...");
  Serial.println();
  selectAll();
}

void loop()
{
}

void selectAll()
{
  if (db.nRecs()) Serial.println("-----");
  for (int i = 1; i <= db.nRecs(); i++)
  {
    db.read(i, DB_REC myrec);
    Serial.print("Recnum: "); Serial.println(i); 
    Serial.print("Date: "); Serial.println(myrec.date); 
    Serial.print("Time: "); Serial.println(myrec.time);
    Serial.print("Temperature: "); Serial.println(myrec.temperature);
    Serial.println("-----");  
  } 
}

Known Issues

  • Tables are limited to a maximum of 255 records
  • Tables are limited to the amount of EEPROM on your Arduino
  • Maximum number of records: (EEPROM size / record size = max records)

Share