1718
|
1 #pragma once
|
|
2
|
|
3 #include <Arduino.h>
|
|
4 #include <WiFi.h>
|
|
5
|
|
6 #include "esp_camera.h"
|
|
7 #include "esp_http_server.h"
|
|
8 #include "esp_timer.h"
|
|
9 #include "esphome.h"
|
|
10 #include "esphome/core/component.h"
|
|
11 #include "esphome/core/log.h"
|
|
12 #include "img_converters.h"
|
|
13
|
|
14 namespace esphome {
|
|
15 // #elif defined(CAMERA_MODEL_AI_THINKER)
|
|
16 #define PWDN_GPIO_NUM 32
|
|
17 #define RESET_GPIO_NUM -1
|
|
18 #define XCLK_GPIO_NUM 0
|
|
19 #define SIOD_GPIO_NUM 26
|
|
20 #define SIOC_GPIO_NUM 27
|
|
21
|
|
22 #define Y9_GPIO_NUM 35
|
|
23 #define Y8_GPIO_NUM 34
|
|
24 #define Y7_GPIO_NUM 39
|
|
25 #define Y6_GPIO_NUM 36
|
|
26 #define Y5_GPIO_NUM 21
|
|
27 #define Y4_GPIO_NUM 19
|
|
28 #define Y3_GPIO_NUM 18
|
|
29 #define Y2_GPIO_NUM 5
|
|
30 #define VSYNC_GPIO_NUM 25
|
|
31 #define HREF_GPIO_NUM 23
|
|
32 #define PCLK_GPIO_NUM 22
|
|
33
|
|
34 static const char *TAG = "cam";
|
|
35
|
|
36 typedef struct {
|
|
37 httpd_req_t *req;
|
|
38 size_t len;
|
|
39 } jpg_chunking_t;
|
|
40
|
|
41 class CamComponent : public Component {
|
|
42 public:
|
|
43 float get_setup_priority() const override {
|
|
44 return esphome::setup_priority::AFTER_CONNECTION;
|
|
45 }
|
|
46
|
|
47 void setup() override {
|
|
48 ESP_LOGD(TAG, "setup");
|
|
49 camera_config_t config;
|
|
50 config.ledc_channel = LEDC_CHANNEL_0;
|
|
51 config.ledc_timer = LEDC_TIMER_0;
|
|
52 config.pin_d0 = Y2_GPIO_NUM;
|
|
53 config.pin_d1 = Y3_GPIO_NUM;
|
|
54 config.pin_d2 = Y4_GPIO_NUM;
|
|
55 config.pin_d3 = Y5_GPIO_NUM;
|
|
56 config.pin_d4 = Y6_GPIO_NUM;
|
|
57 config.pin_d5 = Y7_GPIO_NUM;
|
|
58 config.pin_d6 = Y8_GPIO_NUM;
|
|
59 config.pin_d7 = Y9_GPIO_NUM;
|
|
60 config.pin_xclk = XCLK_GPIO_NUM;
|
|
61 config.pin_pclk = PCLK_GPIO_NUM;
|
|
62 config.pin_vsync = VSYNC_GPIO_NUM;
|
|
63 config.pin_href = HREF_GPIO_NUM;
|
|
64 config.pin_sscb_sda = SIOD_GPIO_NUM;
|
|
65 config.pin_sscb_scl = SIOC_GPIO_NUM;
|
|
66 config.pin_pwdn = PWDN_GPIO_NUM;
|
|
67 config.pin_reset = RESET_GPIO_NUM;
|
|
68 config.xclk_freq_hz = 20000000;
|
|
69 config.frame_size = FRAMESIZE_QVGA;
|
|
70 config.pixel_format = PIXFORMAT_JPEG; // for streaming
|
|
71 // config.pixel_format = PIXFORMAT_RGB565; // for face detection/recognition
|
|
72 // config.grab_mode = CAMERA_GRAB_WHEN_EMPTY;
|
|
73 // config.fb_location = CAMERA_FB_IN_PSRAM;
|
|
74 config.jpeg_quality = 12;
|
|
75 config.fb_count = 1;
|
|
76
|
|
77 if (psramFound()) {
|
|
78 config.jpeg_quality = 10;
|
|
79 config.fb_count = 2;
|
|
80 // config.grab_mode = CAMERA_GRAB_LATEST;
|
|
81 }
|
|
82
|
|
83 ESP_LOGD(TAG, "camera init");
|
|
84 esp_err_t err = esp_camera_init(&config);
|
|
85 if (err != ESP_OK) {
|
|
86 Serial.printf("Camera init failed with error 0x%x", err);
|
|
87 return;
|
|
88 }
|
|
89
|
|
90 startCameraServer();
|
|
91 }
|
|
92
|
|
93 static size_t jpg_encode_stream(void *arg, size_t index, const void *data,
|
|
94 size_t len) {
|
|
95 jpg_chunking_t *j = (jpg_chunking_t *)arg;
|
|
96 if (!index) {
|
|
97 j->len = 0;
|
|
98 }
|
|
99 if (httpd_resp_send_chunk(j->req, (const char *)data, len) != ESP_OK) {
|
|
100 return 0;
|
|
101 }
|
|
102 j->len += len;
|
|
103 return len;
|
|
104 }
|
|
105
|
|
106 static esp_err_t capture_handler(httpd_req_t *req) {
|
|
107 camera_fb_t *fb = NULL;
|
|
108 esp_err_t res = ESP_OK;
|
|
109 int64_t fr_start = esp_timer_get_time();
|
|
110
|
|
111 fb = esp_camera_fb_get();
|
|
112
|
|
113 if (!fb) {
|
|
114 ESP_LOGE(TAG, "Camera capture failed");
|
|
115 httpd_resp_send_500(req);
|
|
116 return ESP_FAIL;
|
|
117 }
|
|
118
|
|
119 httpd_resp_set_type(req, "image/jpeg");
|
|
120 httpd_resp_set_hdr(req, "Content-Disposition",
|
|
121 "inline; filename=capture.jpg");
|
|
122 httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
|
|
123
|
|
124 char ts[32];
|
|
125 snprintf(ts, 32, "%ld.%06ld", fb->timestamp.tv_sec, fb->timestamp.tv_usec);
|
|
126 httpd_resp_set_hdr(req, "X-Timestamp", (const char *)ts);
|
|
127
|
|
128 size_t fb_len = 0;
|
|
129 if (fb->format == PIXFORMAT_JPEG) {
|
|
130 fb_len = fb->len;
|
|
131 res = httpd_resp_send(req, (const char *)fb->buf, fb->len);
|
|
132 } else {
|
|
133 jpg_chunking_t jchunk = {req, 0};
|
|
134 res =
|
|
135 frame2jpg_cb(fb, 80, jpg_encode_stream, &jchunk) ? ESP_OK : ESP_FAIL;
|
|
136 httpd_resp_send_chunk(req, NULL, 0);
|
|
137 fb_len = jchunk.len;
|
|
138 }
|
|
139 esp_camera_fb_return(fb);
|
|
140 int64_t fr_end = esp_timer_get_time();
|
|
141 ESP_LOGI(TAG, "JPG: %uB %ums", (uint32_t)(fb_len),
|
|
142 (uint32_t)((fr_end - fr_start) / 1000));
|
|
143 return res;
|
|
144 }
|
|
145
|
|
146 void startCameraServer() {
|
|
147 ESP_LOGD(TAG, "startCameraServer");
|
|
148 httpd_config_t config = HTTPD_DEFAULT_CONFIG();
|
|
149 config.server_port = 8000;
|
|
150 config.max_uri_handlers = 16;
|
|
151
|
|
152 httpd_uri_t capture_uri = {.uri = "/capture",
|
|
153 .method = HTTP_GET,
|
|
154 .handler = capture_handler,
|
|
155 .user_ctx = NULL};
|
|
156
|
|
157 // httpd_uri_t stream_uri = {
|
|
158 // .uri = "/stream",
|
|
159 // .method = HTTP_GET,
|
|
160 // .handler = stream_handler,
|
|
161 // .user_ctx = NULL
|
|
162 // };
|
|
163
|
|
164 // ra_filter_init(&ra_filter, 20);
|
|
165 ESP_LOGCONFIG(TAG, "startCameraServer2");
|
|
166
|
|
167 ESP_LOGI(TAG, "Starting web server on port: '%d'", config.server_port);
|
|
168 httpd_handle_t camera_httpd = NULL;
|
|
169 if (httpd_start(&camera_httpd, &config) == ESP_OK) {
|
|
170 // httpd_register_uri_handler(camera_httpd, &index_uri);
|
|
171 // httpd_register_uri_handler(camera_httpd, &cmd_uri);
|
|
172 // httpd_register_uri_handler(camera_httpd, &status_uri);
|
|
173 httpd_register_uri_handler(camera_httpd, &capture_uri);
|
|
174 // httpd_register_uri_handler(camera_httpd, &bmp_uri);
|
|
175
|
|
176 // httpd_register_uri_handler(camera_httpd, &xclk_uri);
|
|
177 // httpd_register_uri_handler(camera_httpd, ®_uri);
|
|
178 // httpd_register_uri_handler(camera_httpd, &greg_uri);
|
|
179 // httpd_register_uri_handler(camera_httpd, &pll_uri);
|
|
180 // httpd_register_uri_handler(camera_httpd, &win_uri);
|
|
181 }
|
|
182
|
|
183 // config.server_port += 1;
|
|
184 // config.ctrl_port += 1;
|
|
185 // ESP_LOGI(TAG, "Starting stream server on port: '%d'",
|
|
186 // config.server_port); if (httpd_start(&stream_httpd, &config) == ESP_OK)
|
|
187 // {
|
|
188 // httpd_register_uri_handler(stream_httpd, &stream_uri);
|
|
189 // }
|
|
190 }
|
|
191
|
|
192 void loop() override {}
|
|
193 };
|
|
194 } // namespace esphome |