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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
| #include <BluetoothSerial.h>
#include "FS.h"
#include <SPIFFS.h>
/* You only need to format SPIFFS the first time you run a
test or else use the SPIFFS plugin to create a partition
https://github.com/me-no-dev/arduino-esp32fs-plugin */
#define FORMAT_SPIFFS_IF_FAILED true
BluetoothSerial sBt;
//******************lecture SPIFFS**********************
void lectureFichier(fs::FS &fs, const char *path){
String tempo="";
Serial.printf("fichier lu: %s\r\n", path);
File file = fs.open(path);
if(!file || file.isDirectory()){
Serial.println("Erreur de lecture fichier");
return;
}
Serial.println("lu dans le fichier:");
while(file.available()){
tempo += (char)file.read();
}
Serial.println(tempo);
sBt.println(tempo);
file.close();
}
//*****************Ecriture SPIFFS***********************************
void writeFile(fs::FS &fs, const char * path, const char * message){
Serial.printf("Ecriture du fichier: %s\r\n", path);
File file = fs.open(path, FILE_WRITE);
if(!file){
Serial.println("Erreur d'ecriture du fichier");
return;
}
if(file.print(message)){
Serial.println("Fichier ecrit");
} else {
Serial.println("Erreur d'ecriture");
}
file.close();
}
//*********************commande Bluetooth recu********************
void analyseCommandeBt(String com){
char c = com.charAt(0);
switch (c){
case 'S': writeFile(SPIFFS, "/ssid.txt", com.substring(1).c_str());
break;
case 'P': writeFile(SPIFFS,"/pass.txt",com.substring(1).c_str());
break;
/*case 'T': enregTouches();
break;
case 'A': enregAlarme(com.substring(1));
break;
case 'R': enregAlarmHebdo(com.substring(1)); //format: Rhh:mm+J-'texte'
break;*/
case 'X': sBt.println("Commande recu: " + com);
readFile(SPIFFS,"/ssid.txt");
break;
case 'Y': readFile(SPIFFS,"/pass.txt");
default:
break;
}
sBt.println("Ok");
}
//*********************configuration*****************************
void setup(){
Serial.begin(115200);
sBt.begin("Matrice");
if(!SPIFFS.begin(FORMAT_SPIFFS_IF_FAILED)){
Serial.println("SPIFFS Mount Failed");
return;
}
}
//*******************boucle principale*****************************
void loop(){
char tempo;
String commande;
if(sBt.available()){
tempo = sBt.read();
if(tempo=='Z'){ //Z=commande
while(sBt.available()){
tempo=sBt.read();
commande += tempo;
}
}
Serial.println(commande);
analyseCommandeBt(commande);
}
delay(50);
} |
Partager