Log custom data in persistent memory.
Used to log sensor data for diagnosing problems in case of a crash, or for saving settings while keeping past settings as fallback.
This Library is written for and tested on ESP32 (Espressif), STM32 (STMicroelectronics), and BW16 (AmebaD).
Download or Clone this repository and pull the folder into the Arduino libraries folder or use the 'Add .ZIP Libraries' function in the ArduinoIDE.
struct Entry{
int timestamp;
int sensor1;
int sensor2;
};
DataLog<Entry> log;
Entry newentry = {time(NULL), analogRead(GPIO_NUM_4), 25};
log.addEntry(&newentry);
The log adds the entry to the persistent memory and remembers all entries even after a reset. If the log is full the oldest entry will be overwritten.
Entry readentry;
log.readEntry(-1, &readentry);
With 0 being the oldest entry, -1 the newest entry.
#include <DataLog.h>
struct Entry{
int timestamp;
int sensor1;
int sensor2;
};
void setup(){
Serial.begin(115200);
pinMode(GPIO_NUM_4, INPUT);
DataLog<Entry> log;
Entry readentry;
if(log.readEntry(-1, &readentry) != -1){
Serial.println("Logged Sensor Value: "+String(readentry.sensor1));
}
Entry newentry = {time(NULL), analogRead(GPIO_NUM_4), 25};
log.addEntry(&newentry);
delay(10000);
ESP.restart();
}
void loop(){}
(For other microcontrollers see examples)
Go into DataLog.h and edit the following Macros
//Change to Full Size of EEPROM in use
//If EEPROM range is used but not covered by this number it will be erased
#define EEPROM_MAX_SIZE 4096
//Change to start and end of where the data log should be.
//Memory outside this area is not touched by the library
#define LOG_START_ADDRESS 0
#define LOG_END_ADDRESS EEPROM_MAX_SIZE
For EEPROM memory with this intended layout:
Change Macros to these numbers:
#define EEPROM_MAX_SIZE 1024
#define LOG_START_ADDRESS 256
#define LOG_END_ADDRESS 768