Advertisement
hms11

CoopCommandCamCode

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