SD card wiriting const char* error

Hello ,

I try to use the SD card in combination with an ESP32 Hardware is connected and it is working.
But when I try to run the next software from ESP32: Guide for MicroSD Card Module Arduino | Random Nerd Tutorials
with the function // appendFile(SD, "/test.txt", "button2 active\n "); it is running
when I try to change to appendFile(SD, "/test.txt", dataString);

I get the error message:
RTCenSD.ino:101:49: error: invalid cast from type 'String' to type 'const char*'
appendFile(SD, "/test.txt", dataString);
^~~~~~~~~~
What is wrong ?

#include<SPI.h>
#include<SD.h>
#include<Wire.h>
#include"RTClib.h"
#include<CircularBuffer.h>

RTC_DS1307 rtc;
String timestring;
int16_t interval = 1;  //Log to SD Card every 1 seconds
long timer;
#define chipSelect 5 //SD card
String DT[10];
int16_t i = 0;

void setup(){
  Serial.begin(115200);
  delay(2000);

  if(!SD.begin(chipSelect)) {  //SD CardSerial.println("Initializing SD card...");
    Serial.println("SD Card error");
    return;
  }else  Serial.println("card initialized");
  
  //RTC startup
  if(! rtc.begin()) Serial.println("No RTC found");
  else Serial.println("RTC clock found");
  if(! rtc.isrunning()) Serial.println("RTC is not configured");
   
  //initialize timestring-array
  for(int j = 0; j < 10; j++) {
    DT[j] = "99-99-99 99:99:99";
  }
  timer = millis();

  writeFile(SD, "/test.txt", "new file!!!\n ");

}

void loop(){
  // push each interval one dataset to queue
  if((timer + interval * 1000) < millis()) {
    timer = millis();
    get_time(); //Get time from RTC
    i++;
    if(i==10)i=0;
    write_data();   //Write value and Time to SD
  }
}

void get_time(){ //Read Time from RTCDateTime now = rtc.now();
  DateTime now = rtc.now();
  timestring = now.day();     timestring += "-";
  timestring += now.month();  timestring += "-";
  timestring += now.year();   timestring += " ";  
  timestring += now.hour();   timestring += ":";
  timestring += now.minute(); timestring += ":";
  timestring += now.second();  DT[i] = timestring;
  //Serial.println("i= "+ String(i) + " en DTstring= "+ DT[i]); //Serial.println("TS: "+ timestring);
}

void appendFile(fs::FS &fs, const char * path, const char * message) {
  Serial.printf("Appending to file: %s\n", path);

  File file = fs.open(path, FILE_APPEND);
  if (!file) {
    Serial.println("Failed to open file for appending");
    return;
  }
  if (file.print(message)) {
    Serial.println("Message appended");
  } else {
    Serial.println("Append failed");
  }
  file.close();
}

void writeFile(fs::FS &fs, const char * path, const char * message) {
  Serial.printf("Writing file: %s\n", path);

  File file = fs.open(path, FILE_WRITE);
  if (!file) {
    Serial.println("Failed to open file for writing");
    return;
  }
  if (file.print(message)) {
    Serial.println("File written");
  } else {
    Serial.println("Write failed");
  }
  file.close();
}

void write_data(){ //Write to SD card
  File dataFile = SD.open("/test.txt", FILE_WRITE);
  int16_t j = 0;
      String dataString="Datum en tijd is "+ DT[0] + "\r\n";
//   appendFile(SD, "/test.txt", "button2 active\n ");
      appendFile(SD, "/test.txt", dataString);
      Serial.println(dataString);
    dataFile.close();
}

The error message seems clear

The function expects an array of chars as the second argument but you are providing a String

The String library has a function named toCharArray() which will presumably do the conversion, but I am not a user of Strings so could be wrong

The error message is very clear:

invalid cast from type 'String' to type 'const char*'

The function you're using requires a "const char *" parameter, while you're using a "String":
if you are unsure of the difference perhaps this will help.

You shouldn't use this:

      String dataString="Datum en tijd is "+ DT[0] + "\r\n";

but either use a "char*" variable buffer like this one (I dimensioned it to 30, meaning the total string length must be 29 characters or less, including the trailing CR and LF):

  char dataString[30];
  strcpy(dataString, "Datum en tijd is ");
  strcat(dataString, DT[0]);
  strcat(dataString, "\r\n");
  appendFile(SD, "/test.txt", dataString);

or use sprintf() to build the line in a single statement:

  char dataString[30];
  sprintf(dataString, "Datum en tijd is %s\r\n", DT[0]);
  appendFile(SD, "/test.txt", dataString);

or convert "String" into "char*" using "c_str()" method:

      String dataString="Datum en tijd is "+ DT[0] + "\r\n";
      appendFile(SD, "/test.txt", dataString.c_str());
1 Like

Thanks docdoc that is a clear explanation.

you can use this:

appendFile(SD, "/test.txt", dataString.c_str());

instead of yours:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.