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();
}