esp32-cam-ch340_capture_photo
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
esp32-cam-ch340_capture_photo [2025/03/19 14:08] – created bruno | esp32-cam-ch340_capture_photo [2025/03/19 14:15] (current) – bruno | ||
---|---|---|---|
Line 63: | Line 63: | ||
} | } | ||
} | } | ||
+ | | ||
+ | == Ca donne == | ||
+ | |||
+ | <code c> | ||
+ | /********* | ||
+ | Rui Santos | ||
+ | Complete project details at https:// | ||
+ | |||
+ | The above copyright notice and this permission notice shall be included in all | ||
+ | copies or substantial portions of the Software. | ||
+ | *********/ | ||
+ | |||
+ | #include " | ||
+ | #include " | ||
+ | #include " | ||
+ | #include " | ||
+ | #include " | ||
+ | #include " | ||
+ | #include " | ||
+ | #include " | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | // Replace with your network credentials | ||
+ | const char* ssid = " | ||
+ | const char* password = " | ||
+ | |||
+ | // Create AsyncWebServer object on port 80 | ||
+ | AsyncWebServer server(80); | ||
+ | |||
+ | boolean takeNewPhoto = false; | ||
+ | boolean flashlight = false; | ||
+ | |||
+ | // Photo File Name to save in SPIFFS | ||
+ | #define FILE_PHOTO "/ | ||
+ | |||
+ | // OV2640 camera module pins (CAMERA_MODEL_AI_THINKER) | ||
+ | #define PWDN_GPIO_NUM | ||
+ | #define RESET_GPIO_NUM | ||
+ | #define XCLK_GPIO_NUM | ||
+ | #define SIOD_GPIO_NUM | ||
+ | #define SIOC_GPIO_NUM | ||
+ | #define Y9_GPIO_NUM | ||
+ | #define Y8_GPIO_NUM | ||
+ | #define Y7_GPIO_NUM | ||
+ | #define Y6_GPIO_NUM | ||
+ | #define Y5_GPIO_NUM | ||
+ | #define Y4_GPIO_NUM | ||
+ | #define Y3_GPIO_NUM | ||
+ | #define Y2_GPIO_NUM | ||
+ | #define VSYNC_GPIO_NUM | ||
+ | #define HREF_GPIO_NUM | ||
+ | #define PCLK_GPIO_NUM | ||
+ | |||
+ | #define LED_BUILTIN 4 // flashlight | ||
+ | |||
+ | const char index_html[] PROGMEM = R" | ||
+ | < | ||
+ | < | ||
+ | <meta name=" | ||
+ | < | ||
+ | body { text-align: | ||
+ | .vert { margin-bottom: | ||
+ | .hori{ margin-bottom: | ||
+ | </ | ||
+ | </ | ||
+ | < | ||
+ | <div id=" | ||
+ | < | ||
+ | <p>It might take more than 5 seconds to capture a photo.</ | ||
+ | <p> | ||
+ | <button onclick=" | ||
+ | <button onclick=" | ||
+ | <button onclick=" | ||
+ | <button onclick=" | ||
+ | </p> | ||
+ | </ | ||
+ | < | ||
+ | </ | ||
+ | < | ||
+ | var deg = 0; | ||
+ | function capturePhoto() { | ||
+ | var xhr = new XMLHttpRequest(); | ||
+ | xhr.open(' | ||
+ | xhr.send(); | ||
+ | } | ||
+ | function captureWithFlashlight() { | ||
+ | var xhr = new XMLHttpRequest(); | ||
+ | xhr.open(' | ||
+ | xhr.send(); | ||
+ | } | ||
+ | function rotatePhoto() { | ||
+ | var img = document.getElementById(" | ||
+ | deg += 90; | ||
+ | if(isOdd(deg/ | ||
+ | else{ document.getElementById(" | ||
+ | img.style.transform = " | ||
+ | } | ||
+ | function isOdd(n) { return Math.abs(n % 2) == 1; } | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | void setup() { | ||
+ | // Serial port for debugging purposes | ||
+ | Serial.begin(115200); | ||
+ | |||
+ | pinMode (LED_BUILTIN, | ||
+ | | ||
+ | // Connect to Wi-Fi | ||
+ | WiFi.begin(ssid, | ||
+ | while (WiFi.status() != WL_CONNECTED) { | ||
+ | delay(1000); | ||
+ | Serial.println(" | ||
+ | } | ||
+ | if (!SPIFFS.begin(true)) { | ||
+ | Serial.println(" | ||
+ | ESP.restart(); | ||
+ | } | ||
+ | else { | ||
+ | delay(500); | ||
+ | Serial.println(" | ||
+ | } | ||
+ | |||
+ | // Print ESP32 Local IP Address | ||
+ | Serial.print(" | ||
+ | Serial.println(WiFi.localIP()); | ||
+ | |||
+ | // Turn-off the ' | ||
+ | WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, | ||
+ | |||
+ | // OV2640 camera module | ||
+ | camera_config_t config; | ||
+ | config.ledc_channel = LEDC_CHANNEL_0; | ||
+ | config.ledc_timer = LEDC_TIMER_0; | ||
+ | config.pin_d0 = Y2_GPIO_NUM; | ||
+ | config.pin_d1 = Y3_GPIO_NUM; | ||
+ | config.pin_d2 = Y4_GPIO_NUM; | ||
+ | config.pin_d3 = Y5_GPIO_NUM; | ||
+ | config.pin_d4 = Y6_GPIO_NUM; | ||
+ | config.pin_d5 = Y7_GPIO_NUM; | ||
+ | config.pin_d6 = Y8_GPIO_NUM; | ||
+ | config.pin_d7 = Y9_GPIO_NUM; | ||
+ | config.pin_xclk = XCLK_GPIO_NUM; | ||
+ | config.pin_pclk = PCLK_GPIO_NUM; | ||
+ | config.pin_vsync = VSYNC_GPIO_NUM; | ||
+ | config.pin_href = HREF_GPIO_NUM; | ||
+ | config.pin_sccb_sda = SIOD_GPIO_NUM; | ||
+ | config.pin_sccb_scl = SIOC_GPIO_NUM; | ||
+ | config.pin_pwdn = PWDN_GPIO_NUM; | ||
+ | config.pin_reset = RESET_GPIO_NUM; | ||
+ | config.xclk_freq_hz = 20000000; | ||
+ | config.pixel_format = PIXFORMAT_JPEG; | ||
+ | |||
+ | if (psramFound()) { | ||
+ | config.frame_size = FRAMESIZE_UXGA; | ||
+ | config.jpeg_quality = 10; | ||
+ | config.fb_count = 2; | ||
+ | } else { | ||
+ | config.frame_size = FRAMESIZE_SVGA; | ||
+ | config.jpeg_quality = 12; | ||
+ | config.fb_count = 1; | ||
+ | } | ||
+ | // Camera init | ||
+ | esp_err_t err = esp_camera_init(& | ||
+ | if (err != ESP_OK) { | ||
+ | Serial.printf(" | ||
+ | ESP.restart(); | ||
+ | } | ||
+ | |||
+ | // Route for root / web page | ||
+ | server.on("/", | ||
+ | request-> | ||
+ | }); | ||
+ | |||
+ | server.on("/ | ||
+ | takeNewPhoto = true; | ||
+ | request-> | ||
+ | }); | ||
+ | |||
+ | server.on("/ | ||
+ | takeNewPhoto = true; | ||
+ | flashlight = true; | ||
+ | request-> | ||
+ | }); | ||
+ | |||
+ | |||
+ | server.on("/ | ||
+ | request-> | ||
+ | }); | ||
+ | |||
+ | // Start server | ||
+ | server.begin(); | ||
+ | |||
+ | } | ||
+ | |||
+ | void loop() { | ||
+ | if (takeNewPhoto) { | ||
+ | if (flashlight) digitalWrite(LED_BUILTIN, | ||
+ | capturePhotoSaveSpiffs(); | ||
+ | takeNewPhoto = false; | ||
+ | if (flashlight) { | ||
+ | digitalWrite(LED_BUILTIN, | ||
+ | flashlight = false; | ||
+ | } | ||
+ | } | ||
+ | delay(1); | ||
+ | } | ||
+ | |||
+ | // Check if photo capture was successful | ||
+ | bool checkPhoto( fs::FS &fs ) { | ||
+ | File f_pic = fs.open( FILE_PHOTO ); | ||
+ | unsigned int pic_sz = f_pic.size(); | ||
+ | return ( pic_sz > 100 ); | ||
+ | } | ||
+ | |||
+ | // Capture Photo and Save it to SPIFFS | ||
+ | void capturePhotoSaveSpiffs( void ) { | ||
+ | camera_fb_t * fb = NULL; // pointer | ||
+ | bool ok = 0; // Boolean indicating if the picture has been taken correctly | ||
+ | |||
+ | do { | ||
+ | // Take a photo with the camera | ||
+ | Serial.println(" | ||
+ | |||
+ | fb = esp_camera_fb_get(); | ||
+ | if (!fb) { | ||
+ | Serial.println(" | ||
+ | return; | ||
+ | } | ||
+ | |||
+ | // Photo file name | ||
+ | Serial.printf(" | ||
+ | File file = SPIFFS.open(FILE_PHOTO, | ||
+ | |||
+ | // Insert the data in the photo file | ||
+ | if (!file) { | ||
+ | Serial.println(" | ||
+ | } | ||
+ | else { | ||
+ | file.write(fb-> | ||
+ | Serial.print(" | ||
+ | Serial.print(FILE_PHOTO); | ||
+ | Serial.print(" | ||
+ | Serial.print(file.size()); | ||
+ | Serial.println(" | ||
+ | } | ||
+ | // Close the file | ||
+ | file.close(); | ||
+ | esp_camera_fb_return(fb); | ||
+ | |||
+ | // check if file has been correctly saved in SPIFFS | ||
+ | ok = checkPhoto(SPIFFS); | ||
+ | } while ( !ok ); | ||
+ | } | ||
+ | </ |
esp32-cam-ch340_capture_photo.1742393307.txt.gz · Last modified: 2025/03/19 14:08 by bruno