Wednesday, February 3, 2016

Logger for Arduino over Serial Port

I am bit tired of using Serial as logger - mainly for two reasons: it does not support sprintf syntax and string are being held in RAM. For this reason I've implemented new library: https://github.com/maciejmiklas/ArdLog

ArdLog serves as simple logger for Arduino that creates formatted messages over Serial:
  • Each message has timestamp.
  • Each message within single loop has the same timestamp, so that you can logically connect activities together.
  • Messages can be formatted using sprintf syntax.
  • Text for the messages is being held in PROGMEM.

Installation

In order to install ArdLog you have to download desired release and unpack in into folder containing Arduino libraries. The is the result on MacOS:

$ pwd
/Users/fred/Documents/Arduino/libraries/ArdLog

$ ls
ArdLog.cpp ArdLog.h   LICENSE    README.md

Configuration

  • Logger is disabled by default, in order to enable it set LOG to true.
  • Messages are created over default Serial port. You can choose alternative port by setting: USE_SERIAL_1, USE_SERIAL_2 or USE_SERIAL_3 to true.
  • In order to print current time for each message set USE_CURRENT_TIME to true. By default logger will sample time only once at the beginning of each loop.

Getting up and running  

  1. Choose suitable configuration in ArdLog.h. In most cases you have to only set LOG to true.
  2. Call log_setup() in setup() method - this will initialize serial port.
  3. Call log_cycle() at the beginning of each loop() - it will sample current time.
  4. Put log messages into #if LOG log(F("....") #endif - once logger is disabled, it will not waste RAM and CUP.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <ArdLog.h>

uint16_t loopIdex = 0;

void setup() {
  log_setup();
}

void loop() {
  log_cycle();

  loopIdex++;

  #if LOG
    log(F("**** Loop %d ****"), loopIdex);
    log(F("T1 = %ld"), millis());
  #endif

  delay(100);

  #if LOG
    log(F("T2 = %ld"), millis());
  #endif

  delay(1000);
}
This is output created by example above:

>>[000-00:00:00,000]-> Logger initialized, free RAM: 1537
>>[000-00:00:00,003]-> Free RAM: 1527
>>[000-00:00:00,003]-> **** Loop 1 ****
>>[000-00:00:00,003]-> T1 = 9
>>[000-00:00:00,003]-> T2 = 112
>>[000-00:00:01,113]-> **** Loop 2 ****
>>[000-00:00:01,113]-> T1 = 1114
>>[000-00:00:01,113]-> T2 = 1215
>>[000-00:00:02,215]-> **** Loop 3 ****
>>[000-00:00:02,215]-> T1 = 2217
>>[000-00:00:02,215]-> T2 = 2318
>>[000-00:00:03,319]-> **** Loop 4 ****
>>[000-00:00:03,319]-> T1 = 3320
>>[000-00:00:03,319]-> T2 = 3421
>>[000-00:00:04,422]-> **** Loop 5 ****
>>[000-00:00:04,422]-> T1 = 4423
>>[000-00:00:04,422]-> T2 = 4525

No comments:

Post a Comment