SD nom de fichier .txt

Bonjour, dans le cadre d'un projet d'une petite station météo je souhaite écrire les informations récoltées sur une carte SD.
Lorsque je veux nommer mon fichier en .txt, je passe par des String, mais j'ai l'impression que le file.open ne prend pas en compte les string ? Si quelqu'un verrait le problème...

#include <SPI.h>
#include <SD.h>
int comptFichier = 1;
File myFile;

void setup() {
  Serial.begin(9600);
  while (!Serial) {
    ;
  }
  Serial.print("Initializing SD card...");
  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");
}

void loop() {
  ecrireSD();
  delay(5000);
}

void ecrireSD(){
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  String NomFichier = getRTCdate();
  Serial.println(NomFichier);
  myFile = SD.open(NomFichier, FILE_WRITE);
  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.println("testing 1, 2, 3.");
    // close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening");
  }
}

String getRTCdate(){
    String NomFichier1;
    NomFichier1 ="captures";
    NomFichier1+=String(comptFichier);
    NomFichier1+=".txt";
    comptFichier++;
    return NomFichier1;
}

ben n'utilisez pas de String :slight_smile:
sinon faites

  myFile = SD.open(NomFichier.c_str(), FILE_WRITE);

Alors j'ai fait la modif mais pas d'amélioration de ce côté, j'ai peut être mal fait les choses ?

void ecrireSD(){
  String NomFichier = getRTCdate();
  Serial.println(NomFichier);
  myFile = SD.open(NomFichier.c_str(), FILE_WRITE);
  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.println("testing 1, 2, 3.");
    // close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening");
  }
}

image

pour tester si la carte fonctionne, essayez déjà

void ecrireSD(){
  myFile = SD.open("test.txt", FILE_WRITE);
  if (myFile) {
    myFile.println("testing 1, 2, 3.");
    myFile.close();
    Serial.println("done.");
  } else {
    Serial.println("erreur");
  }
}

ensuite essayez de générer un fichier au format 8.3 (captures1 c'est 9 lettres), c'est nécessaire suivant le formatage de votre carte SD

Oui pas de soucis la carte marche lorsque je met directement entre guillemets, mais autre chose ca ne marche pas...

essayez avec un nom plus court

String getRTCdate(){
    String nom;
    nom ="cap";
    nom +=String(comptFichier);
    nom +=".txt";
    comptFichier++;
    return nom;
}

Oh avec des char vous dites ?

pas obligatoirement mais ce serait "mieux"

essayez avec la fonction getRTCdate() que j'ai donné en #6

Waw MERCI beaucoup, c'était bien la taille de la variable x)

cool :slight_smile:
amusez vous bien

La librairie SD ne prend que les noms de fichiers en 8.3 (8 caractères pour le nom + 3 caractères pour l'extension) quelque soit le format

The library supports FAT16 and FAT32 file systems on standard SD cards and SDHC cards. It uses short 8.3 names for files.
1 Like

effectivement, sinon il faut passer sur SdFat, plus complète, plus à jour, plus performante

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