Hi all,
Striking hard for this scenario, if possible please direct me.
I'm collecting few sensor data and storing them in SD. the current format is,
28/03/2024 23:34:54 10.01 4.81 5.08 10.13 3.33 5.84 2.00 -0.38 4.59 1.45 -0.74 4.49 -3.85 PASS FAIL FAIL PASS FAIL FAIL ;
28/03/2024 23:35:03 10.01 4.81 5.08 10.13 3.33 5.84 2.00 -0.35 4.63 1.57 -0.64 4.57 -3.62 PASS FAIL FAIL PASS FAIL FAIL ;
likewise, Storing 500+ lines in a .txt file. Now, wanna transfer those data weekly once in to a pendrive.
So far I tried, I can copy 2 rows into a single Char Array and save them in pendrive. When i try to copy all, I can copy all 500 lines in to a single "String" but unable to copy / convert it to char array as the buffer limitation.
Can you please suggest a way to make the string to split and send as char array (Thats how pendrive takes the data) to pendrive though a loop may be... so that all 500 lines can be copied?
(NOTE: We can go with any format/delimiters for the texts in SD).
Current Main code:
//in gloabal declaration
char q;
String we;
char adat2[200] = "";
//in the loop
if (B2_Val == LOW) {
DateTime now = rtc.now();
sprintf(Disp_Date_Char,"%02u/%02u/%04u",now.day(),now.month(),now.year());
appendFile(SD, "/hello.txt", Disp_Date_Char);
appendFile(SD, "/hello.txt", " ");
sprintf(Disp_Time_Char,"%02u:%02u:%02u",now.hour(),now.minute(),now.second());
appendFile(SD, "/hello.txt", Disp_Time_Char);
appendFile(SD, "/hello.txt", " ");
appendFile(SD, "/hello.txt", Act1);
appendFile(SD, "/hello.txt", " ");
appendFile(SD, "/hello.txt", Act2);
appendFile(SD, "/hello.txt", " ");
appendFile(SD, "/hello.txt", Act3);
appendFile(SD, "/hello.txt", " ");
appendFile(SD, "/hello.txt", Act4);
appendFile(SD, "/hello.txt", " ");
appendFile(SD, "/hello.txt", Act5);
appendFile(SD, "/hello.txt", " ");
appendFile(SD, "/hello.txt", Act6);
appendFile(SD, "/hello.txt", " ");
appendFile(SD, "/hello.txt", Toler);
appendFile(SD, "/hello.txt", " ");
appendFile(SD, "/hello.txt", Devi1);
appendFile(SD, "/hello.txt", " ");
appendFile(SD, "/hello.txt", Devi2);
appendFile(SD, "/hello.txt", " ");
appendFile(SD, "/hello.txt", Devi3);
appendFile(SD, "/hello.txt", " ");
appendFile(SD, "/hello.txt", Devi4);
appendFile(SD, "/hello.txt", " ");
appendFile(SD, "/hello.txt", Devi5);
appendFile(SD, "/hello.txt", " ");
appendFile(SD, "/hello.txt", Devi6);
appendFile(SD, "/hello.txt", " ");
Stat_Str1.toCharArray(Stat1_Char, Stat_Str1.length() + 1);
appendFile(SD, "/hello.txt", Stat1_Char);
appendFile(SD, "/hello.txt", " ");
Stat_Str2.toCharArray(Stat2_Char, Stat_Str2.length() + 1);
appendFile(SD, "/hello.txt", Stat2_Char);
appendFile(SD, "/hello.txt", " ");
Stat_Str3.toCharArray(Stat3_Char, Stat_Str3.length() + 1);
appendFile(SD, "/hello.txt", Stat3_Char);
appendFile(SD, "/hello.txt", " ");
Stat_Str4.toCharArray(Stat4_Char, Stat_Str4.length() + 1);
appendFile(SD, "/hello.txt", Stat4_Char);
appendFile(SD, "/hello.txt", " ");
Stat_Str5.toCharArray(Stat5_Char, Stat_Str5.length() + 1);
appendFile(SD, "/hello.txt", Stat5_Char);
appendFile(SD, "/hello.txt", " ");
Stat_Str6.toCharArray(Stat6_Char, Stat_Str6.length() + 1);
appendFile(SD, "/hello.txt", Stat6_Char);
appendFile(SD, "/hello.txt", " ");
appendFile(SD, "/hello.txt", ";");
appendFile(SD, "/hello.txt", "\n");
readFile(SD, "/hello.txt");
Serial.println(we);
}
// outside loop
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 readFile(fs::FS &fs, const char *path) {
Serial.printf("Reading file: %s\n", path);
File file = fs.open(path);
if (!file) {
Serial.println("Failed to open file for reading");
return;
}
Serial.print("Read from file: ");
we = "";
while (file.available()) {
//Serial.write(file.read());
q=file.read();
we.concat(q);
}
file.close();
}
now i have all the data from SD ~500+rows in String variable "we"
// for pendrive I input number "2" to appended
case 50: //2
we.toCharArray(adat2, we.length());
printInfo("COMMAND2: Append data to file: TEST1.TXT"); // Append data to the end of the file.
flashDrive.setFileName("TEST1.TXT"); //set the file name
if(flashDrive.openFile() == ANSW_USB_INT_SUCCESS){ //open the file
flashDrive.moveCursor(CURSOREND); //if the file exist, move the "virtual" cursor at end of the file, with CURSORBEGIN we actually rewrite our old file
//flashDrive.moveCursor(flashDrive.getFileSize()); // is almost the same as CURSOREND, because we put our cursor at end of the file
}
for(int a = 0; a < 2; a++){ //write text from string(adat) to flash drive 1 timee
if(flashDrive.getFreeSectors()){ //check the free space on the drive
flashDrive.writeFile(adat2, strlen(adat2)); //string, string length
} else {
printInfo("Disk full");
}
}
flashDrive.closeFile(); //at the end, close the file
printInfo("Done!");
break;
here if i keep the sd data only 2 rows, those data are copied into pendrive.
more data in SD, lead it to reboot and not copying.
char adat2[200] = ""; -> I hope the size can't be increased in thousands. so please help me to break the String, and send as packages.