/****************************************************************** L'ESP32-CAM présente une page web qui permet de prendre des photos et de les enregistrer sur une carte SD, de visionner les photos déjà présentes sur la carte, et de supprimer les photos non-désirées. Pour plus d'informations: http://electroniqueamateur.blogspot.com/2020/07/esp32-cam-gestion-distance-de-la-carte.html ******************************************************************/ #include #include #include #include "FS.h" // manipulation de fichiers #include "SD_MMC.h" // carte SD #include "esp_camera.h" // caméra! // écrivez le nom et le mot de passe de votre réseau WIFI const char* ssid = "**********"; const char* password = "**********"; WebServer server(80); static bool cartePresente = false; int numero_fichier = 0; // numéro de la photo (nom du fichier) // prise de la photo et création du fichier jpeg void enregistrer_photo (void) { char adresse[20] = ""; // chemin d'accès du fichier .jpeg camera_fb_t * fb = NULL; // frame buffer // prise de la photo fb = esp_camera_fb_get(); if (!fb) { Serial.println("Echec de la prise de photo."); return; } numero_fichier = numero_fichier + 1; // enregitrement du fichier sur la carte SD sprintf (adresse, "/%d.jpg", numero_fichier); fs::FS &fs = SD_MMC; File file = fs.open(adresse, FILE_WRITE); if (!file) { Serial.println("Echec lors de la creation du fichier."); } else { file.write(fb->buf, fb->len); // payload (image), payload length Serial.printf("Fichier enregistre: %s\n", adresse); } file.close(); esp_camera_fb_return(fb); // on affiche un message de confirmation server.setContentLength(CONTENT_LENGTH_UNKNOWN); server.send(200, "text/html", ""); WiFiClient client = server.client(); server.sendContent("

Une nouvelle photo a été prise.

"); server.sendContent("

Retour à la liste des fichiers

"); } void returnOK() { server.send(200, "text/plain", ""); } void returnFail(String msg) { server.send(500, "text/plain", msg + "\r\n"); } // Affichage d'un fichier présent sur la carte bool loadFromSdCard(String path) { String dataType = "text/plain"; if (path == "/") { printDirectory(); } else { if (path.endsWith(".src")) { path = path.substring(0, path.lastIndexOf(".")); } else if (path.endsWith(".htm")) { dataType = "text/html"; } else if (path.endsWith(".css")) { dataType = "text/css"; } else if (path.endsWith(".js")) { dataType = "application/javascript"; } else if (path.endsWith(".png")) { dataType = "image/png"; } else if (path.endsWith(".gif")) { dataType = "image/gif"; } else if (path.endsWith(".jpg")) { dataType = "image/jpeg"; } else if (path.endsWith(".ico")) { dataType = "image/x-icon"; } else if (path.endsWith(".xml")) { dataType = "text/xml"; } else if (path.endsWith(".pdf")) { dataType = "application/pdf"; } else if (path.endsWith(".zip")) { dataType = "application/zip"; } fs::FS &fs = SD_MMC; File dataFile = fs.open(path.c_str()); if (!dataFile) { return false; } if (server.hasArg("download")) { dataType = "application/octet-stream"; } if (server.streamFile(dataFile, dataType) != dataFile.size()) { Serial.println("Sent less data than expected!"); } dataFile.close(); } return true; } // utilisé lors de la suppression d'un fichier void deleteRecursive(String path) { fs::FS &fs = SD_MMC; File file = fs.open((char *)path.c_str()); if (!file.isDirectory()) { file.close(); fs.remove((char *)path.c_str()); return; } file.rewindDirectory(); while (true) { File entry = file.openNextFile(); if (!entry) { break; } String entryPath = path + "/" + entry.name(); if (entry.isDirectory()) { entry.close(); deleteRecursive(entryPath); } else { entry.close(); fs.remove((char *)entryPath.c_str()); } yield(); } fs.rmdir((char *)path.c_str()); file.close(); } // suppression d'un fichier void handleDelete() { fs::FS &fs = SD_MMC; if (server.args() == 0) { return returnFail("Mauvais arguments?"); } String path = server.arg(0); if (path == "/" || !fs.exists((char *)path.c_str())) { returnFail("BAD PATH"); return; } deleteRecursive(path); // on affiche un message de confirmation server.setContentLength(CONTENT_LENGTH_UNKNOWN); server.send(200, "text/html", ""); WiFiClient client = server.client(); server.sendContent("

Le fichier a été supprimé

"); server.sendContent("

Retour à la liste des fichiers

"); } // Affichage du contenu de la carte void printDirectory() { fs::FS &fs = SD_MMC; String path = "/"; File dir = fs.open((char *)path.c_str()); path = String(); if (!dir.isDirectory()) { dir.close(); return returnFail("PAS UN REPERTOIRE"); } dir.rewindDirectory(); server.setContentLength(CONTENT_LENGTH_UNKNOWN); server.send(200, "text/html", ""); WiFiClient client = server.client(); server.sendContent("

Prise de photo

"); server.sendContent("

"); server.sendContent("

Contenu de la carte SD

"); for (int cnt = 0; true; ++cnt) { File entry = dir.openNextFile(); if (!entry) { break; } String output; output += " "; output += entry.name(); // on ajoute un bouton delete: output += "