annotate espNode/component/cam.h @ 1754:92999dfbf321 default tip

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