Guest User

Untitled

a guest
Mar 27th, 2020
1,825
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "esp_camera.h"
  2. #include "SPI.h"
  3. #include "driver/rtc_io.h"
  4. #include "ESP32_MailClient.h"
  5. #include <FS.h>
  6. #include <SPIFFS.h>
  7. #include <WiFi.h>
  8.  
  9. // REPLACE WITH YOUR NETWORK CREDENTIALS
  10. const char* ssid = "REPLACE_WITH_YOUR_SSID";
  11. const char* password = "REPLACE_WITH_YOUR_PASSWORD";
  12.  
  13. // To send Email using Gmail use port 465 (SSL) and SMTP Server smtp.gmail.com
  14. // YOU MUST ENABLE less secure app option https://myaccount.google.com/lesssecureapps?pli=1
  15. #define emailSenderAccount "sender_email"
  16. #define emailSenderPassword "sender_passwrod"
  17. #define smtpServer "smtp.gmail.com"
  18. #define smtpServerPort 465
  19. #define emailSubject "[WARNING] ESP32-CAM Motion Detected"
  20. #define emailRecipient "recipient_email"
  21.  
  22. #define CAMERA_MODEL_AI_THINKER
  23.  
  24. #if defined(CAMERA_MODEL_AI_THINKER)
  25. #define PWDN_GPIO_NUM 32
  26. #define RESET_GPIO_NUM -1
  27. #define XCLK_GPIO_NUM 0
  28. #define SIOD_GPIO_NUM 26
  29. #define SIOC_GPIO_NUM 27
  30.  
  31. #define Y9_GPIO_NUM 35
  32. #define Y8_GPIO_NUM 34
  33. #define Y7_GPIO_NUM 39
  34. #define Y6_GPIO_NUM 36
  35. #define Y5_GPIO_NUM 21
  36. #define Y4_GPIO_NUM 19
  37. #define Y3_GPIO_NUM 18
  38. #define Y2_GPIO_NUM 5
  39. #define VSYNC_GPIO_NUM 25
  40. #define HREF_GPIO_NUM 23
  41. #define PCLK_GPIO_NUM 22
  42. #else
  43. #error "Camera model not selected"
  44. #endif
  45.  
  46. // The Email Sending data object contains config and data to send
  47. SMTPData smtpData;
  48.  
  49. // Photo File Name to save in SPIFFS
  50. #define FILE_PHOTO "/photo.jpg"
  51.  
  52. bool takePicture = false;
  53.  
  54. static void IRAM_ATTR detectsMovement(void * arg) {
  55. Serial.println("Motion Detected");
  56. takePicture = true;
  57. }
  58.  
  59. void enableInterrupt(){
  60. esp_err_t err = gpio_isr_handler_add(GPIO_NUM_14, &detectsMovement, (void *) 1);
  61. if (err != ESP_OK) {
  62. Serial.printf("handler add failed with error 0x%x \r\n", err);
  63. }
  64.  
  65. err = gpio_set_intr_type(GPIO_NUM_14, GPIO_INTR_POSEDGE);
  66. if (err != ESP_OK) {
  67. Serial.printf("set intr type failed with error 0x%x \r\n", err);
  68. }
  69. }
  70.  
  71. void setup() {
  72. WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); //disable brownout detector
  73.  
  74. Serial.begin(115200);
  75. Serial.println();
  76.  
  77. // Connect to Wi-Fi
  78. WiFi.begin(ssid, password);
  79. Serial.print("Connecting to WiFi...");
  80. while (WiFi.status() != WL_CONNECTED) {
  81. delay(500);
  82. Serial.print(".");
  83. }
  84. Serial.println();
  85.  
  86. if (!SPIFFS.begin(true)) {
  87. Serial.println("An Error has occurred while mounting SPIFFS");
  88. ESP.restart();
  89. }
  90. else {
  91. delay(500);
  92. Serial.println("SPIFFS mounted successfully");
  93. }
  94.  
  95. // Print ESP32 Local IP Address
  96. Serial.print("IP Address: http://");
  97. Serial.println(WiFi.localIP());
  98.  
  99. camera_config_t config;
  100. config.ledc_channel = LEDC_CHANNEL_0;
  101. config.ledc_timer = LEDC_TIMER_0;
  102. config.pin_d0 = Y2_GPIO_NUM;
  103. config.pin_d1 = Y3_GPIO_NUM;
  104. config.pin_d2 = Y4_GPIO_NUM;
  105. config.pin_d3 = Y5_GPIO_NUM;
  106. config.pin_d4 = Y6_GPIO_NUM;
  107. config.pin_d5 = Y7_GPIO_NUM;
  108. config.pin_d6 = Y8_GPIO_NUM;
  109. config.pin_d7 = Y9_GPIO_NUM;
  110. config.pin_xclk = XCLK_GPIO_NUM;
  111. config.pin_pclk = PCLK_GPIO_NUM;
  112. config.pin_vsync = VSYNC_GPIO_NUM;
  113. config.pin_href = HREF_GPIO_NUM;
  114. config.pin_sscb_sda = SIOD_GPIO_NUM;
  115. config.pin_sscb_scl = SIOC_GPIO_NUM;
  116. config.pin_pwdn = PWDN_GPIO_NUM;
  117. config.pin_reset = RESET_GPIO_NUM;
  118. config.xclk_freq_hz = 20000000;
  119. config.pixel_format = PIXFORMAT_JPEG;
  120.  
  121. if(psramFound()){
  122. config.frame_size = FRAMESIZE_UXGA;
  123. config.jpeg_quality = 10;
  124. config.fb_count = 2;
  125. } else {
  126. config.frame_size = FRAMESIZE_SVGA;
  127. config.jpeg_quality = 12;
  128. config.fb_count = 1;
  129. }
  130.  
  131. // Initialize camera
  132. esp_err_t err = esp_camera_init(&config);
  133. if (err != ESP_OK) {
  134. Serial.printf("Camera init failed with error 0x%x", err);
  135. return;
  136. }
  137. enableInterrupt();
  138. delay(1000);
  139. }
  140.  
  141. void loop() {
  142. if (takePicture){
  143. capturePhotoSaveSpiffs();
  144. sendPhoto();
  145. takePicture=false;
  146. }
  147. }
  148.  
  149. // Check if photo capture was successful
  150. bool checkPhoto( fs::FS &fs ) {
  151. File f_pic = fs.open( FILE_PHOTO );
  152. unsigned int pic_sz = f_pic.size();
  153. return ( pic_sz > 100 );
  154. }
  155.  
  156. // Capture Photo and Save it to SPIFFS
  157. void capturePhotoSaveSpiffs( void ) {
  158. camera_fb_t * fb = NULL; // pointer
  159. bool ok = 0; // Boolean indicating if the picture has been taken correctly
  160.  
  161. do {
  162. // Take a photo with the camera
  163. Serial.println("Taking a photo...");
  164.  
  165. fb = esp_camera_fb_get();
  166. if (!fb) {
  167. Serial.println("Camera capture failed");
  168. return;
  169. }
  170.  
  171. // Photo file name
  172. Serial.printf("Picture file name: %s\n", FILE_PHOTO);
  173. File file = SPIFFS.open(FILE_PHOTO, FILE_WRITE);
  174.  
  175. // Insert the data in the photo file
  176. if (!file) {
  177. Serial.println("Failed to open file in writing mode");
  178. }
  179. else {
  180. file.write(fb->buf, fb->len); // payload (image), payload length
  181. Serial.print("The picture has been saved in ");
  182. Serial.print(FILE_PHOTO);
  183. Serial.print(" - Size: ");
  184. Serial.print(file.size());
  185. Serial.println(" bytes");
  186. }
  187. // Close the file
  188. file.close();
  189. esp_camera_fb_return(fb);
  190.  
  191. // check if file has been correctly saved in SPIFFS
  192. ok = checkPhoto(SPIFFS);
  193. } while ( !ok );
  194. }
  195.  
  196. boolean sendPhoto( void ) {
  197. // Preparing email
  198. Serial.println("Sending email...");
  199. // Set the SMTP Server Email host, port, account and password
  200. smtpData.setLogin(smtpServer, smtpServerPort, emailSenderAccount, emailSenderPassword);
  201.  
  202. // Set the sender name and Email
  203. smtpData.setSender("ESP32-CAM", emailSenderAccount);
  204.  
  205. // Set Email priority or importance High, Normal, Low or 1 to 5 (1 is highest)
  206. smtpData.setPriority("High");
  207.  
  208. // Set the subject
  209. smtpData.setSubject(emailSubject);
  210.  
  211. // Set the email message in HTML format
  212. smtpData.setMessage("<h2>Motion Detected! Photo captured with ESP32-CAM attached in this email.</h2>", true);
  213. // Set the email message in text format
  214. //smtpData.setMessage("Motion Detected! Photo captured with ESP32-CAM attached in this email.", false);
  215.  
  216. // Add recipients, can add more than one recipient
  217. smtpData.addRecipient(emailRecipient);
  218. //smtpData.addRecipient(emailRecipient2);
  219.  
  220. // Add attach files from SPIFFS
  221. smtpData.addAttachFile(FILE_PHOTO, "image/jpg");
  222. // Set the storage type to attach files in your email (SPIFFS)
  223. smtpData.setFileStorageType(MailClientStorageType::SPIFFS);
  224.  
  225. smtpData.setSendCallback(sendCallback);
  226.  
  227. // Start sending Email, can be set callback function to track the status
  228. if (!MailClient.sendMail(smtpData))
  229. Serial.println("Error sending Email, " + MailClient.smtpErrorReason());
  230.  
  231. // Clear all data from Email object to free memory
  232. smtpData.empty();
  233.  
  234. SD.end();
  235. }
  236.  
  237. // Callback function to get the Email sending status
  238. void sendCallback(SendStatus msg) {
  239. //Print the current status
  240. Serial.println(msg.info());
  241. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×