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
|
1740
|
36 #define PART_BOUNDARY "123456789000000000000987654321"
|
|
37 static const char *_STREAM_CONTENT_TYPE =
|
|
38 "multipart/x-mixed-replace;boundary=" PART_BOUNDARY;
|
|
39 static const char *_STREAM_BOUNDARY = "\r\n--" PART_BOUNDARY "\r\n";
|
|
40 static const char *_STREAM_PART =
|
|
41 "Content-Type: image/jpeg\r\nContent-Length: %u\r\nX-Timestamp: "
|
|
42 "%d.%06d\r\n\r\n";
|
|
43
|
|
44 httpd_handle_t camera_httpd = NULL;
|
|
45 httpd_handle_t stream_httpd = NULL;
|
|
46
|
1718
|
47 typedef struct {
|
|
48 httpd_req_t *req;
|
|
49 size_t len;
|
|
50 } jpg_chunking_t;
|
|
51
|
|
52 class CamComponent : public Component {
|
|
53 public:
|
|
54 float get_setup_priority() const override {
|
|
55 return esphome::setup_priority::AFTER_CONNECTION;
|
|
56 }
|
|
57
|
|
58 void setup() override {
|
|
59 ESP_LOGD(TAG, "setup");
|
|
60 camera_config_t config;
|
|
61 config.ledc_channel = LEDC_CHANNEL_0;
|
|
62 config.ledc_timer = LEDC_TIMER_0;
|
|
63 config.pin_d0 = Y2_GPIO_NUM;
|
|
64 config.pin_d1 = Y3_GPIO_NUM;
|
|
65 config.pin_d2 = Y4_GPIO_NUM;
|
|
66 config.pin_d3 = Y5_GPIO_NUM;
|
|
67 config.pin_d4 = Y6_GPIO_NUM;
|
|
68 config.pin_d5 = Y7_GPIO_NUM;
|
|
69 config.pin_d6 = Y8_GPIO_NUM;
|
|
70 config.pin_d7 = Y9_GPIO_NUM;
|
|
71 config.pin_xclk = XCLK_GPIO_NUM;
|
|
72 config.pin_pclk = PCLK_GPIO_NUM;
|
|
73 config.pin_vsync = VSYNC_GPIO_NUM;
|
|
74 config.pin_href = HREF_GPIO_NUM;
|
|
75 config.pin_sscb_sda = SIOD_GPIO_NUM;
|
|
76 config.pin_sscb_scl = SIOC_GPIO_NUM;
|
|
77 config.pin_pwdn = PWDN_GPIO_NUM;
|
|
78 config.pin_reset = RESET_GPIO_NUM;
|
|
79 config.xclk_freq_hz = 20000000;
|
1740
|
80 config.frame_size = FRAMESIZE_SVGA;
|
1718
|
81 config.pixel_format = PIXFORMAT_JPEG; // for streaming
|
|
82 // config.pixel_format = PIXFORMAT_RGB565; // for face detection/recognition
|
|
83 // config.grab_mode = CAMERA_GRAB_WHEN_EMPTY;
|
|
84 // config.fb_location = CAMERA_FB_IN_PSRAM;
|
|
85 config.jpeg_quality = 12;
|
|
86 config.fb_count = 1;
|
|
87
|
|
88 if (psramFound()) {
|
|
89 config.jpeg_quality = 10;
|
|
90 config.fb_count = 2;
|
|
91 // config.grab_mode = CAMERA_GRAB_LATEST;
|
|
92 }
|
|
93
|
|
94 ESP_LOGD(TAG, "camera init");
|
|
95 esp_err_t err = esp_camera_init(&config);
|
|
96 if (err != ESP_OK) {
|
|
97 Serial.printf("Camera init failed with error 0x%x", err);
|
1740
|
98 mark_failed();
|
1718
|
99 return;
|
|
100 }
|
|
101
|
|
102 startCameraServer();
|
|
103 }
|
|
104
|
|
105 static size_t jpg_encode_stream(void *arg, size_t index, const void *data,
|
|
106 size_t len) {
|
|
107 jpg_chunking_t *j = (jpg_chunking_t *)arg;
|
|
108 if (!index) {
|
|
109 j->len = 0;
|
|
110 }
|
|
111 if (httpd_resp_send_chunk(j->req, (const char *)data, len) != ESP_OK) {
|
|
112 return 0;
|
|
113 }
|
|
114 j->len += len;
|
|
115 return len;
|
|
116 }
|
|
117
|
|
118 static esp_err_t capture_handler(httpd_req_t *req) {
|
|
119 camera_fb_t *fb = NULL;
|
|
120 esp_err_t res = ESP_OK;
|
|
121 int64_t fr_start = esp_timer_get_time();
|
|
122
|
|
123 fb = esp_camera_fb_get();
|
|
124
|
|
125 if (!fb) {
|
|
126 ESP_LOGE(TAG, "Camera capture failed");
|
|
127 httpd_resp_send_500(req);
|
|
128 return ESP_FAIL;
|
|
129 }
|
|
130
|
|
131 httpd_resp_set_type(req, "image/jpeg");
|
|
132 httpd_resp_set_hdr(req, "Content-Disposition",
|
|
133 "inline; filename=capture.jpg");
|
|
134 httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
|
|
135
|
|
136 char ts[32];
|
|
137 snprintf(ts, 32, "%ld.%06ld", fb->timestamp.tv_sec, fb->timestamp.tv_usec);
|
|
138 httpd_resp_set_hdr(req, "X-Timestamp", (const char *)ts);
|
|
139
|
|
140 size_t fb_len = 0;
|
|
141 if (fb->format == PIXFORMAT_JPEG) {
|
|
142 fb_len = fb->len;
|
|
143 res = httpd_resp_send(req, (const char *)fb->buf, fb->len);
|
|
144 } else {
|
|
145 jpg_chunking_t jchunk = {req, 0};
|
|
146 res =
|
|
147 frame2jpg_cb(fb, 80, jpg_encode_stream, &jchunk) ? ESP_OK : ESP_FAIL;
|
|
148 httpd_resp_send_chunk(req, NULL, 0);
|
|
149 fb_len = jchunk.len;
|
|
150 }
|
|
151 esp_camera_fb_return(fb);
|
|
152 int64_t fr_end = esp_timer_get_time();
|
|
153 ESP_LOGI(TAG, "JPG: %uB %ums", (uint32_t)(fb_len),
|
|
154 (uint32_t)((fr_end - fr_start) / 1000));
|
|
155 return res;
|
|
156 }
|
|
157
|
1740
|
158 static esp_err_t stream_handler(httpd_req_t *req) {
|
|
159 camera_fb_t *fb = NULL;
|
|
160 struct timeval _timestamp;
|
|
161 esp_err_t res = ESP_OK;
|
|
162 size_t _jpg_buf_len = 0;
|
|
163 uint8_t *_jpg_buf = NULL;
|
|
164 char *part_buf[128];
|
|
165
|
|
166 static int64_t last_frame = 0;
|
|
167 if (!last_frame) {
|
|
168 last_frame = esp_timer_get_time();
|
|
169 }
|
|
170
|
|
171 res = httpd_resp_set_type(req, _STREAM_CONTENT_TYPE);
|
|
172 if (res != ESP_OK) {
|
|
173 return res;
|
|
174 }
|
|
175
|
|
176 while (true) {
|
|
177 fb = esp_camera_fb_get();
|
|
178 if (!fb) {
|
|
179 ESP_LOGE(TAG, "Camera capture failed");
|
|
180 res = ESP_FAIL;
|
|
181 } else {
|
|
182 _timestamp.tv_sec = fb->timestamp.tv_sec;
|
|
183 _timestamp.tv_usec = fb->timestamp.tv_usec;
|
|
184 if (fb->format != PIXFORMAT_JPEG) {
|
|
185 ESP_LOGI(TAG, "format was %d; sw convert to jpeg", fb->format);
|
|
186 bool jpeg_converted = frame2jpg(fb, 80, &_jpg_buf, &_jpg_buf_len);
|
|
187 esp_camera_fb_return(fb);
|
|
188 fb = NULL;
|
|
189 if (!jpeg_converted) {
|
|
190 ESP_LOGE(TAG, "JPEG compression failed");
|
|
191 res = ESP_FAIL;
|
|
192 }
|
|
193 } else {
|
|
194 _jpg_buf_len = fb->len;
|
|
195 _jpg_buf = fb->buf;
|
|
196 }
|
|
197 }
|
|
198 if (res == ESP_OK) {
|
|
199 res = httpd_resp_send_chunk(req, _STREAM_BOUNDARY,
|
|
200 strlen(_STREAM_BOUNDARY));
|
|
201 }
|
|
202 if (res == ESP_OK) {
|
|
203 size_t hlen =
|
|
204 snprintf((char *)part_buf, 128, _STREAM_PART, _jpg_buf_len,
|
|
205 _timestamp.tv_sec, _timestamp.tv_usec);
|
|
206 res = httpd_resp_send_chunk(req, (const char *)part_buf, hlen);
|
|
207 }
|
|
208 if (res == ESP_OK) {
|
|
209 res = httpd_resp_send_chunk(req, (const char *)_jpg_buf, _jpg_buf_len);
|
|
210 }
|
|
211 if (fb) {
|
|
212 esp_camera_fb_return(fb);
|
|
213 fb = NULL;
|
|
214 _jpg_buf = NULL;
|
|
215 } else if (_jpg_buf) {
|
|
216 free(_jpg_buf);
|
|
217 _jpg_buf = NULL;
|
|
218 }
|
|
219 if (res != ESP_OK) {
|
|
220 ESP_LOGE(TAG, "send frame failed failed");
|
|
221 break;
|
|
222 }
|
|
223 int64_t fr_end = esp_timer_get_time();
|
|
224
|
|
225 int64_t frame_time = fr_end - last_frame;
|
|
226 frame_time /= 1000;
|
|
227 uint32_t avg_frame_time = -1;
|
|
228 // ESP_LOGI(TAG, "MJPG: %uB %ums (%.1ffps), AVG: %ums (%.1ffps)"
|
|
229 // ,
|
|
230 // (uint32_t)(_jpg_buf_len), (uint32_t)frame_time,
|
|
231 // 1000.0 / (uint32_t)frame_time, avg_frame_time,
|
|
232 // 1000.0 / avg_frame_time);
|
|
233 }
|
|
234
|
|
235 return res;
|
|
236 }
|
|
237
|
1718
|
238 void startCameraServer() {
|
|
239 ESP_LOGD(TAG, "startCameraServer");
|
|
240 httpd_config_t config = HTTPD_DEFAULT_CONFIG();
|
1740
|
241 config.server_port = 80;
|
1718
|
242 config.max_uri_handlers = 16;
|
|
243
|
|
244 httpd_uri_t capture_uri = {.uri = "/capture",
|
|
245 .method = HTTP_GET,
|
|
246 .handler = capture_handler,
|
|
247 .user_ctx = NULL};
|
|
248
|
1740
|
249 httpd_uri_t stream_uri = {.uri = "/stream",
|
|
250 .method = HTTP_GET,
|
|
251 .handler = stream_handler,
|
|
252 .user_ctx = NULL};
|
1718
|
253
|
|
254 // ra_filter_init(&ra_filter, 20);
|
|
255 ESP_LOGCONFIG(TAG, "startCameraServer2");
|
|
256
|
|
257 ESP_LOGI(TAG, "Starting web server on port: '%d'", config.server_port);
|
1740
|
258
|
1718
|
259 if (httpd_start(&camera_httpd, &config) == ESP_OK) {
|
|
260 // httpd_register_uri_handler(camera_httpd, &index_uri);
|
|
261 // httpd_register_uri_handler(camera_httpd, &cmd_uri);
|
|
262 // httpd_register_uri_handler(camera_httpd, &status_uri);
|
|
263 httpd_register_uri_handler(camera_httpd, &capture_uri);
|
|
264 // httpd_register_uri_handler(camera_httpd, &bmp_uri);
|
|
265
|
|
266 // httpd_register_uri_handler(camera_httpd, &xclk_uri);
|
|
267 // httpd_register_uri_handler(camera_httpd, ®_uri);
|
|
268 // httpd_register_uri_handler(camera_httpd, &greg_uri);
|
|
269 // httpd_register_uri_handler(camera_httpd, &pll_uri);
|
|
270 // httpd_register_uri_handler(camera_httpd, &win_uri);
|
|
271 }
|
|
272
|
1740
|
273 config.server_port += 1;
|
|
274 config.ctrl_port += 1;
|
|
275 ESP_LOGI(TAG, "Starting stream server on port: '%d'", config.server_port);
|
|
276 if (httpd_start(&stream_httpd, &config) == ESP_OK) {
|
|
277 httpd_register_uri_handler(stream_httpd, &stream_uri);
|
|
278 }
|
1718
|
279 }
|
|
280
|
|
281 void loop() override {}
|
|
282 };
|
|
283 } // namespace esphome |