Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert String to type const char* using Arduino

I am using the Arduino library. I would like to log some data from a sensor, date-time stamp it and write it to a SD card.

To build the text file name I have tried

    String dataFileName = String(String(sedClock.getTime().year(),DEC) + 
                         String(sedClock.getTime().month(),DEC) + 
                         String(sedClock.getTime().day(),DEC) + 
                         String(sedClock.getTime().hour(),DEC) + 
                         String(sedClock.getTime().minute(),DEC) + 
                         String(sedClock.getTime().second(),DEC) + '_log.txt');

I would then like to log to that file using

      pinMode(SD_PIN,OUTPUT);
      dataFile = SD.open(dataFileName,FILE_WRITE);

But I get

    no matching function call to SDClass::open(String&, int) 
    candidates are: File SDClass::open(const char*,uint_8)

But it seems that Arduino string doesn't have the equivalent of

    (const char *) dataFileName.c_str()

So I can't figure out how to do the correct conversion

Any help would be greatly appreciated.

like image 589
Caustic Avatar asked Jul 23 '12 09:07

Caustic


1 Answers

Thanks for your help. The solution was

    char __dataFileName[sizeof(dataFileName)];
    dataFileName.toCharArray(__dataFileName, sizeof(__dataFileName));

    pinMode(SD_PIN,OUTPUT);
    dataFile = SD.open(__dataFileName,FILE_WRITE);
like image 196
Caustic Avatar answered Oct 22 '22 18:10

Caustic