1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| #include <arduino.h>
#include <FS.h>
#include <SPIFFS.h>
#define FORMAT_SPIFFS_IF_FAILED true
bool go = true;
void ecritFichier(fs::FS &fs, const char *dir, const char *message){
Serial.printf("%s\n\r",message);
File fic=fs.open(dir,"w");
int lg = fic.print(message);
fic.close();
Serial.print("Nb caracteres: "); Serial.println(lg);
}
void litFichier(fs::FS &fs, const char *dir){
String tempo ="";
Serial.printf("%s\n\r",dir);
File fic=fs.open(dir,"r");
if(!fic || fic.isDirectory()){
Serial.println("Erreur lecture du fichier");
fic.close();
return;
}
else{
while(fic.available()){
tempo+=(char)fic.read();
}
Serial.println(tempo);
}
fic.close();
}
void setup() {
Serial.begin(9600);
Serial.setTimeout(3000);
if(!SPIFFS.begin(FORMAT_SPIFFS_IF_FAILED))
Serial.println("SPIFFS MOUNT FAILED");
}
/*pour écrire: Wnxxxxxxxxxxx n => n° de fichier > "Pn.txt"
x => caractères écrits en SPIFFS
**
pour lire: Rn > lecture du fichier Pn en SPIFFS
*/
void loop() {
String tempo=""; char dir[8];
if(Serial.available() >0){
tempo = Serial.readStringUntil(13);
tempo += '\0';
if(tempo.charAt(0)=='W'){ //pour Write
Serial.printf(" [%s]\n\r", tempo.c_str());
sprintf(dir,"/P%c.txt%c",tempo.charAt(1),'\0');
int lg = tempo.length();
char msg[lg];
for(int i=0; i<lg-2; i++){
msg[i] = tempo.charAt(i+2);
}
ecritFichier(SPIFFS,dir,msg);
}
else if(tempo.charAt(0)=='R'){ //pour Read
sprintf(dir,"/P%c.txt%c",tempo.charAt(1),'\0');
litFichier(SPIFFS,dir);
}
}
} |
Partager