CameraWebsocket_esp32_takephoto.ino 9.3 KB

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