#include "esp_http_server.h" #include "img_converters.h" #include "esp_camera.h" #include "camera_pinout.h" #include "esp32_s3_cam_ovxxxx.h" #include "board.h" #if defined CONFIG_CAMERA_AF_SUPPORT #include "esp_camera_af.h" #endif static const char *TAG = "Camera"; #define PART_BOUNDARY "123456789000000000000987654321" static const char* _STREAM_CONTENT_TYPE = "multipart/x-mixed-replace;boundary=" PART_BOUNDARY; static const char* _STREAM_BOUNDARY = "\r\n--" PART_BOUNDARY "\r\n"; static const char* _STREAM_PART = "Content-Type: image/jpeg\r\nContent-Length: %u\r\n\r\n"; #if ESP_CAMERA_SUPPORTED static camera_config_t camera_config = { .pin_pwdn = CAM_PIN_PWDN, .pin_reset = CAM_PIN_RESET, .pin_xclk = CAM_PIN_XCLK, .pin_sccb_sda = -1, .pin_sccb_scl = -1, .pin_d7 = CAM_PIN_D7, .pin_d6 = CAM_PIN_D6, .pin_d5 = CAM_PIN_D5, .pin_d4 = CAM_PIN_D4, .pin_d3 = CAM_PIN_D3, .pin_d2 = CAM_PIN_D2, .pin_d1 = CAM_PIN_D1, .pin_d0 = CAM_PIN_D0, .pin_vsync = CAM_PIN_VSYNC, .pin_href = CAM_PIN_HREF, .pin_pclk = CAM_PIN_PCLK, //XCLK 20MHz or 10MHz for OV2640 double FPS (Experimental) .xclk_freq_hz = 10000000, .ledc_timer = LEDC_TIMER_0, .ledc_channel = LEDC_CHANNEL_0, .pixel_format = PIXFORMAT_YUV422, //YUV422,GRAYSCALE,RGB565,JPEG .frame_size = FRAMESIZE_QVGA, //QQVGA-UXGA, For ESP32, do not use sizes above QVGA when not JPEG. The performance of the ESP32-S series has improved a lot, but JPEG mode always gives better frame rates. .jpeg_quality = 12, //0-63, for OV series camera sensors, lower number means higher quality .fb_count = 2, //When jpeg mode is used, if fb_count more than one, the driver will work in continuous mode. .fb_location = CAMERA_FB_IN_PSRAM, // .grab_mode = CAMERA_GRAB_WHEN_EMPTY, .grab_mode = CAMERA_GRAB_LATEST, }; esp_err_t init_camera(void) { //initialize the camera esp_err_t err = esp_camera_init(&camera_config); if (err != ESP_OK) { ESP_LOGE(TAG, "Camera Init Failed"); return err; } return ESP_OK; } void start_camera_server(void) { httpd_config_t config = HTTPD_DEFAULT_CONFIG(); httpd_handle_t server = NULL; if (httpd_start(&server, &config) == ESP_OK) { httpd_uri_t uri_capture = { .uri = "/pic", .method = HTTP_GET, .handler = capture_handler }; httpd_uri_t uri_stream = { .uri = "/stream", .method = HTTP_GET, .handler = stream_handler }; httpd_register_uri_handler(server, &uri_capture); httpd_register_uri_handler(server, &uri_stream); } } #if defined(CONFIG_CAMERA_AF_SUPPORT) && CONFIG_CAMERA_AF_SUPPORT static void maybe_init_autofocus(void) { sensor_t *s = esp_camera_sensor_get(); if (!s) { ESP_LOGW(TAG, "AF: no sensor handle"); return; } if (!esp_camera_af_is_supported(s)) { ESP_LOGI(TAG, "AF: not supported by this sensor"); return; } esp_camera_af_config_t af_cfg = { .mode = ESP_CAMERA_AF_MODE_AUTO, .timeout_ms = CONFIG_CAMERA_AF_DEFAULT_TIMEOUT_MS, }; esp_err_t ret = esp_camera_af_init(s, &af_cfg); if (ret != ESP_OK) { ESP_LOGW(TAG, "AF init failed: %s", esp_err_to_name(ret)); return; } ESP_LOGI(TAG, "AF initialized (AUTO mode)"); } #endif #endif esp_err_t capture_handler(httpd_req_t *req) { camera_fb_t *fb = esp_camera_fb_get(); if (!fb) { ESP_LOGE(TAG, "Camera capture failed"); httpd_resp_send_500(req); return ESP_FAIL; } uint8_t *jpeg_buf = NULL; size_t jpeg_len = 0; bool jpeg_converted = frame2jpg(fb, 50, &jpeg_buf, &jpeg_len); esp_camera_fb_return(fb); if (!jpeg_converted) { ESP_LOGE(TAG, "JPEG compression failed"); httpd_resp_send_500(req); return ESP_FAIL; } httpd_resp_set_type(req, "image/jpeg"); httpd_resp_send(req, (const char *)jpeg_buf, jpeg_len); free(jpeg_buf); return ESP_OK; } esp_err_t stream_handler(httpd_req_t *req) { camera_fb_t * fb = NULL; esp_err_t res = ESP_OK; size_t _jpg_buf_len = 0; uint8_t * _jpg_buf = NULL; char * part_buf[64]; res = httpd_resp_set_type(req, _STREAM_CONTENT_TYPE); if (res != ESP_OK) { return res; } while(true) { fb = esp_camera_fb_get(); if (!fb) { ESP_LOGE(TAG, "Camera capture failed"); res = ESP_FAIL; break; } bool jpeg_converted = frame2jpg(fb, 80, &_jpg_buf, &_jpg_buf_len); esp_camera_fb_return(fb); if (!jpeg_converted) { ESP_LOGE(TAG, "JPEG compression failed"); res = ESP_FAIL; break; } if (res == ESP_OK) { res = httpd_resp_send_chunk(req, _STREAM_BOUNDARY, strlen(_STREAM_BOUNDARY)); } if (res == ESP_OK) { size_t hlen = snprintf((char *)part_buf, 64, _STREAM_PART, _jpg_buf_len); res = httpd_resp_send_chunk(req, (const char *)part_buf, hlen); } if (res == ESP_OK) { res = httpd_resp_send_chunk(req, (const char *)_jpg_buf, _jpg_buf_len); } free(_jpg_buf); _jpg_buf = NULL; if (res != ESP_OK) { break; } vTaskDelay(1); } return res; }