Advertisement
MrLunk

ESP32-cam-Timelapse-with-NTP-Timesync.ino

Feb 5th, 2021
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.40 KB | None | 0 0
  1. #include "FS.h"
  2. #include "SD_MMC.h"
  3. #include "esp_camera.h"
  4. #include <WiFi.h>
  5. #include "time.h"
  6. #include <EEPROM.h> // read and write from flash memory
  7.  
  8. // Your Wifi SSID and Password - Only used to sync time to NTP time server on internet
  9. // This way your files don't get saved with wrong timecode (1980's without any timesync ;)
  10. const char* ssid = "YOUR_Network_SSID";
  11. const char* password = "YOUR_NEtwork_PAssword";
  12.  
  13. const char* ntpServer = "pool.ntp.org";
  14. const long gmtOffset_sec = 3600;
  15. const int daylightOffset_sec = 3600;
  16.  
  17. // Select camera pin configuration
  18. #define CAMERA_MODEL_AI_THINKER
  19. #include "camera_pins.h"
  20.  
  21. // define number of bytes to set aside in persistant memory
  22. #define EEPROM_SIZE 1
  23. long pictureNumber = 0;
  24.  
  25. static esp_err_t get_image(fs::FS &fs, const char * path) {
  26. // Variable definitions for camera frame buffer
  27. camera_fb_t * fb = NULL;
  28. int64_t fr_start = esp_timer_get_time();
  29.  
  30. // Get the contents of the camera frame buffer
  31. fb = esp_camera_fb_get();
  32. if (!fb) {
  33. Serial.println("Camera capture failed");
  34. return ESP_FAIL;
  35. }
  36.  
  37. // Save image to file
  38. size_t fb_len = 0;
  39. fb_len = fb->len;
  40. File file = fs.open(path, FILE_WRITE);
  41. if(!file){
  42. Serial.println("Failed to open file in writing mode");
  43. } else {
  44. file.write(fb->buf, fb->len); // payload (image), payload length
  45. Serial.printf("Saved file to path: %s\n", path);
  46. }
  47. file.close();
  48.  
  49. // Release the camera frame buffer
  50. esp_camera_fb_return(fb);
  51. int64_t fr_end = esp_timer_get_time();
  52. Serial.printf("JPG: %uB %ums\n", (uint32_t)(fb_len), (uint32_t)((fr_end - fr_start)/1000));
  53. return ESP_OK;
  54. }
  55.  
  56. void printLocalTime()
  57. {
  58. struct tm timeinfo;
  59. if(!getLocalTime(&timeinfo)){
  60. Serial.println("Failed to obtain time");
  61. return;
  62. }
  63. Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
  64. }
  65.  
  66. void setup() {
  67. Serial.begin(115200);
  68. // Serial.setDebugOutput(true);
  69. Serial.println();
  70.  
  71. // Initial camera configuration
  72. camera_config_t config;
  73. config.ledc_channel = LEDC_CHANNEL_0;
  74. config.ledc_timer = LEDC_TIMER_0;
  75. config.pin_d0 = Y2_GPIO_NUM;
  76. config.pin_d1 = Y3_GPIO_NUM;
  77. config.pin_d2 = Y4_GPIO_NUM;
  78. config.pin_d3 = Y5_GPIO_NUM;
  79. config.pin_d4 = Y6_GPIO_NUM;
  80. config.pin_d5 = Y7_GPIO_NUM;
  81. config.pin_d6 = Y8_GPIO_NUM;
  82. config.pin_d7 = Y9_GPIO_NUM;
  83. config.pin_xclk = XCLK_GPIO_NUM;
  84. config.pin_pclk = PCLK_GPIO_NUM;
  85. config.pin_vsync = VSYNC_GPIO_NUM;
  86. config.pin_href = HREF_GPIO_NUM;
  87. config.pin_sscb_sda = SIOD_GPIO_NUM;
  88. config.pin_sscb_scl = SIOC_GPIO_NUM;
  89. config.pin_pwdn = PWDN_GPIO_NUM;
  90. config.pin_reset = RESET_GPIO_NUM;
  91. config.xclk_freq_hz = 20000000;
  92. config.pixel_format = PIXFORMAT_JPEG; // This is very important for saving the frame buffer as a JPeg
  93.  
  94. //init with high specs to pre-allocate larger buffers
  95. if(psramFound()){
  96. config.frame_size = FRAMESIZE_UXGA;
  97. config.jpeg_quality = 10;
  98. config.fb_count = 2;
  99. } else {
  100. config.frame_size = FRAMESIZE_SVGA;
  101. config.jpeg_quality = 12;
  102. config.fb_count = 1;
  103. }
  104.  
  105. // camera init
  106. esp_err_t err = esp_camera_init(&config);
  107. if (err != ESP_OK) {
  108. Serial.printf("Camera init failed with error 0x%x", err);
  109. return;
  110. }
  111.  
  112. /*****
  113. You may need to adjust these settings to get a picture you like.
  114. Look in app_httpd.cpp in the CameraWebServer as an example on how these are used.
  115. sensor_t * s = esp_camera_sensor_get();
  116. int res = 0;
  117. if(!strcmp(variable, "framesize")) {
  118. if(s->pixformat == PIXFORMAT_JPEG) res = s->set_framesize(s, (framesize_t)val);
  119. }
  120. else if(!strcmp(variable, "quality")) res = s->set_quality(s, val);
  121. else if(!strcmp(variable, "contrast")) res = s->set_contrast(s, val);
  122. else if(!strcmp(variable, "brightness")) res = s->set_brightness(s, val);
  123. else if(!strcmp(variable, "saturation")) res = s->set_saturation(s, val);
  124. else if(!strcmp(variable, "gainceiling")) res = s->set_gainceiling(s, (gainceiling_t)val);
  125. else if(!strcmp(variable, "colorbar")) res = s->set_colorbar(s, val);
  126. else if(!strcmp(variable, "awb")) res = s->set_whitebal(s, val);
  127. else if(!strcmp(variable, "agc")) res = s->set_gain_ctrl(s, val);
  128. else if(!strcmp(variable, "aec")) res = s->set_exposure_ctrl(s, val);
  129. else if(!strcmp(variable, "hmirror")) res = s->set_hmirror(s, val);
  130. else if(!strcmp(variable, "vflip")) res = s->set_vflip(s, val);
  131. else if(!strcmp(variable, "awb_gain")) res = s->set_awb_gain(s, val);
  132. else if(!strcmp(variable, "agc_gain")) res = s->set_agc_gain(s, val);
  133. else if(!strcmp(variable, "aec_value")) res = s->set_aec_value(s, val);
  134. else if(!strcmp(variable, "aec2")) res = s->set_aec2(s, val);
  135. else if(!strcmp(variable, "dcw")) res = s->set_dcw(s, val);
  136. else if(!strcmp(variable, "bpc")) res = s->set_bpc(s, val);
  137. else if(!strcmp(variable, "wpc")) res = s->set_wpc(s, val);
  138. else if(!strcmp(variable, "raw_gma")) res = s->set_raw_gma(s, val);
  139. else if(!strcmp(variable, "lenc")) res = s->set_lenc(s, val);
  140. else if(!strcmp(variable, "special_effect")) res = s->set_special_effect(s, val);
  141. else if(!strcmp(variable, "wb_mode")) res = s->set_wb_mode(s, val);
  142. else if(!strcmp(variable, "ae_level")) res = s->set_ae_level(s, val);
  143. *****/
  144.  
  145. Serial.println("Camera initialized!");
  146.  
  147. if(!SD_MMC.begin()){
  148. Serial.println("Card Mount Failed");
  149. return;
  150. }
  151. uint8_t cardType = SD_MMC.cardType();
  152. if(cardType == CARD_NONE){
  153. Serial.println("No SD_MMC card attached");
  154. return;
  155. }
  156. Serial.println("SD Card Initialized");
  157.  
  158. // initialize EEPROM with predefined size
  159. EEPROM.begin(EEPROM_SIZE);
  160. pictureNumber = EEPROM.read(0) + 1;
  161.  
  162. //connect to WiFi
  163. Serial.printf("Connecting to %s ", ssid);
  164. WiFi.begin(ssid, password);
  165. while (WiFi.status() != WL_CONNECTED) {
  166. delay(500);
  167. Serial.print(".");
  168. }
  169. Serial.println(" CONNECTED");
  170.  
  171. //init and get the time
  172. configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
  173. printLocalTime();
  174.  
  175. //disconnect WiFi as it's no longer needed
  176. WiFi.disconnect(true);
  177. WiFi.mode(WIFI_OFF);
  178.  
  179. }
  180.  
  181. void loop() {
  182.  
  183. pictureNumber = pictureNumber + 1;
  184. // Call function to capture the image and save it as a file
  185. String path = "/picture" + String(pictureNumber) + ".jpg";
  186. if(get_image(SD_MMC, path.c_str()) != ESP_OK ) {
  187. Serial.println("Failed to capture picture");
  188. }
  189. printLocalTime();
  190. delay(5000); // Set the desired delay between images
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement