Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

A minimal library for binary data logging in ESP8266 systems

License

NotificationsYou must be signed in to change notification settings

bitmario/SPIFFSLogger

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

A minimal library for binary data logging in ESP8266 systems.

Features

  • Easy to use
  • Stores data in binary along with a UTC timestamp
  • Results in about 50% space spavings when compared to logging the same data in CSV
  • Splits data into daily files, allowing for efficient search
  • Automatically rotates files, deleting the ones that exceed the specified age
  • Uses built-in ESP/newlib time functions

Installing

  1. Download thelatest release
  2. Extract it into your libraries folder
  3. Rename the folder fromSPIFFSLogger-x.x.x toSPIFFSLogger

Getting started

See the summary below and try theexample. You may also want to take a peek at ourdocumentation.

Required includes

#include<SPIFFSLogger.h>

The SPIFFSLogData<T> struct

This struct represents the data as stored in SPIFFS (including the timestamp). It is returned in any logger read operations.

Definition:

template<classT>structSPIFFSLogData{time_t timestampUTC;/** creation time in UTC*/    T data;/** data of type T*/};

The SPIFFSLogger<T> class

This is the core of the library. You can create your logger like so:

// store ints, saving files in /log/ and keeping today + 7 days of historySPIFFSLogger<int>logger("/log",7);

We can also store a struct (which is probably more useful!):

structEnvData {float temperature;float humidity;uint16_t pressure;};SPIFFSLogger<EnvData>logger("/log",7);

Initializing the logger

This library uses the ESP8266 SPIFFS and built-in time functions so you must initialize those components and then calllogger.init().

Yoursetup() should look something like this:

voidsetup() {// configure time however you like, we use NTP herewifiSetup();// ommitted for brevityconfigTime(0,0,"pool.ntp.org");// initialize SPIFFS    SPIFFS.begin();// initialize our logger    logger.init();}

Running required operations

You must calllogger.process() in your loop to run the required operations (file updates and rotation). E.g.:

voidloop() {// your code is here    logger.process();}

Writing data

Simply calllogger.write() and the data will be logged with the current timestamp:

structEnvData data = {23.54,50.67,1020 };logger.write(data);

Reading data

Reading the first row of today's logfile:

SPIFFSLogData<EnvData> data;logger.readRows(&data, time(nullptr),0,1);Serial.printf("TS: %d, T: %.2f, H: %.2f, %u\n",              data.timestampUTC,              data.data.temperature,              data.data.humidity,              data.data.pressure);

Or the last row:

consttime_t now = time(nullptr);constsize_t rowCount = logger.rowCount(now);logger.readRows(&data, now, rowCount -1,1);

Or 25 rows starting at the 50th:

SPIFFSLogData<EnvData> data[25];logger.readRows(data, time(nullptr),49,25);

Filtering data

Retrieving data from the last 20 minutes:

SPIFFSLogData<EnvData> data[25];size_t count = logger.readRowsBetween(                       &data,// output                       now - (60 *20),// time start (inclusive)                       now,// time end (inclusive)0,// start index within results25// max number of rows to fetch);for (int i=0; i<count; i++) {    Serial.printf("TS: %d, T: %.2f, H: %.2f, %u\n",                  data[i].timestampUTC,                  data[i].data.temperature,                  data[i].data.humidity,                  data[i].data.pressure);}

License

Licensed under the GNU LGPLv3, see theLICENSE file.


[8]ページ先頭

©2009-2025 Movatter.jp