0

My Arduino program produces debug/error output using methods like Serial.printf. I can see all this output when it's connected to the computer, but I want to also log all this same output to my board's SD card, so that I can read it later after my Arduino has been running out in the field, without being connected to the computer.

My Feather M0 board has an SD card, and I am already writing other data to it using SdFat library. That part is ok.

What I can't figure out is how to effectively log the same text both to the SD Card and to Serial output. I mean, without excessive boilerplate. Normally this wouldn't be hard, just make a method like: (pseudocode)

void logStuff(char text[]) {
  Serial.print(text);
  sdFatFile.print(text);
}

However, calling such a function in C is a lot of boilerplate. For example in case of printf, I would need something like this every time I want to log one piece of text:

char text[100];
sprintf(text, "line to log %d", someNumber);
logStuff(text);

Which is too much ceremony for debug calls peppered throughout my application.

Ideally I'd like to implement something like this:

void logStuff(char format[], ...varargs) {
  Serial.printf(format, ...varargs);
  sdFatFile.printf(formar, ...varargs);
}

Which you could call exactly like you'd call Serial.printf, but I don't know if that's possible to implement in C/C++.

Note, it would also be acceptable if my code logged EITHER to Serial OR to sdFatFile – that is, only to one target, not both at the same time – depending on whether Arduino is plugged in or not. But I'm not sure how to do that either, the types on which I would be calling printf (SdFile vs Serial) don't seem to have a common interface, even though the method invocations would be source-compatible.

9
  • it is a pure codding question and a 50 years old thing.
    – Juraj
    Oct 23 at 4:58
  • Not my fault that Arduino uses 50 years old programming language
    – Nikita
    Oct 23 at 5:00
  • this is the printf function you use: github.com/adafruit/ArduinoCore-samd/blob/master/cores/arduino/…
    – Juraj
    Oct 23 at 5:00
  • Internet has many knowlege on 50 years old things. and Stack sites require research before asking
    – Juraj
    Oct 23 at 5:01
  • 1
    I know how to use a printf function, that's not what my question is about. The question is specifically about printing to two arduino interfaces, SdFile and Serial. The internet has many things, but it doesn't have an answer to this question, not on SO, not elsewhere.
    – Nikita
    Oct 23 at 5:07
1

What works for me

#include <SD.h> //Load SD card library
#include<SPI.h> //Load SPI Library

void loop()
{
 Serial.print(tempSensor3);
//Open existing file on SD card named PTData.txt
mySensorData = SD.open("PTData.txt", FILE_WRITE);
  if (mySensorData) {
    mySensorData.print(tempSensor3);
    mySensorData.close();                    //close the file
  }
1
  • You're showing me how to print to serial, and separately how to print to a file. I know how to do both of those things the way you showed. What I DON'T know is how to do it in one printf-style function, which I can call in order to printf into both serial and a file at once.
    – Nikita
    Oct 23 at 4:40

Not the answer you're looking for? Browse other questions tagged or ask your own question.