How to print datalogger timestamp as string with fprintf?

Hello. I want to coding telemetry on Arduino GIGA. We do this with Arduino GIGA's mass storage interface. I get the value of a sensor along with the timestamp and print it to a txt file. However, when the fprintf function writes to the txt file, it writes the time as an symbol. There is no problem when we call the getlocaltime() function with the serial interface. Time information is displayed correctly. It only looks like this when writing to txt file. What could be the reason for this? How can I solve this problem?

#include <Arduino_USBHostMbed5.h>
#include <DigitalOut.h>
#include <FATFileSystem.h>
#include "mbed.h"
#include <mbed_mktime.h>
constexpr unsigned long printInterval { 1000 };
unsigned long printNow {};
int duman = A0; 
String duman_okunan="";
long lastTime = 0;

USBHostMSD msd;
mbed::FATFileSystem usb("veriler");
mbed::DigitalOut otg(PB_8, 1);

void setup() {
  Serial.begin(115200);
  pinMode(duman, INPUT);

  while (!Serial);
  
  while (!msd.connect()) {
    delay(1000);
  }
 
  Serial.println("Mounting USB device...");

  int err = usb.mount(&msd); // Mount USB device
  if (err) {
    Serial.print("Error mounting USB device ");
    Serial.println(err);
    while (1);
  }
  Serial.println("USB device mounted.");
}

void loop() {

  if (!msd.connected()) {
    msd.connect(); // Bağlantı kopmuşsa tekrar bağlanıyoruz
  }
  if (millis() - lastTime > 1000) {
    lastTime = millis();       
   
    FILE *f = fopen("/veriler/telemetry.txt", "a"); 
    if (f == NULL) {
      Serial.println("Dosya açılırken hata oluştu...");
    } else {
      Serial.println(getLocaltime());
 fprintf(f, "Zaman: %s, duman : %ld\n", getLocaltime(), duman_oku());
      fclose(f); // Close the file
      Serial.println("Veri kaydedildi...");
    }
    
  }
}
String getLocaltime()
{
    char buffer[32];
    tm t;
    _rtc_localtime(time(NULL), &t, RTC_4_YEAR_LEAP_YEAR_SUPPORT);
    strftime(buffer, 32, "%Y-%m-%d %k:%M:%S", &t);
    return String(buffer);
}
int duman_oku()    
{
  int duman_bilgi = analogRead(duman);  
  return duman_bilgi;
}

telemetry

getLocaltime() willl return a timestamp in some kind of number format. You need to convert this into the different elements of a time; e.g. hours and minutes, and then output those separately. Look at the definition of getLocaltime() to see what it outputs and how to interpret it.

1 Like

I solved the problem... I used C_str and wrote the timestamp to the file...