Skip to content

Commit 02f152b

Browse files
examples, docs
1 parent 9b4d8e9 commit 02f152b

File tree

5 files changed

+92
-106
lines changed

5 files changed

+92
-106
lines changed

examples/esp_logger/esp_logger.ino

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,68 @@
1+
/**
2+
* Log on flash memory an event every 1.5 seconds. Every 30 seconds,
3+
* the log file is flushed over serial port.
4+
*
5+
* NOTE: the first time you run this sketch or when changing the file system
6+
* layout, explicit formatting is recommended.
7+
*/
18
#include <Ticker.h>
29
#include <logger_spiffs.h>
310

11+
// Specify the path where the log is placed
412
LoggerSPIFFS myLog("/log/mylog.log");
513

6-
/**
7-
* Event generation and management
8-
*/
9-
1014
// Event generator, this could be implemented in the main loop
1115
Ticker somethingHappens;
1216

13-
// in seconds
14-
float eventFrequency = 1.5;
17+
// Event generation period, in seconds
18+
float eventPeriod = 1.5;
1519

16-
// This is the variable event to log
20+
// Variable to be logged
1721
int counter = 0;
1822

1923
void somethingHappening(){
2024
counter++;
2125

2226
// counter is a multiple of 3, log it!
2327
if(counter%3==0){
24-
Serial.println(String("Oh, ->") + counter + "<- is just happened");
25-
myLog.append(String("val:") + counter);
28+
Serial.println(String("Hey, event ->") + counter + "<- is just happened");
29+
String record = String("val:") + counter;
30+
myLog.append(record.c_str());
2631
}
27-
somethingHappens.attach(eventFrequency, somethingHappening);
2832
}
2933

3034
void setup() {
3135
Serial.begin(115200);
3236
while(!Serial);
37+
Serial.println();
38+
Serial.println("ESP Logger - Log on internal flash memory");
3339

34-
Serial.println("Basic log example");
35-
36-
Serial.print("Initializing SD card... ");
37-
if(!myLog.begin()){
38-
Serial.println("Failed!");
39-
while(1) delay(100);
40-
}else{
41-
Serial.println("Done!");
42-
}
40+
myLog.begin();
4341
myLog.setFlusherCallback(senderHelp);
44-
somethingHappens.attach(eventFrequency, somethingHappening);
42+
43+
Serial.println("Starting to log...");
44+
somethingHappens.attach(eventPeriod, somethingHappening);
4545
}
4646

47-
// in millisecond
47+
// flush period, in millisecond
4848
int period = 30000;
4949

5050
unsigned int nextTime = 0;
5151

52-
// This loop is the logger controller, it decides when it's time to flush
53-
// You can see a more elegant management in other examples.
52+
5453
void loop() {
54+
// This loop is the logger controller, it decides
55+
// when it's time to flush. In other examples I will use Ticker library.
5556
if (millis()>nextTime){
5657
nextTime += period;
5758
myLog.flush();
5859
}
5960
}
6061

