Advertisement
hms11

CoopCamTest2_BlynkEmail

May 19th, 2021
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.86 KB | None | 0 0
  1.  
  2. #define BLYNK_PRINT Serial
  3. #define BUTTON 13
  4. #define LED 4
  5. #define DOORUP 14
  6. #define DOORDOWN 15
  7.  
  8. #include "esp_camera.h"
  9. #include "SPI.h"
  10. #include "driver/rtc_io.h"
  11. #include "ESP32_MailClient.h"
  12. #include <FS.h>
  13. #include <SPIFFS.h>
  14. #include <WiFi.h>
  15. #include <WiFiClient.h>
  16. #include <BlynkSimpleEsp32.h>
  17. #include <ArduinoOTA.h>
  18. WidgetLED led1(V1);
  19. WidgetLED led2(V2);
  20. WidgetLED led3(V3);
  21. WidgetLED led4(V4);
  22.  
  23.  
  24. // Your WiFi credentials & Authentication Code from Blynk
  25. // Set password to "" for open networks.
  26.  
  27. const char* ssid = "xxxxxxx";
  28. const char* password = "xxxxxxxxx";
  29. char auth[] = "xxxxxxxxx"; //sent by Blynk
  30.  
  31.  
  32. // Serial Communication Variables
  33.  
  34. char coopRx; // Info received from CoopCommand
  35. bool newDataRx = false; //has CoopCam received new data from CoopCommand
  36. bool takePhoto = false; //Did the Blynk App request a photo?
  37.  
  38. // Timers
  39.  
  40. unsigned long photoTimer = 500; // Time to run flash before taking photo
  41. unsigned long lastPhotoTimer = 0; // Last time the flash timer was checked
  42. unsigned long wifiTimer = 30000; // How often to try re-connecting if WIFI lost
  43. unsigned long lastWifiTimer = 0; // Last time the WIFI re-connect timer was checked
  44.  
  45.  
  46.  
  47. // To send Email using Gmail use port 465 (SSL) and SMTP Server smtp.gmail.com
  48. // YOU MUST ENABLE less secure app option https://myaccount.google.com/lesssecureapps?pli=1
  49. #define emailSenderAccount "xxxxxxx"
  50. #define emailSenderPassword "xxxxxxx"
  51. #define smtpServer "smtp.gmail.com"
  52. #define smtpServerPort 465
  53.  
  54. #define CAMERA_MODEL_AI_THINKER
  55.  
  56. #if defined(CAMERA_MODEL_AI_THINKER)
  57. #define PWDN_GPIO_NUM 32
  58. #define RESET_GPIO_NUM -1
  59. #define XCLK_GPIO_NUM 0
  60. #define SIOD_GPIO_NUM 26
  61. #define SIOC_GPIO_NUM 27
  62.  
  63. #define Y9_GPIO_NUM 35
  64. #define Y8_GPIO_NUM 34
  65. #define Y7_GPIO_NUM 39
  66. #define Y6_GPIO_NUM 36
  67. #define Y5_GPIO_NUM 21
  68. #define Y4_GPIO_NUM 19
  69. #define Y3_GPIO_NUM 18
  70. #define Y2_GPIO_NUM 5
  71. #define VSYNC_GPIO_NUM 25
  72. #define HREF_GPIO_NUM 23
  73. #define PCLK_GPIO_NUM 22
  74. #else
  75. #error "Camera model not selected"
  76. #endif
  77.  
  78. // The Email Sending data object contains config and data to send
  79. SMTPData smtpData;
  80.  
  81. // Photo File Name to save in SPIFFS
  82. #define FILE_PHOTO "/photo.jpg"
  83.  
  84. void setup() {
  85.  
  86. WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); //disable brownout detector
  87. pinMode(LED, OUTPUT);
  88. // Serial, WI-FI & Blynk Communication
  89. Serial.begin(115200);
  90. Blynk.begin(auth, ssid, password);
  91. ArduinoOTA.begin();
  92.  
  93. // You can also specify server:
  94. //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
  95. //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);
  96.  
  97.  
  98. if (!SPIFFS.begin(true)) {
  99. ESP.restart();
  100. }
  101. else {
  102. delay(500);
  103. }
  104. camera_config_t config;
  105. config.ledc_channel = LEDC_CHANNEL_0;
  106. config.ledc_timer = LEDC_TIMER_0;
  107. config.pin_d0 = Y2_GPIO_NUM;
  108. config.pin_d1 = Y3_GPIO_NUM;
  109. config.pin_d2 = Y4_GPIO_NUM;
  110. config.pin_d3 = Y5_GPIO_NUM;
  111. config.pin_d4 = Y6_GPIO_NUM;
  112. config.pin_d5 = Y7_GPIO_NUM;
  113. config.pin_d6 = Y8_GPIO_NUM;
  114. config.pin_d7 = Y9_GPIO_NUM;
  115. config.pin_xclk = XCLK_GPIO_NUM;
  116. config.pin_pclk = PCLK_GPIO_NUM;
  117. config.pin_vsync = VSYNC_GPIO_NUM;
  118. config.pin_href = HREF_GPIO_NUM;
  119. config.pin_sscb_sda = SIOD_GPIO_NUM;
  120. config.pin_sscb_scl = SIOC_GPIO_NUM;
  121. config.pin_pwdn = PWDN_GPIO_NUM;
  122. config.pin_reset = RESET_GPIO_NUM;
  123. config.xclk_freq_hz = 20000000;
  124. config.pixel_format = PIXFORMAT_JPEG;
  125.  
  126. if (psramFound()) {
  127. config.frame_size = FRAMESIZE_UXGA;
  128. config.jpeg_quality = 10;
  129. config.fb_count = 2;
  130. } else {
  131. config.frame_size = FRAMESIZE_SVGA;
  132. config.jpeg_quality = 12;
  133. config.fb_count = 1;
  134. }
  135.  
  136. // Initialize camera
  137. esp_err_t err = esp_camera_init(&config);
  138. if (err != ESP_OK) {
  139. return;
  140. }
  141.  
  142. }
  143.  
  144. // Check if photo capture was successful
  145. bool checkPhoto( fs::FS &fs ) {
  146. File f_pic = fs.open( FILE_PHOTO );
  147. unsigned int pic_sz = f_pic.size();
  148. return ( pic_sz > 100 );
  149. }
  150.  
  151. // Capture Photo and Save it to SPIFFS
  152. void capturePhotoSaveSpiffs( void ) {
  153. camera_fb_t * fb = NULL; // pointer
  154. bool ok = 0; // Boolean indicating if the picture has been taken correctly
  155.  
  156. do {
  157. // Take a photo with the camera
  158. fb = esp_camera_fb_get();
  159. if (!fb) {
  160. return;
  161. }
  162.  
  163. // Photo file name
  164. File file = SPIFFS.open(FILE_PHOTO, FILE_WRITE);
  165.  
  166. // Insert the data in the photo file
  167. if (!file) {
  168. }
  169. else {
  170. file.write(fb->buf, fb->len); // payload (image), payload length
  171.  
  172. }
  173. // Close the file
  174. file.close();
  175. esp_camera_fb_return(fb);
  176.  
  177. // check if file has been correctly saved in SPIFFS
  178. ok = checkPhoto(SPIFFS);
  179. } while ( !ok );
  180. }
  181.  
  182. void sendPhoto( void ) {
  183. // Preparing email
  184. // Set the SMTP Server Email host, port, account and password
  185. smtpData.setLogin(smtpServer, smtpServerPort, emailSenderAccount, emailSenderPassword);
  186.  
  187. // Set the sender name and Email
  188. smtpData.setSender("ESP32-CAM", emailSenderAccount);
  189.  
  190. // Set Email priority or importance High, Normal, Low or 1 to 5 (1 is highest)
  191. smtpData.setPriority("High");
  192.  
  193. // Set the subject
  194. smtpData.setSubject(emailSubject);
  195.  
  196. // Set the email message in HTML format
  197. smtpData.setMessage("<h2>Photo captured with ESP32-CAM and attached in this email.</h2>", true);
  198. // Set the email message in text format
  199. //smtpData.setMessage("Photo captured with ESP32-CAM and attached in this email.", false);
  200.  
  201. // Add recipients, can add more than one recipient
  202. smtpData.addRecipient(emailRecipient);
  203. //smtpData.addRecipient(emailRecipient2);
  204.  
  205. // Add attach files from SPIFFS
  206. smtpData.addAttachFile(FILE_PHOTO, "image/jpg");
  207. // Set the storage type to attach files in your email (SPIFFS)
  208. smtpData.setFileStorageType(MailClientStorageType::SPIFFS);
  209.  
  210. smtpData.setSendCallback(sendCallback);
  211.  
  212. // Start sending Email, can be set callback function to track the status
  213. if (!MailClient.sendMail(smtpData))
  214.  
  215. // Clear all data from Email object to free memory
  216. smtpData.empty();
  217. }
  218.  
  219. // Callback function to get the Email sending status
  220. void sendCallback(SendStatus msg) {
  221. //Print the current status
  222. }
  223.  
  224. void coopCom ( void ) {
  225. if (Serial.available() > 0) {
  226. coopRx = Serial.read();
  227. newDataRx = true;
  228. }
  229. if (newDataRx == true) {
  230. if (coopRx == 'O') { //If CoopCommand says the door is up
  231. led1.on();
  232. led2.off();
  233. led3.off();
  234. led4.off();
  235. newDataRx = false;
  236. }
  237. if (coopRx == 'S') { //If CoopCommand says the door is down
  238. led1.off();
  239. led2.on();
  240. led3.off();
  241. led4.off();
  242. newDataRx = false;
  243. }
  244. if (coopRx == 'U') { //If CoopCommand says the door is opening
  245. led1.off();
  246. led2.off();
  247. led3.on();
  248. led4.off();
  249. newDataRx = false;
  250. }
  251. if (coopRx == 'D') { //If CoopCommand says the door is closing
  252. led1.off();
  253. led2.off();
  254. led3.off();
  255. led4.on();
  256. newDataRx = false;
  257. }
  258. }
  259. if (digitalRead(DOORUP) == HIGH) {
  260. Serial.print('U');
  261. }
  262. if (digitalRead(DOORDOWN) == HIGH) {
  263. Serial.print('D');
  264. }
  265. }
  266.  
  267. void photoRequest ( void ) {
  268. if (takePhoto) {
  269. digitalWrite(LED, HIGH);
  270.  
  271. if ((unsigned long)(millis() - lastPhotoTimer) >= photoTimer) {
  272. capturePhotoSaveSpiffs();
  273. sendPhoto();
  274. digitalWrite(LED, LOW);
  275. takePhoto = false;
  276. }
  277. }
  278. }
  279.  
  280. void wifiLost ( void ) {
  281. if (WiFi.status() == WL_CONNECTED) {
  282. lastWifiTimer = millis();
  283. }
  284. else if (WiFi.status() !=WL_CONNECTED) {
  285. if ((unsigned long)(millis() - lastWifiTimer) >= wifiTimer) {
  286. ESP.restart();
  287. }
  288. }
  289. }
  290.  
  291. void loop() {
  292. coopCom();
  293. photoRequest();
  294. wifiLost();
  295. ArduinoOTA.handle();
  296. Blynk.run();
  297. if (digitalRead(BUTTON) == HIGH) {
  298. takePhoto = true;
  299. lastPhotoTimer = millis();
  300. }
  301. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement