Not every String gets saved to .csv file to SD card

Hello,

im using a ESP32. I already wrote code for saving strings to a SD card (It works perfectly and saves everything). I use the SD libary.

Now i get a pointer to the array which has all the values i want to save.
The header String gets saved to the SD card no problem, but the actual datastring doesnt get saved. Both are Strings, both use the same function to save, but the header saves onto the SD card and the datastring doesnt.

To clarify this. When i open the .csv file i just see "ID; Value". So no data gets saved but only the header

This is kind of a big project so i will just post the function, the setup and the class.

I thank you in advance for helping me with my problem :slight_smile:

Another question to this post:

Could i change the file name while the program is running? Like have data1.csv, data2.csv, data3.csv and so on

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

  p_EKG_state_1 = &EKG_state_1;

  pinMode(CS_Pin, OUTPUT);
  if(!SD.begin(CS_Pin))
    {
      Serial.println("Card failed or not present");
    }

  EKG_data_transmision.matrixData = SD.open("/data.csv", FILE_APPEND);  //erstelle data.csv datei

  if(EKG_data_transmision.matrixData)
    {
    String header = "ID; Value;";
    EKG_data_transmision.matrixData.println(header);
    EKG_data_transmision.matrixData.close();
    Serial.println(header);
    Serial.println("card init sucess");
    }
  else
    {
    Serial.println("Couldnt open vector data file");
    }
  

}

Here is the function where everything should go down.

bool  EKG_data_transmision::write_matrix_to_SD(unsigned short *array_pointer) 
{

   if(!SD.begin(CS_Pin))
  {
    Serial.println("Card failed or not present");
    return 1;
  }
  matrixData = SD.open("/data.csv", FILE_APPEND);  //erstelle data.csv datei
  
  if(matrixData)
  {
    //sd karte immer noch da und schau ob data.csv exisitiert
    
    while(matrix_coloum < 3070)
    {
      if(matrix_coloum == 1024 || matrix_coloum == 2048)
      {
          header = "ID; Value;\n";
          matrixData.println(header);   
      }
      dataString = String(matrix_coloum) + "; " + "\n"; // + String(*array_pointer) 
      matrixData.println(dataString);
      //Serial.println("This is datastring:");
      Serial.println(dataString);
      array_pointer++;
      matrix_coloum++;
    } 
    
  }
   
  else{
    Serial.println("Error writing to file !");
}
  matrixData.close(); // close the file
  
  return 0;
}

Here is the class

#include "Arduino.h"
#include <WiFi.h>
#include "SD.h"
#include "SPI.h"
#include <vector>
#include <string>


class EKG_data_transmision
{
  private:
    
    String dataString;
    String header;
    unsigned short matrix_coloum = 0;
    
    
    
  public:
    EKG_data_transmision();

    File matrixData;

    //Def Funktions:
    bool write_matrix_to_SD(unsigned short *array_pointer);
    bool delete_matrix_from_SD(unsigned int record);
    bool send_record_to_DB(unsigned int record);
    bool connect_wifi(char* ssid, char *password);
    unsigned int count_record_from_SD();
    unsigned int get_last_record_from_SD();

    void set_sendingprogress(unsigned int record);
    unsigned int get_sendingprogress(unsigned int record);
    void set_wifi_password(char* password);
    char* get_wifi_password();
    void set_wifi_ssid(char* ssid);
    char* get_wifi_ssid();

};

Maybe you should try to use write instead of println when writing into the file.

To change the name of the file, you can use a number that you store in the EEPROM , the NVS or in a file in the SPIFFS of the ESP32 and which will be kept even if the ESP32 is off. Then each time you want a new name, just read the number, add one, store it again and use it to create the new filename

Every tutorial i saw for datalogging used the println() function but i am gonna try it.

This is the error i get. It seems like the write() function doesnt get recognized but the SD.h libary is included.
Could i even use strings for the write() function or do i have to use char?

Thanks for the help :slight_smile:

sketch\EKG_data_transimission.cpp: In member function 'bool EKG_data_transmision::write_matrix_to_SD(short unsigned int*)':

EKG_data_transimission.cpp:32:34: error: no matching function for call to 'fs::File::write(String&)'

           matrixData.write(header);   

                                  ^

In file included from C:\Users\Valentin\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4\libraries\SD\src/SD.h:17:0,

                 from sketch\EKG_data_transmision.h:3,

                 from sketch\EKG_data_transimission.cpp:2:

C:\Users\Valentin\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4\libraries\FS\src/FS.h:54:12: note: candidate: virtual size_t fs::File::write(uint8_t)

     size_t write(uint8_t) override;

            ^

C:\Users\Valentin\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4\libraries\FS\src/FS.h:54:12: note:   no known conversion for argument 1 from 'String' to 'uint8_t {aka unsigned char}'

C:\Users\Valentin\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4\libraries\FS\src/FS.h:55:12: note: candidate: virtual size_t fs::File::write(const uint8_t*, size_t)

     size_t write(const uint8_t *buf, size_t size) override;

            ^

C:\Users\Valentin\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4\libraries\FS\src/FS.h:55:12: note:   candidate expects 2 arguments, 1 provided

EKG_data_transimission.cpp:35:34: error: no matching function for call to 'fs::File::write(String&)'

       matrixData.write(dataString);

                                  ^

In file included from C:\Users\Valentin\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4\libraries\SD\src/SD.h:17:0,

                 from sketch\EKG_data_transmision.h:3,

                 from sketch\EKG_data_transimission.cpp:2:

C:\Users\Valentin\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4\libraries\FS\src/FS.h:54:12: note: candidate: virtual size_t fs::File::write(uint8_t)

     size_t write(uint8_t) override;

            ^

C:\Users\Valentin\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4\libraries\FS\src/FS.h:54:12: note:   no known conversion for argument 1 from 'String' to 'uint8_t {aka unsigned char}'

C:\Users\Valentin\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4\libraries\FS\src/FS.h:55:12: note: candidate: virtual size_t fs::File::write(const uint8_t*, size_t)

     size_t write(const uint8_t *buf, size_t size) override;

            ^

C:\Users\Valentin\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4\libraries\FS\src/FS.h:55:12: note:   candidate expects 2 arguments, 1 provided

Multiple libraries were found for "SD.h"
 Used: C:\Users\Valentin\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4\libraries\SD
 Not used: C:\Program
 Not used: C:\Users\Valentin\Documents\Arduino\libraries\SD
Multiple libraries were found for "FS.h"
 Used: C:\Users\Valentin\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4\libraries\FS
Multiple libraries were found for "SPI.h"
 Used: C:\Users\Valentin\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4\libraries\SPI
Multiple libraries were found for "WiFi.h"
 Used: C:\Users\Valentin\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4\libraries\WiFi
 Not used: C:\Program
exit status 1
no matching function for call to 'fs::File::write(String&)'