| #include “esp_camera.h”
|
| #include
|
| #include “esp_timer.h”
|
| #include “img_converters.h”
|
| #include “Arduino.h”
|
| #include "fb_gfx.h"
|
| #include "soc/soc.h"
|
| #include "soc/rtc_cntl_reg.h"
|
| #include "esp_http_server.h"
|
|
|
| const char* ssid = "输入你的 SSID";
|
| const char* 密码 = "密码";
|
|
|
| #define PART_BOUNDARY "123456789000000000000987654321"
|
| #define CAMERA_MODEL_AI_THINKER
|
|
|
| #如果定义(CAMERA_MODEL_WROVER_KIT)
|
| #define PWDN_GPIO_NUM-1
|
| #define RESET_GPIO_NUM -1
|
| #define XCLK_GPIO_NUM21
|
| #define SIOD_GPIO_NUM26
|
| #define SIOC_GPIO_NUM27
|
|
|
| #define Y9_GPIO_NUM35
|
| #define Y8_GPIO_NUM34
|
| #define Y7_GPIO_NUM39
|
| #define Y6_GPIO_NUM36
|
| #define Y5_GPIO_NUM19
|
| #define Y4_GPIO_NUM18
|
| #define Y3_GPIO_NUM 5
|
| #define Y2_GPIO_NUM 4
|
| #define VSYNC_GPIO_NUM 25
|
| #define HREF_GPIO_NUM23
|
| #define PCLK_GPIO_NUM22
|
|
|
| #elif 定义(CAMERA_MODEL_M5STACK_PSRAM)
|
| #define PWDN_GPIO_NUM -1
|
| #define RESET_GPIO_NUM15
|
| #define XCLK_GPIO_NUM 27
|
| #define SIOD_GPIO_NUM 25
|
| #define SIOC_GPIO_NUM 23
|
|
|
| #define Y9_GPIO_NUM 19
|
| #define Y8_GPIO_NUM 36
|
| #define Y7_GPIO_NUM 18
|
| #define Y6_GPIO_NUM 39
|
| #define Y5_GPIO_NUM5
|
| #define Y4_GPIO_NUM 34
|
| #define Y3_GPIO_NUM 35
|
| #define Y2_GPIO_NUM 32
|
| #define VSYNC_GPIO_NUM22
|
| #define HREF_GPIO_NUM 26
|
| #define PCLK_GPIO_NUM 21
|
|
|
| #elif 定义(CAMERA_MODEL_M5STACK_WITHOUT_PSRAM)
|
| #define PWDN_GPIO_NUM -1
|
| #define RESET_GPIO_NUM15
|
| #define XCLK_GPIO_NUM 27
|
| #define SIOD_GPIO_NUM 25
|
| #define SIOC_GPIO_NUM 23
|
|
|
| #define Y9_GPIO_NUM 19
|
| #define Y8_GPIO_NUM 36
|
| #define Y7_GPIO_NUM 18
|
| #define Y6_GPIO_NUM 39
|
| #define Y5_GPIO_NUM5
|
| #define Y4_GPIO_NUM 34
|
| #define Y3_GPIO_NUM 35
|
| #define Y2_GPIO_NUM 17
|
| #define VSYNC_GPIO_NUM22
|
| #define HREF_GPIO_NUM 26
|
| #define PCLK_GPIO_NUM 21
|
|
|
| #elif 定义(CAMERA_MODEL_AI_THINKER)
|
| #define PWDN_GPIO_NUM 32
|
| #define RESET_GPIO_NUM-1
|
| #define XCLK_GPIO_NUM0
|
| #define SIOD_GPIO_NUM 26
|
| #define SIOC_GPIO_NUM 27
|
|
|
| #define Y9_GPIO_NUM 35
|
| #define Y8_GPIO_NUM 34
|
| #define Y7_GPIO_NUM 39
|
| #define Y6_GPIO_NUM 36
|
| #define Y5_GPIO_NUM 21
|
| #define Y4_GPIO_NUM 19
|
| #define Y3_GPIO_NUM 18
|
| #define Y2_GPIO_NUM5
|
| #define VSYNC_GPIO_NUM25
|
| #define HREF_GPIO_NUM 23
|
| #define PCLK_GPIO_NUM 22
|
| #别的
|
| #error "未选择相机型号"
|
| #万一
|
|
|
| static const char* _STREAM_CONTENT_TYPE = "multipart/x-mixed-replace;boundary=" PART_BOUNDARY;
|
| static const char* _STREAM_BOUNDARY = "
--" PART_BOUNDARY "
";
|
| static const char* _STREAM_PART = "内容类型:图像/jpeg
内容长度:%u
";
|
|
|
| httpd_handle_t 流_httpd = NULL;
|
|
|
| 静态 esp_err_t 流处理程序(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;
|
| 字符 * part_buf[64];
|
|
|
| res = httpd_resp_set_type(req, _STREAM_CONTENT_TYPE);
|
| 如果(res!= ESP_OK){
|
| 返回资源;
|
| }
|
|
|
| 而(真){
|
| fb = esp_camera_fb_get();
|
| 如果(!fb){
|
| Serial.println("相机拍摄失败");
|
| 水库= ESP_FAIL;
|
| } 别的 {
|
| 如果(FB->宽度> 400){
|
| 如果(FB->格式!= PIXFORMAT_JPEG){
|
| bool jpeg_converted = frame2jpg(fb, 80, &_jpg_buf, &_jpg_buf_len);
|
| esp_camera_fb_return(fb);
|
| FB = NULL;
|
| 如果(!jpeg_converted){
|
| Serial.println("JPEG 压缩失败");
|
| 水库= ESP_FAIL;
|
| }
|
| } 别的 {
|
| _jpg_buf_len = fb->len;
|
| _jpg_buf = fb->buf;
|
| }
|
| }
|
| }
|
| 如果(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);
|
| }
|
| 如果(res == ESP_OK){
|
| res = httpd_resp_send_chunk(req, (const char *)_jpg_buf, _jpg_buf_len);
|
| }
|
| 如果(res == ESP_OK){
|
| res = httpd_resp_send_chunk(req, _STREAM_BOUNDARY, strlen(_STREAM_BOUNDARY));
|
| }
|
| 如果(FB){
|
| esp_camera_fb_return(fb);
|
| FB = NULL;
|
| _jpg_buf = NULL;
|
| } 否则如果(_jpg_buf){
|
| 免费(_jpg_buf);
|
| _jpg_buf = NULL;
|
| }
|
| 如果(res!= ESP_OK){
|
| 休息;
|
| }
|
| //Serial.printf("MJPG: %uB
",(uint32_t)(_jpg_buf_len));
|
| }
|
| 返回资源;
|
| }
|
|
|
| 无效 startCameraServer(){
|
| httpd_config_t 配置 = HTTPD_DEFAULT_CONFIG();
|
| config.server_port = 80;
|
|
|
| httpd_uri_t index_uri = {
|
| .uri = "/",
|
| .method=HTTP_GET,
|
| .handler = stream_handler,
|
| .user_ctx=NULL
|
| };
|
|
|
| //Serial.printf("在端口启动 Web 服务器:'%d'
", config.server_port);
|
| if (httpd_start(&stream_httpd, &config) == ESP_OK) {
|
| httpd_register_uri_handler(stream_httpd, &index_uri);
|
| }
|
| }
|
|
|
| 无效设置(){
|
| WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG,0);//禁用掉电检测器
|
|
|
| 序列号.开始(115200);
|
| Serial.setDebugOutput(false);
|
|
|
| camera_config_t 配置;
|
| 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_sscb_sda = SIOD_GPIO_NUM;
|
| config.pin_sscb_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;
|
|
|
| 如果(psramFound()){
|
| config.frame_size = FRAMESIZE_UXGA;
|
| config.jpeg_quality = 10;
|
| config.fb_count = 2;
|
| } 别的 {
|
| config.frame_size = FRAMESIZE_SVGA;
|
| config.jpeg_quality = 12;
|
| config.fb_count = 1;
|
| }
|
|
|
| // 相机初始化
|
| esp_err_t err = esp_camera_init(&config);
|
| 如果(错误!= ESP_OK){
|
| Serial.printf("相机初始化失败,错误 0x%x", err);
|
| 返回;
|
| }
|
| // 无线网络连接
|
| WiFi.begin(ssid, 密码);
|
| 而(WiFi.status()!= WL_CONNECTED){
|
| 延迟(500);
|
| Serial.print(".");
|
| }
|
| 序列号.println("");
|
| Serial.println("WiFi 连接");
|
|
|
| Serial.print("摄像头流就绪!转到:http://");
|
| Serial.print(WiFi.localIP());
|
|
|
| // 启动流式网络服务器
|
| startCameraServer();
|
| }
|
|
|
| 无效循环(){
|
| 延迟(1);
|
| }
|