CameraWebsocket_esp32_sendpic.ino 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. // We include necessary libraries here:
  2. #include "esp_camera.h"
  3. #include "SPI.h"
  4. #include "driver/rtc_io.h"
  5. #include "ESP32_MailClient.h"
  6. #include <FS.h>
  7. #include <SPIFFS.h>
  8. #include <WiFi.h>
  9. #include <ArduinoWebsockets.h>
  10. #include <HTTPClient.h>
  11. // Replace with your local network data
  12. const char* ssid = "goldin_lab1";
  13. const char* password = "goldin53743001";
  14. const char* websocket_server_host = "60.250.156.230";
  15. const uint16_t websocket_server_port = 8093;
  16. String IFTTTUrl="http://maker.ifttt.com/trigger/Test/with/key/c3xo5EvpBX64fPEqxphcR4jBTzDh1r2joTDsB_BslOA";
  17. void startCameraServer();
  18. using namespace websockets;
  19. WebsocketsClient client;
  20. //Set Static IP Address
  21. IPAddress local_IP(192, 168, 50, 83);
  22. //Set Gateway IP Address
  23. IPAddress gateway(192, 168, 50, 254);
  24. IPAddress subnet(255, 255, 255, 0);
  25. IPAddress dns(8, 8, 8, 8); // Google DNS
  26. const char* getphoto ="getphoto";
  27. const char* statusok ="statusok";
  28. // Replace with your data
  29. #define emailSenderAccount "foresp32test@gmail.com"
  30. #define emailSenderPassword "Esp32test"
  31. #define smtpServer "smtp.gmail.com"
  32. #define smtpServerPort 465
  33. #define emailSubject "ESP32-CAM Picture"
  34. #define emailRecipient "service.gitcr081@gmail.com"
  35. // We define the pins used by the AI-Thinker ESP32-CAM:
  36. #define PWDN_GPIO_NUM 32
  37. #define RESET_GPIO_NUM -1
  38. #define XCLK_GPIO_NUM 0
  39. #define SIOD_GPIO_NUM 26
  40. #define SIOC_GPIO_NUM 27
  41. #define Y9_GPIO_NUM 35
  42. #define Y8_GPIO_NUM 34
  43. #define Y7_GPIO_NUM 39
  44. #define Y6_GPIO_NUM 36
  45. #define Y5_GPIO_NUM 21
  46. #define Y4_GPIO_NUM 19
  47. #define Y3_GPIO_NUM 18
  48. #define Y2_GPIO_NUM 5
  49. #define VSYNC_GPIO_NUM 25
  50. #define HREF_GPIO_NUM 23
  51. #define PCLK_GPIO_NUM 22
  52. // The Email Sending data object contains config and data to send
  53. SMTPData smtpData;
  54. // Picture file name to save in SPIFFS
  55. #define PICTURE_FILE "/picture.jpg"
  56. void setup() {
  57. WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); // We disable brownout detector
  58. Serial.begin(115200); // We initialise the serial communication at 115200 bps
  59. Serial.println();
  60. client.send("Hi Server!");
  61. if(!WiFi.config(local_IP, gateway, subnet,dns)) {
  62. Serial.println("Set IP Failed");
  63. }
  64. WiFi.begin(ssid, password);
  65. while (WiFi.status() != WL_CONNECTED) {
  66. delay(500);
  67. Serial.print(".");
  68. }
  69. Serial.println("");
  70. Serial.println("WiFi connected");
  71. startCameraServer();
  72. Serial.print("Camera Ready! Use 'http://");
  73. Serial.print(WiFi.localIP());
  74. Serial.println("' to connect");
  75. while(!client.connect(websocket_server_host, websocket_server_port, "/")){
  76. delay(500);
  77. Serial.print(".");
  78. }
  79. Serial.println("Websocket Connected!");
  80. // We initialize SPI Flash File System (SPIFFS) to save the last picture taken with the ESP32-CAM:
  81. if (!SPIFFS.begin(true)) {
  82. Serial.println("An Error has occurred while mounting SPIFFS");
  83. ESP.restart();
  84. }
  85. else {
  86. delay(500);
  87. Serial.println("SPIFFS is mounted!");
  88. }
  89. // The following lines configure the camera and set the camera settings:
  90. camera_config_t config;
  91. config.ledc_channel = LEDC_CHANNEL_0;
  92. config.ledc_timer = LEDC_TIMER_0;
  93. config.pin_d0 = Y2_GPIO_NUM;
  94. config.pin_d1 = Y3_GPIO_NUM;
  95. config.pin_d2 = Y4_GPIO_NUM;
  96. config.pin_d3 = Y5_GPIO_NUM;
  97. config.pin_d4 = Y6_GPIO_NUM;
  98. config.pin_d5 = Y7_GPIO_NUM;
  99. config.pin_d6 = Y8_GPIO_NUM;
  100. config.pin_d7 = Y9_GPIO_NUM;
  101. config.pin_xclk = XCLK_GPIO_NUM;
  102. config.pin_pclk = PCLK_GPIO_NUM;
  103. config.pin_vsync = VSYNC_GPIO_NUM;
  104. config.pin_href = HREF_GPIO_NUM;
  105. config.pin_sscb_sda = SIOD_GPIO_NUM;
  106. config.pin_sscb_scl = SIOC_GPIO_NUM;
  107. config.pin_pwdn = PWDN_GPIO_NUM;
  108. config.pin_reset = RESET_GPIO_NUM;
  109. config.xclk_freq_hz = 20000000;
  110. config.pixel_format = PIXFORMAT_JPEG;
  111. if(psramFound()){
  112. config.frame_size = FRAMESIZE_UXGA;
  113. config.jpeg_quality = 10;
  114. config.fb_count = 2;
  115. } else {
  116. config.frame_size = FRAMESIZE_SVGA;
  117. config.jpeg_quality = 12;
  118. config.fb_count = 1;
  119. }
  120. // We initialize the camera
  121. esp_err_t err = esp_camera_init(&config);
  122. if (err != ESP_OK) {
  123. Serial.printf("Camera init failed with error 0x%x", err);
  124. return;
  125. }
  126. }
  127. // Take a picture and save it to the SPIFFS function
  128. void capturePictureSaveSpiffs( void ) {
  129. camera_fb_t * fb = NULL; // pointer
  130. bool ok = 0; // Boolean indicating if the picture has been taken correctly
  131. do {
  132. // We take a picture with the ESP32-CAM
  133. Serial.println("Taking a picture...");
  134. fb = esp_camera_fb_get();
  135. if (!fb) {
  136. Serial.println("Failed to take a picture!");
  137. return;
  138. }
  139. Serial.printf("Picture file name is %s\n", PICTURE_FILE);
  140. File file = SPIFFS.open(PICTURE_FILE, FILE_WRITE);
  141. // We insert the data in the picture file
  142. if (!file) {
  143. Serial.println("Failed to open file in writing mode");
  144. }
  145. else {
  146. file.write(fb->buf, fb->len); // payload (image), payload length
  147. Serial.print("The picture has been saved in ");
  148. Serial.print(PICTURE_FILE);
  149. Serial.print(" - Size: ");
  150. Serial.print(file.size());
  151. Serial.println(" bytes");
  152. }
  153. // We close the file
  154. file.close();
  155. esp_camera_fb_return(fb);
  156. // We check if file has been correctly saved in SPIFFS
  157. ok = checkPicture(SPIFFS);
  158. } while ( !ok );
  159. }
  160. // We check if picture capture was successful
  161. bool checkPicture( fs::FS &fs ) {
  162. File f_pic = fs.open( PICTURE_FILE );
  163. unsigned int pic_sz = f_pic.size();
  164. return ( pic_sz > 100 );
  165. }
  166. // Send picture function
  167. void sendPicture( void ) {
  168. Serial.println("Sending of an email is in progress...");
  169. // We set the SMTP Server Email host, port, account and password
  170. smtpData.setLogin(smtpServer, smtpServerPort, emailSenderAccount, emailSenderPassword);
  171. // We set the sender name and email
  172. smtpData.setSender("ESP32-CAM", emailSenderAccount);
  173. // We set email priority or importance High, Normal, Low or 1 to 5 (1 is highest)
  174. smtpData.setPriority("High");
  175. // We set the subject
  176. smtpData.setSubject(emailSubject);
  177. // We set the email message in HTML format
  178. smtpData.setMessage("<h2>Picture taken with ESP32-CAM attached in this email.</h2>", true);
  179. // uncomment this line if you want to set the email message in text format
  180. //smtpData.setMessage("Picture taken with ESP32-CAM attached in this email.", false);
  181. // We add recipients, you can have more than one recipient
  182. smtpData.addRecipient(emailRecipient);
  183. //smtpData.addRecipient(emailRecipient1);
  184. // We attach files from SPIFFS
  185. smtpData.addAttachFile(PICTURE_FILE, "image/jpg");
  186. // We set the storage type to attach files in your email (SPIFFS)
  187. smtpData.setFileStorageType(MailClientStorageType::SPIFFS);
  188. // We set a callback function that will be called upon sending an email
  189. smtpData.setSendCallback(sendCallback);
  190. // We start sending an email
  191. if (!MailClient.sendMail(smtpData))
  192. Serial.println("Error sending Email, " + MailClient.smtpErrorReason());
  193. // We clear all data from an object to free up a memory
  194. smtpData.empty();
  195. }
  196. // Callback function to get the email sending status
  197. void sendCallback(SendStatus msg) {
  198. // We print the current status
  199. Serial.println(msg.info());
  200. }
  201. void loop() {
  202. camera_fb_t *fb = esp_camera_fb_get();
  203. if(!fb){
  204. Serial.println("Camera capture failed");
  205. esp_camera_fb_return(fb);
  206. return;
  207. }
  208. if(fb->format != PIXFORMAT_JPEG){
  209. Serial.println("Non-JPEG data not implemented");
  210. return;
  211. }
  212. client.sendBinary((const char*) fb->buf, fb->len);
  213. esp_camera_fb_return(fb);
  214. if ((WiFi.status()!= WL_CONNECTED)){
  215. WiFi.reconnect();
  216. Serial.println("WiFi reconnected");
  217. }
  218. while(!client.connect(websocket_server_host, websocket_server_port, "/")){
  219. delay(500);
  220. Serial.print(".");
  221. }
  222. Serial.println("Websocket Connected!");
  223. String url=IFTTTUrl+"?value1="+getphoto+"&value2="+statusok;
  224. //client.poll();
  225. //Start to send data to IFTTT
  226. HTTPClient http;
  227. Serial.print("[HTTP] begin...\n");
  228. http.begin(url); //HTTP
  229. Serial.print("[HTTP] GET...\n");
  230. // start connection and send HTTP header
  231. int httpCode = http.GET();
  232. // httpCode will be negative on error
  233. if(httpCode > 0) {
  234. // HTTP header has been send and Server response header has been handled
  235. Serial.printf("[HTTP] GET... code: %d\n", httpCode);
  236. // file found at server
  237. if(httpCode == HTTP_CODE_OK) {
  238. String payload = http.getString();
  239. Serial.println(payload);
  240. }
  241. } else {
  242. Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
  243. }
  244. http.end();
  245. capturePictureSaveSpiffs();
  246. sendPicture();
  247. }