Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*********
- Julfi
- Ce code a été réalisé à partir de celui de Rui Santos.
- Vous pouvez retrouver les détails du projet ici :
- Vous pouvez retrouver le copyright juste en dessous.
- L'aide mémoire pour charger un programme dans l'ESP32cam
- a été traduit en francais mais sinon cette partie reste
- inchangé.
- You can find bellow the copyright text. The memo for upload
- sketch into the ESP32cam has been translated in french but
- this part is still unchanged.
- *********/
- /*********
- Rui Santos
- Complete project details at https://RandomNerdTutorials.com/esp32-cam-take-photo-display-web-server/
- IMPORTANT!!!
- - Selectionné la carte "AI Thinker ESP32-CAM"
- - GPIO 0 doit être connecté à la masse pour charger le programme
- - Après avoir connecté le GPIO 0 à la masse veuillez appuyer sur le bouton reset pour passer la carte en mode "flashing"
- The above copyright notice and this permission notice shall be included in all
- copies or substantial portions of the Software.
- *********/
- #include "WiFi.h"
- #include "esp_camera.h"
- #include "esp_timer.h"
- #include "img_converters.h"
- #include "Arduino.h"
- #include "soc/soc.h"
- #include "soc/rtc_cntl_reg.h"
- #include "driver/rtc_io.h"
- #include <ESPAsyncWebServer.h>
- #include <StringArray.h>
- #include <SPIFFS.h>
- #include <FS.h>
- #include <stdio.h>
- #include <time.h>
- const char* NTP_SERVER = "ch.pool.ntp.org"; //adresse des serveurs NTP
- const char* TZ_INFO = "CET-1CEST-2,M3.5.0/02:00:00,M10.5.0/03:00:00"; // Changer votre fuseau horaire en allant sur (https://remotemonitoringsystems.ca/time-zone-abbreviations.php)
- const char* ssid = "SSID";
- const char* password = "PASSWORD";
- //Déclarations pour récupérer l'heure
- tm timeinfo;
- time_t now;
- long unsigned lastNTPtime;
- unsigned long lastEntryTime;
- // Création d'un objet AsyncWebServer sur le port 80
- AsyncWebServer server(80);
- boolean takeNewPhoto = false;
- const int Photoresistance = 13;
- const int PIRsensor = 14;
- int numero_photo = 1;
- boolean goPhoto = false;
- // OV2640 broches du module caméra (CAMERA_MODEL_AI_THINKER)
- #define PWDN_GPIO_NUM 32
- #define RESET_GPIO_NUM -1
- #define XCLK_GPIO_NUM 0
- #define SIOD_GPIO_NUM 26
- #define SIOC_GPIO_NUM 27
- #define Y9_GPIO_NUM 35
- #define Y8_GPIO_NUM 34
- #define Y7_GPIO_NUM 39
- #define Y6_GPIO_NUM 36
- #define Y5_GPIO_NUM 21
- #define Y4_GPIO_NUM 19
- #define Y3_GPIO_NUM 18
- #define Y2_GPIO_NUM 5
- #define VSYNC_GPIO_NUM 25
- #define HREF_GPIO_NUM 23
- #define PCLK_GPIO_NUM 22
- //Code de la page web
- const char index_html[] PROGMEM = R"rawliteral(
- <!DOCTYPE HTML><html>
- <head>
- <meta name="viewport" content="width=device-width, initial-scale=1">
- <style>
- body { text-align:center; }
- .vert { margin-bottom: 10%; }
- .hori{ margin-bottom: 0%; }
- </style>
- </head>
- <body style="background-color:#1f1f1f;color:white;">
- <div id="container" style="font-family: 'Source Sans Pro', sans-serif;">
- <h2>Home - Video surveillance</h2>
- <p>Les photos sont actualisees toute les 5 secondes.</p>
- <p>
- </p>
- </div>
- <div style="display:flex;flex-direction: row;justify-content: center;">
- <div><img src="saved-photo/photo1" id="photo1" width="100%"></div>
- <div><img src="saved-photo/photo2" id="photo2" width="100%"></div>
- <div><img src="saved-photo/photo3" id="photo3" width="100%"></div>
- </div>
- </body>
- <script >
- function refresh() {
- var tmp = new Date();
- var img1 = document.getElementById("photo1");
- var img2 = document.getElementById("photo2");
- var img3 = document.getElementById("photo3");
- img1.src = img1.src + '?' + tmp.getTime();
- img2.src = img2.src + '?' + tmp.getTime();
- img3.src = img3.src + '?' + tmp.getTime();
- }
- window.onload = function() {
- setInterval(refresh,5000);
- };
- var deg = 0;
- function capturePhoto() {
- var xhr = new XMLHttpRequest();
- xhr.open('GET', "/capture", true);
- xhr.send();
- }
- function rotatePhoto() {
- var img = document.getElementById("photo");
- deg += 90;
- if(isOdd(deg/90)){ document.getElementById("container").className = "vert"; }
- else{ document.getElementById("container").className = "hori"; }
- img.style.transform = "rotate(" + deg + "deg)";
- }
- function isOdd(n) { return Math.abs(n % 2) == 1; }
- </script>
- </html>)rawliteral";
- void setup() {
- Serial.begin(115200);
- // Connect to Wi-Fi
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(1000);
- Serial.println("Connecting to WiFi...");
- }
- if (!SPIFFS.begin(true)) {
- Serial.println("An Error has occurred while mounting SPIFFS");
- ESP.restart();
- }
- else {
- delay(500);
- Serial.println("SPIFFS mounted successfully");
- }
- configTime(0, 0, NTP_SERVER);
- // Regarder ici https://github.com/nayarsystems/posix_tz_db/blob/master/zones.csv pour connaitre les codes du fuseau associés à votre région
- setenv("TZ", TZ_INFO, 1);
- if (getNTPtime(10)) { // Attendez 10sec pour la synchronisation
- } else {
- Serial.println("Time not set");
- ESP.restart();
- }
- lastNTPtime = time(&now);
- lastEntryTime = millis();
- // Affichage de l'adresse IP du module
- Serial.print("IP Address: http://");
- Serial.println(WiFi.localIP());
- // Coupe le 'brownout detector'
- WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0);
- // OV2640 camera module
- camera_config_t config;
- config.ledc_channel = LEDC_CHANNEL_0;
- config.ledc_timer = LEDC_TIMER_0;
- config.pin_d0 = Y2_GPIO_NUM;
- config.pin_d1 = Y3_GPIO_NUM;
- config.pin_d2 = Y4_GPIO_NUM;
- config.pin_d3 = Y5_GPIO_NUM;
- config.pin_d4 = Y6_GPIO_NUM;
- config.pin_d5 = Y7_GPIO_NUM;
- config.pin_d6 = Y8_GPIO_NUM;
- config.pin_d7 = Y9_GPIO_NUM;
- config.pin_xclk = XCLK_GPIO_NUM;
- config.pin_pclk = PCLK_GPIO_NUM;
- config.pin_vsync = VSYNC_GPIO_NUM;
- config.pin_href = HREF_GPIO_NUM;
- config.pin_sscb_sda = SIOD_GPIO_NUM;
- config.pin_sscb_scl = SIOC_GPIO_NUM;
- config.pin_pwdn = PWDN_GPIO_NUM;
- config.pin_reset = RESET_GPIO_NUM;
- config.xclk_freq_hz = 20000000;
- config.pixel_format = PIXFORMAT_JPEG;
- if (psramFound()) {
- config.frame_size = FRAMESIZE_UXGA;
- config.jpeg_quality = 10;
- config.fb_count = 2;
- } else {
- config.frame_size = FRAMESIZE_SVGA;
- config.jpeg_quality = 12;
- config.fb_count = 1;
- }
- // Camera init
- esp_err_t err = esp_camera_init(&config);
- if (err != ESP_OK) {
- Serial.printf("Camera init failed with error 0x%x", err);
- ESP.restart();
- }
- // Page racine "/"
- server.on("/", HTTP_GET, [](AsyncWebServerRequest * request) {
- request->send_P(200, "text/html", index_html);
- });
- server.on("/capture", HTTP_GET, [](AsyncWebServerRequest * request) {
- takeNewPhoto = true;
- request->send_P(200, "text/plain", "Taking Photo");
- });
- //Chemin d'accès aux photos pour les balises images
- server.on("/saved-photo/photo1", HTTP_GET, [](AsyncWebServerRequest * request) {
- request->send(SPIFFS, "/photo1.jpg", "image/jpg", false);
- });
- server.on("/saved-photo/photo2", HTTP_GET, [](AsyncWebServerRequest * request) {
- request->send(SPIFFS, "/photo2.jpg", "image/jpg", false);
- });
- server.on("/saved-photo/photo3", HTTP_GET, [](AsyncWebServerRequest * request) {
- request->send(SPIFFS, "/photo3.jpg", "image/jpg", false);
- });
- // Démarrage du serveur web
- server.begin();
- }
- void loop() {
- //Vrai s'il est plus de 22h mais moins de 5h
- if(showTime(timeinfo, 1) >= 8 and showTime(timeinfo, 1) <= 17){
- //Vrai si la photorésistance détecte de la lumière
- if(!digitalRead(Photoresistance)){
- //Vrai si le détecteur PIR ne détecte pas de présence et que la variable goPhoto est égale à 0
- if(digitalRead(PIRsensor) && !goPhoto){
- goPhoto = true;
- takeNewPhoto = true;
- //Vrai si le détecteur PIR détecte une présence
- }else if(!digitalRead(PIRsensor)){
- goPhoto = false;
- }
- if (takeNewPhoto) {
- //Vrai le numéro de photo est supérieur à 1
- if(numero_photo > 3){
- numero_photo = 1;
- }
- capturePhotoSaveSpiffs(numero_photo); //Prise de le l'image numéro "numero_photo"
- numero_photo++; //Incrémentation du numéro de photo
- takeNewPhoto = false;
- }
- }
- }
- delay(1);
- getNTPtime(10); //Récupération des données sur le temps
- }
- // Vérification que l'enregistrement de la photo c'est bien passé
- bool checkPhoto( fs::FS &fs, int number ) {
- char name[50];
- sprintf(name, "/photo%d.jpg", number);
- File f_pic = fs.open( name );
- unsigned int pic_sz = f_pic.size();
- return ( pic_sz > 100 );
- }
- bool getNTPtime(int sec) {
- {
- uint32_t start = millis();
- do {
- time(&now);
- localtime_r(&now, &timeinfo);
- delay(10);
- } while (((millis() - start) <= (1000 * sec)) && (timeinfo.tm_year < (2016 - 1900)));
- if (timeinfo.tm_year <= (2016 - 1900)) return false; // L'appel NTP à échoué
- char time_output[30];
- strftime(time_output, 30, "%a %d-%m-%y %T", localtime(&now)); //Mise sous forme de string de la date et de l'heure
- }
- return true;
- }
- int showTime(tm localTime, int heure) {
- if(heure == 1){
- return localTime.tm_hour;
- }else{
- return localTime.tm_min;
- }
- }
- // Prise d'une photo sauvegardé dans la mémoire SPIFFS
- void capturePhotoSaveSpiffs( int number ) {
- camera_fb_t * fb = NULL; // pointer
- bool ok = 0; // Boolean qui indique si la capture de photo a été réalisé
- char name[50];
- sprintf(name, "/photo%d.jpg", number);
- do {
- // Prise d'une photo avec la caméra
- Serial.println("Taking a photo...");
- fb = esp_camera_fb_get();
- if (!fb) {
- Serial.println("Camera capture failed");
- return;
- }
- // Affichage du nom de la photo prise
- Serial.printf("Picture file name: %s\n", name);
- File file = SPIFFS.open(name, FILE_WRITE);
- // Insertion de données dans le fichier de la photo
- if (!file) {
- Serial.println("Failed to open file in writing mode");
- }
- else {
- file.write(fb->buf, fb->len); // charge utile (image), longueur de la charge
- Serial.print("The picture has been saved in ");
- Serial.print(name);
- Serial.print(" - Size: ");
- Serial.print(file.size());
- Serial.println(" bytes");
- }
- // Fermeture du fichier
- file.close();
- esp_camera_fb_return(fb);
- // Vérfification pour savoir si le fichier a correctement été enregistré
- ok = checkPhoto(SPIFFS, number);
- } while ( !ok );
- }
Add Comment
Please, Sign In to add comment