Last active
July 29, 2020 12:30
Capteur de mouvement PIR branché à une ESP32-CAM. Des photos sont prises lorsqu'un mouvement est détecté. Version sans WIFI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* ESP32-CAM PIR | |
* | |
* Capteur de mouvement PIR branché à une ESP32-CAM. | |
* | |
* Des photos sont prises lorsqu'un mouvement est détecté. | |
* Version sans WIFI | |
* | |
* Pour plus d'infos: | |
* https://electroniqueamateur.blogspot.com/2020/07/esp32-cam-et-capteur-infrarouge-passif.html | |
* | |
*/ | |
#include "FS.h" // manipulation de fichiers | |
#include "SD_MMC.h" // carte SD | |
#include "esp_camera.h" // caméra! | |
static bool cartePresente = false; | |
int numero_fichier = 0; // numéro de la photo (nom du fichier) | |
int brochePIR = 13; // capteur PIR branché à la broche GPIO13. | |
// 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); | |
} | |
void setup(void) { | |
// définition des broches de la caméra pour le modèle AI Thinker - ESP32-CAM | |
camera_config_t config; | |
config.ledc_channel = LEDC_CHANNEL_0; | |
config.ledc_timer = LEDC_TIMER_0; | |
config.pin_d0 = 5; | |
config.pin_d1 = 18; | |
config.pin_d2 = 19; | |
config.pin_d3 = 21; | |
config.pin_d4 = 36; | |
config.pin_d5 = 39; | |
config.pin_d6 = 34; | |
config.pin_d7 = 35; | |
config.pin_xclk = 0; | |
config.pin_pclk = 22; | |
config.pin_vsync = 25; | |
config.pin_href = 23; | |
config.pin_sscb_sda = 26; | |
config.pin_sscb_scl = 27; | |
config.pin_pwdn = 32; | |
config.pin_reset = -1; | |
config.xclk_freq_hz = 20000000; | |
config.pixel_format = PIXFORMAT_JPEG; //YUV422|GRAYSCALE|RGB565|JPEG | |
config.frame_size = FRAMESIZE_VGA; // QVGA|CIF|VGA|SVGA|XGA|SXGA|UXGA | |
config.jpeg_quality = 10; // 0-63 ; plus bas = meilleure qualité | |
config.fb_count = 2; // nombre de frame buffers | |
// initialisation de la caméra | |
esp_err_t err = esp_camera_init(&config); | |
if (err != ESP_OK) { | |
Serial.printf("Echec de l'initialisation de la camera, erreur 0x%x", err); | |
return; | |
} | |
sensor_t * s = esp_camera_sensor_get(); | |
// initialisation de la carte micro SD | |
// en mode 1 bit: plus lent, mais libère des broches | |
if (SD_MMC.begin("/sdcard",true)) { | |
uint8_t cardType = SD_MMC.cardType(); | |
if (cardType != CARD_NONE) { | |
Serial.println("Carte SD Initialisee."); | |
cartePresente = true; | |
} | |
} | |
pinMode(brochePIR,INPUT); | |
} | |
void loop(void) { | |
if (digitalRead(brochePIR)){ | |
enregistrer_photo(); | |
Serial.print("Nouvelle photo! "); | |
Serial.println(numero_fichier); | |
delay(300); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment