Advertisement
Guest User

Code for taking pictures ESP32

a guest
May 22nd, 2025
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.92 KB | Source Code | 0 0
  1. #include "WiFi.h"
  2. #include "esp_camera.h"
  3. #include "esp_timer.h"
  4. #include "img_converters.h"
  5. #include "Arduino.h"
  6. #include "soc/soc.h" // Disable brownour problems
  7. #include "soc/rtc_cntl_reg.h" // Disable brownour problems
  8. #include "driver/rtc_io.h"
  9. #include <ESPAsyncWebServer.h>
  10. #include <SPIFFS.h>
  11. #include <FS.h>
  12.  
  13. // Replace with your network credentials
  14. const char* ssid = "***";
  15. const char* password = "***";
  16.  
  17. // Create AsyncWebServer object on port 80
  18. AsyncWebServer server(80);
  19.  
  20. boolean takeNewPhoto = false;
  21.  
  22. // Photo File Name to save in SPIFFS
  23. #define FILE_PHOTO "/photo.jpg"
  24.  
  25. // OV2640 camera module pins (CAMERA_MODEL_AI_THINKER)
  26. #define PWDN_GPIO_NUM 32
  27. #define RESET_GPIO_NUM -1
  28. #define XCLK_GPIO_NUM 0
  29. #define SIOD_GPIO_NUM 26
  30. #define SIOC_GPIO_NUM 27
  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.  
  43. const char index_html[] PROGMEM = R"rawliteral(
  44. <!DOCTYPE HTML><html>
  45. <head>
  46. <meta name="viewport" content="width=device-width, initial-scale=1">
  47. <style>
  48. body { text-align:center; }
  49. .vert { margin-bottom: 10%; }
  50. .hori{ margin-bottom: 0%; }
  51. </style>
  52. </head>
  53. <body>
  54. <div id="container">
  55. <h2>ESP32-CAM Last Photo</h2>
  56. <p>It might take more than 5 seconds to capture a photo.</p>
  57. <p>
  58. <button onclick="rotatePhoto();">ROTATE</button>
  59. <button onclick="capturePhoto()">CAPTURE PHOTO</button>
  60. <button onclick="location.reload();">REFRESH PAGE</button>
  61. </p>
  62. </div>
  63. <div><img src="saved-photo" id="photo" width="70%"></div>
  64. </body>
  65. <script>
  66. var deg = 0;
  67. function capturePhoto() {
  68. var xhr = new XMLHttpRequest();
  69. xhr.open('GET', "/capture", true);
  70. xhr.send();
  71. }
  72. function rotatePhoto() {
  73. var img = document.getElementById("photo");
  74. deg += 90;
  75. if(isOdd(deg/90)){ document.getElementById("container").className = "vert"; }
  76. else{ document.getElementById("container").className = "hori"; }
  77. img.style.transform = "rotate(" + deg + "deg)";
  78. }
  79. function isOdd(n) { return Math.abs(n % 2) == 1; }
  80. </script>
  81. </html>)rawliteral";
  82.  
  83. void setup() {
  84. // Serial port for debugging purposes
  85. Serial.begin(115200);
  86.  
  87. // Connect to Wi-Fi
  88. WiFi.begin(ssid, password);
  89. while (WiFi.status() != WL_CONNECTED) {
  90. delay(1000);
  91. Serial.println("Connecting to WiFi...");
  92. }
  93. if (!SPIFFS.begin(true)) {
  94. Serial.println("An Error has occurred while mounting SPIFFS");
  95. ESP.restart();
  96. }
  97. else {
  98. delay(500);
  99. Serial.println("SPIFFS mounted successfully");
  100. }
  101.  
  102. // Print ESP32 Local IP Address
  103. Serial.print("IP Address: http://");
  104. Serial.println(WiFi.localIP());
  105.  
  106. // Turn-off the 'brownout detector'
  107. WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0);
  108.  
  109. // OV2640 camera module
  110. camera_config_t config;
  111. config.ledc_channel = LEDC_CHANNEL_0;
  112. config.ledc_timer = LEDC_TIMER_0;
  113. config.pin_d0 = Y2_GPIO_NUM;
  114. config.pin_d1 = Y3_GPIO_NUM;
  115. config.pin_d2 = Y4_GPIO_NUM;
  116. config.pin_d3 = Y5_GPIO_NUM;
  117. config.pin_d4 = Y6_GPIO_NUM;
  118. config.pin_d5 = Y7_GPIO_NUM;
  119. config.pin_d6 = Y8_GPIO_NUM;
  120. config.pin_d7 = Y9_GPIO_NUM;
  121. config.pin_xclk = XCLK_GPIO_NUM;
  122. config.pin_pclk = PCLK_GPIO_NUM;
  123. config.pin_vsync = VSYNC_GPIO_NUM;
  124. config.pin_href = HREF_GPIO_NUM;
  125. config.pin_sccb_sda = SIOD_GPIO_NUM;
  126. config.pin_sccb_scl = SIOC_GPIO_NUM;
  127. config.pin_pwdn = PWDN_GPIO_NUM;
  128. config.pin_reset = RESET_GPIO_NUM;
  129. config.xclk_freq_hz = 20000000;
  130. config.pixel_format = PIXFORMAT_JPEG;
  131.  
  132. if (psramFound()) {
  133. config.frame_size = FRAMESIZE_HVGA;/* FRAMESIZE_96X96, // 96x96
  134. FRAMESIZE_QQVGA, // 160x120
  135. FRAMESIZE_QCIF, // 176x144
  136. FRAMESIZE_HQVGA, // 240x176
  137. FRAMESIZE_240X240, // 240x240
  138. FRAMESIZE_QVGA, // 320x240
  139. FRAMESIZE_CIF, // 400x296
  140. FRAMESIZE_HVGA, // 480x320
  141. FRAMESIZE_VGA, // 640x480
  142. FRAMESIZE_SVGA, // 800x600
  143. FRAMESIZE_XGA, // 1024x768
  144. FRAMESIZE_HD, // 1280x720
  145. FRAMESIZE_SXGA, // 1280x1024
  146. FRAMESIZE_UXGA, // 1600x1200*/
  147. config.jpeg_quality = 24; /*It could be anything between 0 and 63.The smaller the number, the higher the quality*/
  148. config.fb_count = 2;
  149. Serial.println("FRAMESIZE_HVGA");
  150. }
  151.  
  152. //if (psramFound()) {
  153. // config.frame_size = FRAMESIZE_UXGA;
  154. // config.jpeg_quality = 10;
  155. // config.fb_count = 2;
  156. //}
  157. //else {
  158. // config.frame_size = FRAMESIZE_SVGA;
  159. // config.jpeg_quality = 12;
  160. // config.fb_count = 1;
  161. //}
  162. // Camera init
  163. esp_err_t err = esp_camera_init(&config);
  164. if (err != ESP_OK) {
  165. Serial.printf("Camera init failed with error 0x%x", err);
  166. ESP.restart();
  167. }
  168.  
  169. // Route for root / web page
  170. server.on("/", HTTP_GET, [](AsyncWebServerRequest * request) {
  171. request->send(200, "text/html", index_html);
  172. });
  173.  
  174. server.on("/capture", HTTP_GET, [](AsyncWebServerRequest * request) {
  175. takeNewPhoto = true;
  176. request->send(200, "text/plain", "Taking Photo");
  177. });
  178.  
  179. server.on("/saved-photo", HTTP_GET, [](AsyncWebServerRequest * request) {
  180. request->send(SPIFFS, FILE_PHOTO, "image/jpg", false);
  181. });
  182.  
  183. // Start server
  184. server.begin();
  185.  
  186. }
  187.  
  188. void loop() {
  189. if (takeNewPhoto) {
  190. capturePhotoSaveSpiffs();
  191. takeNewPhoto = false;
  192. }
  193. delay(1);
  194. }
  195.  
  196. // Check if photo capture was successful
  197. bool checkPhoto( fs::FS &fs ) {
  198. File f_pic = fs.open( FILE_PHOTO );
  199. unsigned int pic_sz = f_pic.size();
  200. return ( pic_sz > 100 );
  201. }
  202.  
  203. // Capture Photo and Save it to SPIFFS
  204. void capturePhotoSaveSpiffs( void ) {
  205. camera_fb_t * fb = NULL; // pointer
  206. bool ok = 0; // Boolean indicating if the picture has been taken correctly
  207.  
  208. do {
  209. // Take a photo with the camera
  210. Serial.println("Taking a photo...");
  211.  
  212. fb = esp_camera_fb_get();
  213. if (!fb) {
  214. Serial.println("Camera capture failed");
  215. return;
  216. }
  217.  
  218. // Photo file name
  219. Serial.printf("Picture file name: %s\n", FILE_PHOTO);
  220. File file = SPIFFS.open(FILE_PHOTO, FILE_WRITE);
  221.  
  222. // Insert the data in the photo file
  223. if (!file) {
  224. Serial.println("Failed to open file in writing mode");
  225. }
  226. else {
  227. file.write(fb->buf, fb->len); // payload (image), payload length
  228. Serial.print("The picture has been saved in ");
  229. Serial.print(FILE_PHOTO);
  230. Serial.print(" - Size: ");
  231. Serial.print(file.size());
  232. Serial.println(" bytes");
  233. }
  234. // Close the file
  235. file.close();
  236. esp_camera_fb_return(fb);
  237.  
  238. // check if file has been correctly saved in SPIFFS
  239. ok = checkPhoto(SPIFFS);
  240. } while ( !ok );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement