Bonjour bonjour, je fais une sorte de centrale domotique avec des ESP8266, toute la partie en local fonctionnait jusqu'à ce que j'implémente la lib d'Arduino Cloud. En gros j'ai un ESP qui fait office de serveur, il se connecte à internet pour récupérer l'heure via NTP (j'utilise directement UDP car j'ai eu des problèmes de compatibilité avec la lib NTPClient), je mets à jour mon horloge RTC. L'ESP démarre une autre session UDP pour communiquer avec les clients via le point d'Accès WiFi qu'il démarre. Et donc tout ça ça fonctionnait : la communication entre ESP, la gestion de scénario, la carte SD, l'horloge. Mais j'avais besoin de pouvoir contrôler mon ESP, donc j'ai utilisé Arduino IoT Cloud, mais depuis mon point d'accès WiFi bug, il s'initialise, mais dès que Arduino Cloud se connecte, mon AP devient inopérant, impossible de s'y connecter puis il disparait carrément. En fouillant un peu j'ai cru comprendre que la lib ArduinoCloud n'était pas un chef d'oeuvre de conception. D'ailleurs je sais que j'aurais "plus simple" à connecter chaque client à Internet pour les contrôler depuis là, mais j'aimerais garder la possibilité de le faire fonctionner en local.
#include <SDCustom.h>
#include <Dipher.h>
#include <RTClib.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include "secrets.h"
#include "thingProperties.h"
WiFiUDP udp;
WiFiUDP NTPudp;
RTC_DS3231 _rtc;
IPAddress ntpServerIP(145, 238, 203, 10);
bool RTCUpdated = false;
int previousMillis = 0;
int connectedTime = 0;
String previousHour = "0";
String code1 = "xx";
String code2 = "xx";
String crypted;
//Variable IoT
String path;
SD_File file;
String filepath_array[21];
String client_array[20];
String actualClient;
String FoF_array[21];
String _sourcePath;
String _pasteFoF;
String _pasteCoC;
char daysOfTheWeek[7][12] = {
"Dimanche",
"Lundi",
"Mardi",
"Mercredi",
"Jeudi",
"Vendredi",
"Samedi"
};
void setup() {
isConnected = false;
Serial.begin(9600);
Serial.println();
Wire.begin(4,5);
pinMode(CS_Pin, OUTPUT);
udp.begin(localPort);
NTPudp.begin(123);
Init();
}
void loop() {
ArduinoCloud.update();
int currentMillis = millis();
if(currentMillis - connectedTime > 1000){
connectedTime = currentMillis;
if(isConnected == false){
isConnected = true;
} else {
isConnected = false;
}
}
if(!SD.exists("racine/scenario")){
Serial.print("Redémarrage");
for(int i = 0; i < 3; i++){
delay(500);
Serial.print(".");
}
delay(500);
Serial.println();
Init();
}
DateTime now = _rtc.now();
if(now.hour() == 15 && now.minute() == 3 && RTCUpdated == false){
RTCUpdated = true;
updateRTCFromNTP();
}
if(now.hour() == 15 && now.minute() == 4 && RTCUpdated == true){
RTCUpdated = false;
}
packetReceive();
scenario();
}
void packetReceive(){
int packetSize = udp.parsePacket();
bool error = false;
if(packetSize > 0){
char packetBuffer[255];
String ID;
String Data;
String param_Value[20];
String param[20];
String value[20];
IPAddress remoteIP = udp.remoteIP();
//Lecture du packet
if(packetSize) {
int len = udp.read(packetBuffer, 255);
if(len > 0) packetBuffer[len] = 0;
}
String _packetBuffer = pDecrypt(String(packetBuffer));
Serial.println("Dipher : " + _packetBuffer);
//Fonction de séparation des éléments du message
if(_packetBuffer.indexOf('?') > -1){
ID = _packetBuffer.substring(0, _packetBuffer.indexOf('?'));
Data = _packetBuffer.substring(_packetBuffer.indexOf('?') + 1);
} else if(_packetBuffer.indexOf('!') > -1){
String _packetBuffer = String(packetBuffer);
ID = _packetBuffer.substring(0, _packetBuffer.indexOf('!'));
Data = _packetBuffer.substring(_packetBuffer.indexOf('!') + 1);
} else {
Serial.println("----Code : 0xFF----");
code1 = "0x";
code2 = "FF";
error = true;
return;
}
int dataIndexed = index(Data, param_Value, '&');
for(int i = 0; i < dataIndexed; i++){
param[i] = param_Value[i].substring(0, param_Value[i].indexOf('='));
value[i] = param_Value[i].substring(param_Value[i].indexOf('=') + 1);
}
Serial.print("-Client : ");
Serial.print(ID);
Serial.print(" / IP : ");
Serial.println(remoteIP);
//-------------------------------------Traitement du packet-------------------------------------
String filepath;
SD_File cFile;
//Fonction quand le client s'initialise auprès du serveur
if(param[0] == "payload" && param[1] == NULL && _packetBuffer.indexOf('?') > -1 && _packetBuffer.indexOf('!') == -1 && error == false){
Serial.println("-Initialisation du Client");
String payload[20];
int payload_INT = index(value[0], payload, '|');
if(!SD.exists(ID)){
Serial.println("-Création du dossier Client");
SD.mkdir(ID);
filepath = "racine/" + ID + "/payload";
SD.mkdir(filepath);
filepath = "racine/" + ID + "/IP.ip";
cFile = SD.open(filepath, FILE_WRITE);
cFile.print(remoteIP.toString());
cFile.close();
for(int i = 0; i < payload_INT; i++){
filepath = "racine/" + ID + "/payload/" + payload[i];
SD.mkdir(filepath);
filepath += "/recipients.rpt";
cFile = SD.open(filepath, FILE_WRITE);
cFile.close();
}
Serial.println("----Code : 1xC0----");
code1 = "1x";
code2 = "C0";
} else {
Serial.println("-Mise à jour du dossier Client");
filepath = "racine/" + ID + "/payload";
if(SD.exists(filepath)) SD.mkdir(filepath);
filepath = "racine/" + ID + "/IP.ip";
if(SD.exists(filepath)) SD.remove(filepath);
cFile = SD.open(filepath, FILE_WRITE);
cFile.print(remoteIP);
cFile.close();
for(int i = 0; i < payload_INT; i++){
filepath = "racine/" + ID + "/payload/" + payload[i];
if(!SD.exists(filepath)){
Serial.print(" Dossier manquant : ");
Serial.println(payload[i]);
SD.mkdir(filepath);
filepath += "/recipients.rpt";
cFile = SD.open(filepath, FILE_WRITE);
cFile.close();
}
}
Serial.println("----Code : 1xC1----");
code1 = "1x";
code2 = "C1";
}
} else if(param[0] != "payload" && _packetBuffer.indexOf('!') > -1 && _packetBuffer.indexOf('?') == -1 && error == false) {
Serial.println("-Requête d'action Client");
if(!SD.exists(ID)){
Serial.println("----Code : 0xF0----");
code1 = "0x";
code2 = "F0";
error = true;
goto errorOut;
}
for(int i = 0; i < dataIndexed; i++){
String filepath1 = "racine/" + ID + "/payload/" + param[i];
String filepath2 = "racine/" + ID + "/payload/" + param[i] + "/recipients.rpt";
if(!SD.exists(filepath1) || !SD.exists(filepath2)){
Serial.println("----Code : 0xFF----");
code1 = "0x";
code2 = "FF";
error = true;
goto errorOut;
}
}
for(int i = 0; i < dataIndexed; i++){
filepath = "racine/" + ID + "/payload/" + param[i] + "/recipients.rpt";
cFile = SD.open(filepath, FILE_READ);
String _recipients = "";
while(cFile.available() > 0){
_recipients += cFile.readString();
}
String recipients[20];
int recipients_INT = index(_recipients, recipients, '|');
for(int u = 0; u < recipients_INT; u++){
String pSend = param[i];
String vSend = value[i];
filepath = "racine" + recipients[u] + "/IP.ip";
cFile = SD.open(filepath, FILE_READ);
String _IP = "";
while(cFile.available() > 0){
_IP += cFile.readString();
}
crypted = pEncrypt("Server?&" + pSend + "=" + vSend + "&\r\n");
Serial.println("Dipher : " + crypted);
IPAddress ip;
ip.fromString(_IP);
udp.beginPacket(ip, localPort);
udp.print(crypted);
udp.endPacket();
}
}
Serial.println("----Code : 1xC2----");
code1 = "1x";
code2 = "C2";
}
errorOut:
crypted = pEncrypt("Server?" + code1 + code2 + "\r\n");
Serial.println("Dipher : " + crypted);
udp.beginPacket(remoteIP, localPort);
udp.print(crypted);
udp.endPacket();
}
}
int index(String Data, String array[20], char limiters){
int dataIndexed = 0;
for(int i = 0; Data.indexOf(limiters) != Data.lastIndexOf(limiters); i++){
Data.remove(0, 1);
array[i] = Data.substring(0, Data.indexOf(limiters));
Data.remove(0, Data.indexOf(limiters));
dataIndexed++;
}
return dataIndexed;
}
void Init(){
if (!SD.begin(CS_Pin) || !SD.exists("racine/scenario")) {
Serial.println("Erreur avec la carte µSD !");
Serial.println("En attente de connexion avec la carte µSD");
while(!SD.exists("racine/scenario")){
SD.begin(CS_Pin);
Serial.print(".");
delay(500);
}
Serial.println();
}
Serial.println("#---------------------------------------------------#");
Serial.println("Connexion avec la carte SD établie");
Serial.println("#---------------------------------------------------#");
RTCinit();
DateTime now = _rtc.now();
if(now.year() == 1900){
Serial.println("Erreur de synchronisation de l'horloge RTC !");
while(now.year() == 1900){
DateTime now = _rtc.now();
Serial.print(".");
delay(500);
updateRTCFromNTP();
}
Serial.println();
}
Serial.println("Horloge RTC synchronisée");
Serial.println("#---------------------------------------------------#");
WiFi.mode(WIFI_AP_STA);
Serial.print("Configuration du Point d'Accès : ");
Serial.println(WiFi.softAPConfig(local_IP, gateway, subnet) ? "OK" : "Erreur !");
Serial.print("Point d'Accès WiFi : ");
Serial.println(WiFi.softAP(APssid, APpassword) ? "OK" : "Erreur !");
Serial.print(F("Serveur UDP : "));
Serial.println(WiFi.softAPIP());
Serial.println("#---------------------------------------------------#");
initProperties();
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
Serial.println("Connexion à Arduino IoT Cloud établie");
Serial.println("#---------------------------------------------------#");
Serial.println("Démarrage programme : ");
}
void RTCinit(){
Serial.println("Connexion au WiFi : ");
WiFi.begin(STA_ssid, STA_password);
while(!WiFi.isConnected()){
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("#---------------------------------------------------#");
Serial.println("Connexion au résau WiFi établie");
if (!_rtc.begin()) {
Serial.println("Erreur avec l'horloge RTC");
while (!_rtc.begin()){
Serial.print(".");
delay(500);
}
Serial.println();
}
Serial.println("Connexion avec l'horloge RTC établie");
_rtc.adjust(DateTime(1900, 1, 1, 3, 0, 0));
Serial.println("Synchronisation de l'horloge RTC");
updateRTCFromNTP();
DisableWifi();
}
void updateRTCFromNTP() {
char _packetBuffer[48];
// Envoie une requête au serveur NTP
memset(_packetBuffer, 0, sizeof(_packetBuffer));
_packetBuffer[0] = 0b11100011; // En-tête NTP
NTPudp.beginPacket(ntpServerIP, 123); // Port NTP standard 123
NTPudp.write(_packetBuffer, 48);
NTPudp.endPacket();
int cb = 0;
while(cb == 0){
cb = NTPudp.parsePacket();
}
if (cb) {
NTPudp.read(_packetBuffer, 48);
// Convertit les octets reçus en valeurs d'heure
unsigned long highWord = word(_packetBuffer[40], _packetBuffer[41]);
unsigned long lowWord = word(_packetBuffer[42], _packetBuffer[43]);
unsigned long secsSince1900 = highWord << 16 | lowWord;
const unsigned long seventyYears = 2208988800UL;
unsigned long epoch = secsSince1900 - seventyYears;
int annee, mois, jour, heure, minute, seconde;
epochToDateTime(epoch, &annee, &mois, &jour, &heure, &minute, &seconde);
_rtc.adjust(DateTime(annee, mois, jour, heure, minute, seconde));
// Affiche les valeurs obtenues
Serial.println();
Serial.print("Année : ");
Serial.print(annee);
Serial.print(" / Mois : ");
Serial.print(mois);
Serial.print(" / Jour: ");
Serial.println(jour);
Serial.print("Heure: ");
Serial.print(heure);
Serial.print(":");
Serial.print(minute);
Serial.print(":");
Serial.println(seconde);
}
}
void scenario(){
SD_File file;
DateTime now = _rtc.now();
String day = daysOfTheWeek[now.dayOfTheWeek()];
String hours = String(now.hour());
String minutes;
if(now.minute() < 10){
minutes = "0" + String(now.minute());
} else {
minutes = String(now.minute());
}
String heure_minutes = hours + minutes;
if(previousHour != heure_minutes){
previousHour = heure_minutes;
String filepath = "racine/scenario/" + day + "/" + heure_minutes;
for(int i = 0; SD.exists(filepath); i++){
Serial.println("Scénario : " + hours + ":" + minutes);
String filepath2;
filepath = "racine/scenario/" + day + "/" + hours + minutes + "/" + i + ".scn";
if(!SD.exists(filepath)){
Serial.println("----Code : 2xF2----");
break;
}
file = SD.open(filepath, FILE_READ);
String scn = "";
while(file.available()){
scn += file.readString();
}
String ID_Charge[20];
index(scn, ID_Charge, '|');
filepath2 = "racine/" + ID_Charge[0] + "/IP.ip";
Serial.println(filepath2);
IPAddress ip;
String _IP = "";
if(!SD.exists(filepath2)){
Serial.println("----Code : 2xF1----");
break;
} else {
file = SD.open(filepath2, FILE_READ);
while(file.available()){
_IP += file.readString();
}
crypted = pEncrypt("Server:" + ID_Charge[1] + "\r\n");
Serial.println("Dipher : " + crypted);
ip.fromString(_IP);
udp.beginPacket(ip, localPort);
udp.print(crypted);
udp.endPacket();
}
}
}
}
void epochToDateTime(unsigned long epoch, int *year, int *month, int *day, int *hour, int *minute, int *second) {
const unsigned long SECONDS_FROM_1900_TO_1970 = 2208988800UL;
epoch += SECONDS_FROM_1900_TO_1970; // Convertir epoch depuis 1900 vers epoch depuis 1970
*second = epoch % 60;
unsigned long epochAfterSecond = epoch / 60;
*minute = epochAfterSecond % 60;
unsigned long epochAfterMinute = epochAfterSecond / 60;
*hour = (epochAfterMinute % 24) + 2;
unsigned long epochAfterHour = epochAfterMinute / 24;
unsigned long epochAfterDay = epochAfterHour;
unsigned long y = 1900;
int dayPerYear = 365;
while (epochAfterDay >= (dayPerYear + isLeapYear(y))) {
epochAfterDay -= (dayPerYear + isLeapYear(y));
y++;
}
*year = y;
int dayPerMonth[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int m = 0;
while (epochAfterDay >= (dayPerMonth[m] + (m == 1 && isLeapYear(y)))) {
epochAfterDay -= (dayPerMonth[m] + (m == 1 && isLeapYear(y)));
m++;
}
*month = m + 1;
*day = epochAfterDay + 1;
}
bool isLeapYear(int year) {
return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
}
String readFile(String read_path_){
SD_File read_file = SD.open(read_path_, FILE_READ);
if(read_file.isDirectory()){
return "Erreur : Destination non compatible";
}
String readed = "";
while(read_file.available()){
readed += read_file.readString();
}
read_file.close();
return readed;
}
String listDirectory(String filepath_, String filepath_array_[21]) {
SD_File dir = SD.open(filepath_);
int i = 0;
String result = filepath_ + " :";
while (true) {
SD_File entry = dir.openNextFile();
if (!entry) {
dir.close();
return result;
}
i++;
String name = String(entry.name());
result += '\n' + String(i) + " : " + name;
filepath_array_[i] = name;
entry.close();
}
}
String listClient(String client_array_[20]) {
SD_File dir = SD.open("RACINE");
int i = 0;
String result = "Clients Valides :";
while (true) {
SD_File entry = dir.openNextFile();
if (!entry) {
dir.close();
return result;
}
String name = String(entry.name());
String client_path = "RACINE/" + name;
if(SD.exists(client_path + "/IP.ip") && SD.exists(client_path + "/PAYLOAD")){
result += "\n|--> " + String(i) + " : " + name;
client_array[i] = name;
i++;
SD_File listdir = SD.open(client_path);
while (true) {
SD_File listentry = listdir.openNextFile();
if (!listentry) {
listdir.close();
break;
}
String name = String(listentry.name());
result += "\n |--> " + name;
listentry.close();
}
}
entry.close();
}
}
void deleteFolderContents(String folderPath) {
SD_File dir = SD.open(folderPath);
if (!dir) {
Serial.println("Failed to opn directory");
return;
}
while (true) {
SD_File entry = dir.openNextFile();
if (!entry) {
// Pas plus de fichiers dans le répertoire
break;
}
if (entry.isDirectory()) {
deleteFolderContents(folderPath + "/" + entry.name()); // Appel récursif pour les sous-répertoires
SD.rmdir(folderPath + "/" + entry.name()); // Supprime le répertoire après avoir supprimé son contenu
} else {
SD.remove(folderPath + "/" + entry.name()); // Supprime le fichier
}
entry.close();
}
dir.close();
}
void copyFile(String sourcePath, String destinationPath) {
SD_File sourceFile = SD.open(sourcePath);
if (!sourceFile) {
Serial.println("Failed to opn source file");
return;
}
SD_File destFile = SD.open(destinationPath, FILE_WRITE);
if (!destFile) {
Serial.println("Failed to opn destination file");
sourceFile.close();
return;
}
while (sourceFile.available()) {
destFile.write(sourceFile.read());
}
sourceFile.close();
destFile.close();
Serial.println("SD_File copied: " + sourcePath + " to " + destinationPath);
}
void copyFolderContents(String sourcePath, String destinationPath) {
SD_File dir = SD.open(sourcePath);
if (!dir) {
Serial.println("Failed to opn source directory");
return;
}
if (!SD.exists(destinationPath)) {
SD.mkdir(destinationPath);
}
while (true) {
SD_File entry = dir.openNextFile();
if (!entry) {
// Pas plus de fichiers dans le répertoire
break;
}
String newSourcePath = String(sourcePath) + "/" + entry.name();
String newDestinationPath = String(destinationPath) + "/" + entry.name();
if (entry.isDirectory()) {
copyFolderContents(newSourcePath, newDestinationPath); // Appel récursif pour les sous-répertoires
} else {
copyFile(newSourcePath, newDestinationPath); // Copie du fichier
}
entry.close();
}
dir.close();
}
String pEncrypt(String Data){
String cipher = "";
while(Data != pDecrypt(cipher)){
cipher = _pEncrypt(Data);
}
return cipher;
}
String _pEncrypt(String Data){
int _alea = random(1000000, 100000000);
String alea = String(_alea);
Serial.println("Alea : " + alea);
String seed = STRONG_KEY + alea;
// Encrypting STRONG LEVEL
String returnEncrypted = alea + ":::" + Dipher::encrypt(Data, seed);
//Encrypting WEAK LEVEL
return Dipher::encrypt(returnEncrypted, WEAK_KEY);
}
String pDecrypt(String Data){
String alea = "";
// Decrypting WEAK LEVEL
String returnDecrypted = Dipher::decrypt(Data, WEAK_KEY);
//Getting "Alea"
alea = returnDecrypted.substring(0, returnDecrypted.indexOf(":::"));
String seed = STRONG_KEY + alea;
returnDecrypted = returnDecrypted.substring(returnDecrypted.indexOf(":::") + 3);
//Decrypting STRONG LEVEL
return Dipher::decrypt(returnDecrypted, seed);
}
void onSelectorChange() {
// Add your code here to act upon Selector change
}
void onSelectChange() {
if(select == false){
if(SD.exists(path)){
SD_File check_file = SD.open(path);
if(selector == 0){
path.remove(path.lastIndexOf('/'));
cloudTX = listDirectory(path, filepath_array);
} else if (check_file.isDirectory()){
check_file.close();
path += "/" + filepath_array[selector];
check_file = SD.open(path);
if(!check_file.isDirectory()){
cloudRX = readFile(path);
}
cloudTX = listDirectory(path, filepath_array);
}
check_file.close();
}
}
}
void onCopierChange() {
if(couper == false){
_sourcePath = path;
file = SD.open(path);
if(file.isDirectory()){
_pasteFoF = "FOLDER";
} else {
_pasteFoF = "FILE";
}
_pasteCoC = "COPY";
cloudTX = "Copier : " + _sourcePath;
}
}
void onCouperChange() {
if(couper == false){
_sourcePath = path;
file = SD.open(path);
if(file.isDirectory()){
_pasteFoF = "FOLDER";
} else {
_pasteFoF = "FILE";
}
_pasteCoC = "CUT";
file.close();
cloudTX = "Couper : " + _sourcePath;
}
}
void onCollerChange() {
if(coller == false){
cloudTX = "Coller : " + path;
path += "/" + _sourcePath.substring(_sourcePath.lastIndexOf('/')+1);
if(_pasteFoF == "FOLDER"){
SD.mkdir(path);
copyFolderContents(_sourcePath, path);
if(_pasteCoC == "CUT"){
deleteFolderContents(_sourcePath);
SD.remove(_sourcePath);
}
} else {
copyFile(_sourcePath, path);
if(_pasteCoC == "CUT"){
SD.remove(_sourcePath);
}
}
cloudTX = listDirectory(path, filepath_array);
}
}
void onRenommerChange() {
if(renommer == false){
String origin_path = path;
SD_File ren_file = SD.open(origin_path);
String newPath = path.substring(0, path.lastIndexOf('/')) + "/" + cloudRX;
//renameFileOrDir(path, newName);
if(ren_file.isDirectory()){
ren_file.close();
SD.mkdir(newPath);
copyFolderContents(origin_path, newPath);
deleteFolderContents(origin_path);
SD.rmdir(origin_path);
} else {
ren_file.close();
copyFile(origin_path, newPath);
SD.remove(origin_path);
cloudRX = readFile(newPath);
}
newPath.remove(newPath.lastIndexOf('/'));
cloudTX = listDirectory(newPath, filepath_array);
}
}
void onEcrireChange() {
if(ecrire == false){
file = SD.open(path, FILE_WRITE);
if(!file.isDirectory()) {
SD.remove(path);
file.close();
file = SD.open(path, FILE_WRITE);
file.print(cloudRX);
file.close();
cloudTX = "Lecture du fichier :\n" + readFile(path);
cloudRX = "";
}
}
}
void onValeurChange() {
if(valeur == false){
String ID;
if(path.lastIndexOf('/') != path.indexOf('/')){
ID = path.substring(0, path.indexOf('/', path.indexOf('/')+1)-1);
} else {
ID = path;
}
SD_File val_File = SD.open(ID + "/IP.ip", FILE_READ);
String _IP = "";
while(val_File.available()){
_IP=val_File.readString();
}
val_File.close();
IPAddress ip;
ip.fromString(_IP);
if(cloudRX.indexOf("getPayload") != -1){
if(!SD.exists(ID + "/PAYLOAD")){
cloudTX = "Erreur de destination";
}
String foolArray[21];
cloudTX = listDirectory(ID + "/PAYLOAD", foolArray);
cloudRX = "";
} else if(cloudRX.indexOf("getData=") != -1){
crypted = pEncrypt("Server:" + cloudRX + "\r\n");
Serial.println("Dipher : " + crypted);
udp.beginPacket(ip, localPort);
udp.print(crypted);
udp.endPacket();
int packetSize = udp.parsePacket();
while(packetSize <= 0){
delay(100);
}
char packetBuffer[255];
if(packetSize) {
int len = udp.read(packetBuffer, 255);
if(len > 0) packetBuffer[len] = 0;
}
String _packetBuffer = pDecrypt(String(packetBuffer));
Serial.println("Dipher : " + _packetBuffer);
cloudTX=_packetBuffer;
cloudRX="";
} else if (cloudRX.indexOf("setData=") != -1){
crypted = pEncrypt("Server:" + cloudRX + "\r\n");
Serial.println("Dipher : " + crypted);
udp.beginPacket(ip, localPort);
udp.print(crypted);
udp.endPacket();
int packetSize = udp.parsePacket();
while(packetSize <= 0){
delay(100);
}
char packetBuffer[255];
if(packetSize) {
int len = udp.read(packetBuffer, 255);
if(len > 0) packetBuffer[len] = 0;
}
String _packetBuffer = pDecrypt(String(packetBuffer));
Serial.println("Dipher : " + _packetBuffer);
cloudTX=_packetBuffer;
cloudRX="";
}
}
}
void onTestConnectChange() {
if(testConnect == false){
path = "RACINE";
cloudTX = listDirectory(path, filepath_array);
cloudRX = "";
connectedTime = millis();
controlTX = listClient(client_array);
}
}
void onCloudRXChange() {
// Add your code here to act upon CloudRX change
}
void onNewFileChange() {
if(new_file == false) {
SD_File isDir = SD.open(path, FILE_READ);
if(!isDir.isDirectory()){
return;
}
isDir.close();
path += "/" + cloudRX;
if(!SD.exists(path)){
SD_File new_file = SD.open(path, FILE_WRITE);
new_file.print("");
new_file.close();
}
isDir.close();
cloudTX = listDirectory(path, filepath_array);
cloudRX = readFile(path);
}
}
void onNewDirChange() {
if(new_dir == false) {
SD_File isDir = SD.open(path, FILE_READ);
if(!isDir.isDirectory()){
return;
}
isDir.close();
path += "/" + cloudRX;
if(!SD.exists(path)){
SD.mkdir(path);
}
cloudTX = listDirectory(path, filepath_array);
}
}
void onSupprimerChange() {
if(supprimer == false) {
if(path == "RACINE" || path == "RACINE/" || path == "racine" || path == "racine/"){
return;
}
cloudTX = "Supprimmer : " + path;
SD_File del_file = SD.open(path, FILE_READ);
if(del_file.isDirectory()){
del_file.close();
deleteFolderContents(path);
SD.rmdir(path);
} else {
del_file.close();
SD.remove(path);
}
path.remove(path.lastIndexOf('/'));
cloudTX = listDirectory(path, filepath_array);
}
}
void onActivateChange() {
if(controlSelect == false){
String ACTIV = udpSendWait(actualClient, "ACTIV");
ACTIV.remove(0, ACTIV.indexOf('>'));
if(ACTIV == "true"){
activate = true;
} else {
activate = false;
}
controlRX = "";
}
}
void onControlRXChange() {
// Add your code here to act upon ControlRX change
}
void onControlTXChange() {
// Add your code here to act upon ControlTX change
}
void onControlSelectChange() {
if(controlSelect == false){
actualClient = client_array[controlRX.toInt()];
controlTX = udpSendWait(actualClient, "GET");
controlRX = "";
}
}
String udpSendWait(String ID, String payload){
SD_File slct_File = SD.open("RACINE/" + ID + "/IP.ip", FILE_READ);
String _IP = "";
while(slct_File.available()){
_IP=slct_File.readString();
}
slct_File.close();
IPAddress ip;
ip.fromString(_IP);
crypted = pEncrypt("Server:" + payload + "\r\n");
Serial.println("Dipher : " + crypted);
udp.beginPacket(ip, localPort);
udp.print(crypted);
udp.endPacket();
int startWaiting = millis();
int packetSize = udp.parsePacket();
while(packetSize <= 0){
int currentMillis = millis();
if(currentMillis - startWaiting > 10000){
return "Le client à mis trop de temps à répondre !";
}
if(isConnected == false){
isConnected = true;
} else {
isConnected = false;
}
delay(1000);
}
char packetBuffer[255];
if(packetSize) {
int len = udp.read(packetBuffer, 255);
if(len > 0) packetBuffer[len] = 0;
}
String _packetBuffer = pDecrypt(String(packetBuffer));
Serial.println("Dipher : " + _packetBuffer);
return _packetBuffer;
}
void DisableWifi(void)
{
WiFi.disconnect();
WiFi.mode(WIFI_OFF);
WiFi.forceSleepBegin();
delay(1);
}
- Le gros du problème se trouve dans la fonction Init()
Je sais pas trop comment me dépatouiller de tout ça ? Merci d'avance ![]()
PS : la lib SDCustom sent le souffre ? Normal ! J'ai essayé un truc avec ESPAsyncWebServer, mais il à une dépendence avec FS.h qui utilise aussi la classe "File", j'ai pas trouvé mieux que renommer toute les classes en "SD_File".