6162
/**
62-
* Callback to support the flushing.
63-
* In this case the flushing is performed on the Serial interface,
64-
* but you can easily replace this to send data over wireless network
63+
* Flush a chuck of logged records. To exemplify, the records are
64+
* flushed on Serial, but you are free to modify it to send data
65+
* over the network.
6566
*/
6667
bool senderHelp(char* buffer, int n){
6768
int index=0;

examples/esp_logger_runtime/esp_logger_runtime.ino

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,60 @@
11
#include <logger_spiffs.h>
22
#include <logger_routine.h>
33

4-
// in second
4+
// flush period, in second
55
const int period = 30;
66
// Path where the log is placed
77
const String filepath = "/log/mylog.log";
88

99
LoggerSPIFFS myLog(filepath);
1010

11-
// This class takes care of flushing the log file every period
11+
// This class takes care of flushing the log file every "period"
1212
LoggerRoutine logRun(myLog, period);
1313

14-
15-
/**
16-
* Event generation and management
17-
*/
18-
1914
// Event generator, this could be implemented in the main loop
2015
Ticker somethingHappens;
2116

22-
// in seconds
23-
float eventFrequency = 1.5;
17+
// Event generation period, in seconds
18+
float eventPeriod = 1.5;
2419

25-
// This is the variable event to log
20+
// Variable to be logged
2621
int counter = 0;
2722

2823
void somethingHappening(){
2924
counter++;
3025

31-
// counter is a multiple of 4, log it!
26+
// counter is a multiple of 3, log it!
3227
if(counter%3==0){
33-
Serial.println(String("Oh, ->") + counter + "<- is just happened");
28+
Serial.println(String("Hey, event ->") + counter + "<- is just happened");
3429
myLog.append(String("val:") + counter);
3530
Serial.println(String("Now the log takes ") + myLog.getActualSize() + "/" + myLog.getSizeLimit());
3631
}
37-
somethingHappens.attach(eventFrequency, somethingHappening);
3832
}
3933

4034
void setup() {
4135
Serial.begin(115200);
4236
while(!Serial);
43-
Serial.println("Log on SPIFFS example");
37+
Serial.println();
38+
Serial.println("ESP Logger - Log on internal flash memory (with logger routine)");
4439

4540
myLog.begin();
46-
myLog.setSizeLimit(1000, false);
41+
myLog.setSizeLimit(1000);
4742
myLog.setSizeLimitPerChunk(60);
4843
myLog.setFlusherCallback(senderHelp);
49-
somethingHappens.attach(eventFrequency, somethingHappening);
50-
44+
45+
Serial.println("Starting to log...");
46+
somethingHappens.attach(eventPeriod, somethingHappening);
5147
logRun.begin(true);
5248
}
5349

5450
void loop() {}
5551

5652
/**
57-
* Callback to support the flushing.
58-
* In this case the flushing is performed on the Serial interface, but you can easily
59-
* replace this behaviour with a wireless connection or whatever function.
53+
* Flush a chuck of logged records. To exemplify, the records are
54+
* flushed on Serial, but you are free to modify it to send data
55+
* over the network.
56+
*
57+
* NOTE: This example simulates a channel that sometimes may fail.
6058
*/
6159
bool senderHelp(char* buffer, int n){
6260
static int failPacketCounter = 0;

examples/esp_logger_runtime_sd/esp_logger_runtime_sd.ino

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,54 +5,46 @@
55
// othewise you can call the method begin(..) (only for ESP32)
66
#include <SD.h>
77

8-
#ifdef ESP8266
9-
const int csPin = D4;
10-
#else
11-
const int csPin = 16;
12-
#endif
8+
// Specify CS (chip select) pin
9+
const int csPin = 5;
1310

14-
// in seconds
11+
// flush period, in seconds
1512
const int period = 30;
16-
13+
// Path where the log is placed
1714
const String filepath = "/myLog.log";
1815

1916
LoggerSD myLog(filepath);
2017

21-
// This class takes care to flush "myLog" object every period
18+
// This class takes care to flush "myLog" object every "period"
2219
LoggerRoutine myLogRun(myLog, period);
2320

24-
25-
/**
26-
* Event generation and management
27-
*/
28-
2921
// Event generator, this could be implemented in the main loop
3022
Ticker somethingHappens;
3123

32-
// in seconds
33-
float eventFrequency = 1.5;
24+
// Event generation period, in seconds
25+
float eventPeriod = 1.5;
3426

35-
// This is the variable event to myLog
27+
// Variable to be logged
3628
int counter = 0;
3729

3830
void somethingHappening(){
3931
counter++;
4032

41-
// counter is a multiple of 4, myLog it!
33+
// counter is a multiple of 3, myLog it!
4234
if(counter%3==0){
43-
Serial.println(String("Oh, ->") + counter + "<- is just happened");
35+
Serial.println(String("Hey, event ->") + counter + "<- is just happened");
4436
myLog.append(String("val:") + counter);
4537
Serial.println(String("Now the log takes ") + myLog.getActualSize() + "/" + myLog.getSizeLimit());
4638
}
47-
somethingHappens.attach(eventFrequency, somethingHappening);
4839
}
4940

5041
void setup() {
5142
Serial.begin(115200);
5243
while(!Serial);
5344
Serial.println();
54-
Serial.println("Log on SD Example");
45+
Serial.println("ESP Logger - Log on SD (with logger routine)");
5546

47+
// Check if the microSD is connected
5648
Serial.print("Initializing SD card... ");
5749
if (!SD.begin(csPin)) {
5850
Serial.println("Failed!");
@@ -62,23 +54,23 @@ void setup() {
6254

6355
// Effectively working only on ESP32
6456
myLog.begin(csPin);
65-
66-
myLog.setSizeLimit(1000, false);
57+
myLog.setSizeLimit(1000);
6758
myLog.setSizeLimitPerChunk(60);
6859
myLog.setFlusherCallback(senderHelp);
69-
somethingHappens.attach(eventFrequency, somethingHappening);
70-
60+
61+
Serial.println("Starting to log...");
62+
somethingHappens.attach(eventPeriod, somethingHappening);
7163
myLogRun.begin(true);
7264
}
7365

7466
void loop() {}
7567

7668
/**
77-
* Callback implementing the log flushing.
78-
* In this case log flushing is performed on the Serial interface, but you can easily
79-
* write your own implementation for Wifi connection.
80-
*
81-
* NOTE: This example simulates a channel failure.
69+
* Flush a chuck of logged records. To exemplify, the records are
70+
* flushed on Serial, but you are free to modify it to send data
71+
* over the network.
72+
*
73+
* NOTE: This example simulates a channel that sometimes may fail.
8274
*/
8375
bool senderHelp(char* buffer, int n){
8476
static int failPacketCounter = 0;

readme.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,21 @@ There are 2 main functions to be aware of: *append* and *flush*.
2929

3030
#### Append
3131

32-
bool append(String data)
32+
bool append(const char* record)
3333

34-
it creates and stores a *Record* containing the input data. You cannot log data containing non-printable characters, nor new line or carriage return. Return true if the data is saved, false otherwise.
34+
it creates and stores a *Record*. You cannot log data containing non-printable characters, nor new line or carriage return. Return true if the data is saved, false otherwise.
3535

3636
#### Flush
3737

3838
bool flush()
3939

4040
it calls your callback function which has the following prototype:
4141

42-
bool flusher(char* buffer, int n);
42+
bool flusher(char* chunk, int n);
4343

44-
where char* is a buffer that contains one or more records, *n* tells how many bytes is long this buffer. This function should return true is the buffer flush has succeeded, false otherwise (e.g. the server wasn't reachable). If true, the library deletes the flushed data and, if other data are available, it calls flusher() again, otherwise it stops. If false, the library stops the flushing process and preserves the unflushed data for the next flush().
44+
where *chunk* is a buffer that contains one or more records separated by '\0' char; *n* tells how many bytes is long the chunk, including '\0'. This function must return true is the buffer flush has succeeded, false otherwise (e.g. the server wasn't reachable). If true, the library deletes the flushed data and, if other data are available, it calls flusher() again, otherwise it stops. If false, the library stops the flushing process and preserves the unflushed data for the next flush().
4545

46-
This kind of packetization can be useful in various scenarios, especially when the log is very large and you cannot send everything in one shot. The max buffer size can be arbitrarily set at run-time, though a dedicated method.
46+
This kind of packetization can be useful in various scenarios, especially when the log is very large and you cannot send everything in one shot. The maximum size of a chunk can be set at run-time through *setSizeLimitPerChunk()*.
4747

4848
Please note that the flush() method guarantees that:
4949

0 commit comments

Comments
 (0)