/****************************************************************************** ESP32-CAM a PIR sensor. Pri detekci PIR cidla se sepne napajeni ESP32-CAM, vytvori fotografii, ktera se ulozi na microSD kartu a odesle na FPT server. Zdroje: https://github.com/ldab/ESP32-CAM-Picture-Sharing https://randomnerdtutorials.com/esp32-cam-take-photo-save-microsd-card/ https://randomnerdtutorials.com/esp32-ntp-client-date-time-arduino-ide/ 1/2021 chiptron.cz ******************************************************************************/ #include "Arduino.h" #define CAMERA_MODEL_AI_THINKER #include #include #include #include #include #include "esp_camera.h" #include "esp_timer.h" #include "img_converters.h" #include "esp_http_server.h" #include "fb_gfx.h" #include "fd_forward.h" #include "fr_forward.h" #include "FS.h" // SD Card ESP32 #include "SD_MMC.h" // SD Card ESP32 #include "soc/soc.h" // Disable brownour problems #include "soc/rtc_cntl_reg.h" // Disable brownour problems #include "driver/rtc_io.h" // Connection timeout; #define CON_TIMEOUT 10*1000 // milliseconds // Not using Deep Sleep on PCB because TPL5110 timer takes over. #define TIME_TO_SLEEP (uint64_t)120 * 1000000 // microseconds // FTP Client Lib #include "ESP32_FTPClient.h" /******************************************************************************/ /* UZIVATEL MUSI UPRAVIT */ // Your WiFi credentials. char ssid[] = "SSID-WIFI"; char pass[] = "HESLO-WIFI"; // FTP Server credentials char ftp_server[] = "FTP SERVER"; char ftp_user[] = "FTP USER"; char ftp_pass[] = "FTP HESLO"; /* UZIVATEL UZ NESMI UPRAVIT */ /******************************************************************************/ // Camera buffer, URL and picture name camera_fb_t *fb = NULL; String pic_name = ""; String pic_url = ""; ESP32_FTPClient ftp (ftp_server, ftp_user, ftp_pass); void FTP_upload( void ); bool take_picture(void); // Define NTP Client to get time WiFiUDP ntpUDP; NTPClient timeClient(ntpUDP); String formattedDate; String dayStamp; String timeStamp; void setup() { Serial.begin(115200); Serial.setDebugOutput(true); /* nastaveni kamery */ 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; //init with high specs to pre-allocate larger buffers config.frame_size = FRAMESIZE_XGA; // set picture size, FRAMESIZE_XGA = 1024x768 config.jpeg_quality = 10; // quality of JPEG output. 0-63 lower means higher quality config.fb_count = 2; // camera init esp_err_t err = esp_camera_init(&config); if (err != ESP_OK) { Serial.println("Camera init failed with error 0x%x"); return; } // Enable timer wakeup for ESP32 sleep esp_sleep_enable_timer_wakeup( TIME_TO_SLEEP ); WiFi.begin( ssid, pass ); Serial.println("\nConnecting to WiFi"); while ( WiFi.status() != WL_CONNECTED && millis() < CON_TIMEOUT ) { delay(500); Serial.println("."); } if( !WiFi.isConnected() ) { Serial.println("Failed to connect to WiFi, going to sleep"); esp_deep_sleep_start(); } Serial.println(""); Serial.println("WiFi connected"); Serial.println( WiFi.localIP() ); /* NTP */ timeClient.begin(); timeClient.setTimeOffset(3600); timeClient.update(); } void loop() { /******************************************************************************/ /* NTP */ formattedDate = timeClient.getFormattedDate(); int splitT = formattedDate.indexOf("T"); dayStamp = formattedDate.substring(0, splitT); Serial.println("DATE: "); Serial.println(dayStamp); // Extract time timeStamp = formattedDate.substring(splitT+1, formattedDate.length()-1); Serial.println("HOUR: "); Serial.println(timeStamp); /******************************************************************************/ // Take picture if( take_picture() ) { FTP_upload(); Serial.println("Capture done, sleeping"); Serial.flush(); esp_deep_sleep_start(); } else { Serial.println("Capture failed, sleeping"); Serial.flush(); esp_deep_sleep_start(); } if( millis() > CON_TIMEOUT) { Serial.println("Going to sleep after: " + String( millis() ) + "ms"); Serial.flush(); esp_deep_sleep_start(); } } bool take_picture() { Serial.println("Taking picture now"); /******************************************************************************/ /* KAMERA */ fb = esp_camera_fb_get(); if(!fb) { Serial.println("Camera capture failed"); return false; } /******************************************************************************/ /******************************************************************************/ /* NTP */ now(); char picTime[30] = {0}; sprintf(picTime, "pic_%s:%s", dayStamp, timeStamp); /******************************************************************************/ // Rename the picture with the time string pic_name += String(picTime) + ".jpg"; Serial.println("Camera capture success, saved as:"); Serial.println( pic_name ); return true; } void FTP_upload() { /******************************************************************************/ /* microSD karta */ String pic_path = "/picture" + pic_name; Serial.println("Starting SD Card"); delay(100); if(!SD_MMC.begin()) { Serial.println("SD Card Mount Failed"); } uint8_t cardType = SD_MMC.cardType(); if(cardType == CARD_NONE) { Serial.println("No SD Card attached"); return; } fs::FS &fs = SD_MMC; Serial.println("Picture file name: "); Serial.println(pic_path.c_str()); File file = fs.open(pic_path.c_str(), FILE_WRITE); if(!file) { Serial.println("Failed to open file in writing mode"); } else { file.write(fb->buf, fb->len); // payload (image), payload length Serial.println("Saved file to path: "); Serial.println(pic_path.c_str()); } file.close(); /******************************************************************************/ Serial.println("Uploading via FTP"); /******************************************************************************/ /* FPT */ ftp.OpenConnection(); //Create a file and write the image data to it; ftp.InitFile("Type I"); ftp.ChangeWorkDir("/"); // change it to reflect your directory const char *f_name = pic_name.c_str(); ftp.NewFile( f_name ); ftp.WriteData(fb->buf, fb->len); ftp.CloseFile(); /******************************************************************************/ // Breath, withouth delay URL failed to update. delay(100); }