Hello everyone,
I made two functions for data logging in my webserver project, and I would like another people opnion about it.
I used this functions to replace the Serial.print and Serial.println, to print the text to the serial and log it to the SD Card (both with timeStamps).
printSDln("Initializing Arduino", true);
void printSD(char *data, boolean stamp) {
logFile = SD.open("logs.txt", FILE_WRITE);
if (logFile) {
if (stamp == true) {
char timeStamp[22];
sprintf(timeStamp, "[%02d/%02d/%02d][%02d:%02d:%02d] ", day(), month(), (year() - 2000), hour(), minute(), second());
Serial.print(timeStamp);
logFile.print(timeStamp);
}
logFile.print(data);
Serial.print(data);
logFile.close();
}
else {
Serial.println("printSD failed");
}
}
void printSDln(char *data, boolean stamp) {
logFile = SD.open("logs.txt", FILE_WRITE);
if (logFile) {
if (stamp == true) {
char timeStamp[22];
sprintf(timeStamp, "[%02d/%02d/%02d][%02d:%02d:%02d] ", day(), month(), (year() - 2000), hour(), minute(), second());
Serial.print(timeStamp);
logFile.print(timeStamp);
}
Serial.println(data);
logFile.println(data);
logFile.close();
}
else {
Serial.println("printSDln failed");
}
}
The only problem I have is printing / logging byte's, int's etc., since I passed it as a char array.
I don't know how to made a function that could print "any type"... I kinda "handled" it using itoa() for int's, but byte's I couldn't even figure anything.
int randomNumber = random(0, 60);
char Number [2];
itoa(randomNumber, Number, 10);
byte IP [4] = {192, 168, 0, 1}
Serial.print("Client IP: ");
for (int i = 0; i < 4; i++) {
Serial.print(clientIP[i]);
if (i < 3 ) {
Serial.print(".");
}
else {
Serial.println();
}