This repository has been archived by the owner on Jun 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 243
/
app_main.c
341 lines (296 loc) · 11.2 KB
/
app_main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
// Copyright 2015-2017 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#include "freertos/event_groups.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "esp_event_loop.h"
#include "esp_log.h"
#include "esp_err.h"
#include "nvs_flash.h"
#include "driver/gpio.h"
#include "camera.h"
#include "bitmap.h"
#include "http_server.h"
static void handle_grayscale_pgm(http_context_t http_ctx, void* ctx);
static void handle_rgb_bmp(http_context_t http_ctx, void* ctx);
static void handle_rgb_bmp_stream(http_context_t http_ctx, void* ctx);
static void handle_jpg(http_context_t http_ctx, void* ctx);
static void handle_jpg_stream(http_context_t http_ctx, void* ctx);
static esp_err_t event_handler(void *ctx, system_event_t *event);
static void initialise_wifi(void);
static const char* TAG = "camera_demo";
static const char* STREAM_CONTENT_TYPE =
"multipart/x-mixed-replace; boundary=123456789000000000000987654321";
static const char* STREAM_BOUNDARY = "--123456789000000000000987654321";
static EventGroupHandle_t s_wifi_event_group;
const int CONNECTED_BIT = BIT0;
static ip4_addr_t s_ip_addr;
static camera_pixelformat_t s_pixel_format;
#define CAMERA_PIXEL_FORMAT CAMERA_PF_GRAYSCALE
#define CAMERA_FRAME_SIZE CAMERA_FS_QVGA
void app_main()
{
esp_log_level_set("wifi", ESP_LOG_WARN);
esp_log_level_set("gpio", ESP_LOG_WARN);
esp_err_t err = nvs_flash_init();
if (err != ESP_OK) {
ESP_ERROR_CHECK( nvs_flash_erase() );
ESP_ERROR_CHECK( nvs_flash_init() );
}
ESP_ERROR_CHECK(gpio_install_isr_service(0));
camera_config_t camera_config = {
.ledc_channel = LEDC_CHANNEL_0,
.ledc_timer = LEDC_TIMER_0,
.pin_d0 = CONFIG_D0,
.pin_d1 = CONFIG_D1,
.pin_d2 = CONFIG_D2,
.pin_d3 = CONFIG_D3,
.pin_d4 = CONFIG_D4,
.pin_d5 = CONFIG_D5,
.pin_d6 = CONFIG_D6,
.pin_d7 = CONFIG_D7,
.pin_xclk = CONFIG_XCLK,
.pin_pclk = CONFIG_PCLK,
.pin_vsync = CONFIG_VSYNC,
.pin_href = CONFIG_HREF,
.pin_sscb_sda = CONFIG_SDA,
.pin_sscb_scl = CONFIG_SCL,
.pin_reset = CONFIG_RESET,
.xclk_freq_hz = CONFIG_XCLK_FREQ,
};
camera_model_t camera_model;
err = camera_probe(&camera_config, &camera_model);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Camera probe failed with error 0x%x", err);
return;
}
if (camera_model == CAMERA_OV7725) {
s_pixel_format = CAMERA_PIXEL_FORMAT;
camera_config.frame_size = CAMERA_FRAME_SIZE;
ESP_LOGI(TAG, "Detected OV7725 camera, using %s bitmap format",
CAMERA_PIXEL_FORMAT == CAMERA_PF_GRAYSCALE ?
"grayscale" : "RGB565");
} else if (camera_model == CAMERA_OV2640) {
ESP_LOGI(TAG, "Detected OV2640 camera, using JPEG format");
s_pixel_format = CAMERA_PF_JPEG;
camera_config.frame_size = CAMERA_FRAME_SIZE;
camera_config.jpeg_quality = 15;
} else {
ESP_LOGE(TAG, "Camera not supported");
return;
}
camera_config.pixel_format = s_pixel_format;
err = camera_init(&camera_config);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Camera init failed with error 0x%x", err);
return;
}
initialise_wifi();
http_server_t server;
http_server_options_t http_options = HTTP_SERVER_OPTIONS_DEFAULT();
ESP_ERROR_CHECK( http_server_start(&http_options, &server) );
if (s_pixel_format == CAMERA_PF_GRAYSCALE) {
ESP_ERROR_CHECK( http_register_handler(server, "/pgm", HTTP_GET, HTTP_HANDLE_RESPONSE, &handle_grayscale_pgm, NULL) );
ESP_LOGI(TAG, "Open http://" IPSTR "/pgm for a single image/x-portable-graymap image", IP2STR(&s_ip_addr));
}
if (s_pixel_format == CAMERA_PF_RGB565) {
ESP_ERROR_CHECK( http_register_handler(server, "/bmp", HTTP_GET, HTTP_HANDLE_RESPONSE, &handle_rgb_bmp, NULL) );
ESP_LOGI(TAG, "Open http://" IPSTR "/bmp for single image/bitmap image", IP2STR(&s_ip_addr));
ESP_ERROR_CHECK( http_register_handler(server, "/bmp_stream", HTTP_GET, HTTP_HANDLE_RESPONSE, &handle_rgb_bmp_stream, NULL) );
ESP_LOGI(TAG, "Open http://" IPSTR "/bmp_stream for multipart/x-mixed-replace stream of bitmaps", IP2STR(&s_ip_addr));
}
if (s_pixel_format == CAMERA_PF_JPEG) {
ESP_ERROR_CHECK( http_register_handler(server, "/jpg", HTTP_GET, HTTP_HANDLE_RESPONSE, &handle_jpg, NULL) );
ESP_LOGI(TAG, "Open http://" IPSTR "/jpg for single image/jpg image", IP2STR(&s_ip_addr));
ESP_ERROR_CHECK( http_register_handler(server, "/jpg_stream", HTTP_GET, HTTP_HANDLE_RESPONSE, &handle_jpg_stream, NULL) );
ESP_LOGI(TAG, "Open http://" IPSTR "/jpg_stream for multipart/x-mixed-replace stream of JPEGs", IP2STR(&s_ip_addr));
}
ESP_LOGI(TAG, "Free heap: %u", xPortGetFreeHeapSize());
ESP_LOGI(TAG, "Camera demo ready");
}
static esp_err_t write_frame(http_context_t http_ctx)
{
http_buffer_t fb_data = {
.data = camera_get_fb(),
.size = camera_get_data_size(),
.data_is_persistent = true
};
return http_response_write(http_ctx, &fb_data);
}
static void handle_grayscale_pgm(http_context_t http_ctx, void* ctx)
{
esp_err_t err = camera_run();
if (err != ESP_OK) {
ESP_LOGD(TAG, "Camera capture failed with error = %d", err);
return;
}
char* pgm_header_str;
asprintf(&pgm_header_str, "P5 %d %d %d\n",
camera_get_fb_width(), camera_get_fb_height(), 255);
if (pgm_header_str == NULL) {
return;
}
size_t response_size = strlen(pgm_header_str) + camera_get_data_size();
http_response_begin(http_ctx, 200, "image/x-portable-graymap", response_size);
http_response_set_header(http_ctx, "Content-disposition", "inline; filename=capture.pgm");
http_buffer_t pgm_header = { .data = pgm_header_str };
http_response_write(http_ctx, &pgm_header);
free(pgm_header_str);
write_frame(http_ctx);
http_response_end(http_ctx);
}
static void handle_rgb_bmp(http_context_t http_ctx, void* ctx)
{
esp_err_t err = camera_run();
if (err != ESP_OK) {
ESP_LOGD(TAG, "Camera capture failed with error = %d", err);
return;
}
bitmap_header_t* header = bmp_create_header(camera_get_fb_width(), camera_get_fb_height());
if (header == NULL) {
return;
}
http_response_begin(http_ctx, 200, "image/bmp", sizeof(*header) + camera_get_data_size());
http_buffer_t bmp_header = {
.data = header,
.size = sizeof(*header)
};
http_response_set_header(http_ctx, "Content-disposition", "inline; filename=capture.bmp");
http_response_write(http_ctx, &bmp_header);
free(header);
write_frame(http_ctx);
http_response_end(http_ctx);
}
static void handle_jpg(http_context_t http_ctx, void* ctx)
{
esp_err_t err = camera_run();
if (err != ESP_OK) {
ESP_LOGD(TAG, "Camera capture failed with error = %d", err);
return;
}
http_response_begin(http_ctx, 200, "image/jpeg", camera_get_data_size());
http_response_set_header(http_ctx, "Content-disposition", "inline; filename=capture.jpg");
write_frame(http_ctx);
http_response_end(http_ctx);
}
static void handle_rgb_bmp_stream(http_context_t http_ctx, void* ctx)
{
http_response_begin(http_ctx, 200, STREAM_CONTENT_TYPE, HTTP_RESPONSE_SIZE_UNKNOWN);
bitmap_header_t* header = bmp_create_header(camera_get_fb_width(), camera_get_fb_height());
if (header == NULL) {
return;
}
http_buffer_t bmp_header = {
.data = header,
.size = sizeof(*header)
};
while (true) {
esp_err_t err = camera_run();
if (err != ESP_OK) {
ESP_LOGD(TAG, "Camera capture failed with error = %d", err);
return;
}
err = http_response_begin_multipart(http_ctx, "image/bitmap",
camera_get_data_size() + sizeof(*header));
if (err != ESP_OK) {
break;
}
err = http_response_write(http_ctx, &bmp_header);
if (err != ESP_OK) {
break;
}
err = write_frame(http_ctx);
if (err != ESP_OK) {
break;
}
err = http_response_end_multipart(http_ctx, STREAM_BOUNDARY);
if (err != ESP_OK) {
break;
}
}
free(header);
http_response_end(http_ctx);
}
static void handle_jpg_stream(http_context_t http_ctx, void* ctx)
{
http_response_begin(http_ctx, 200, STREAM_CONTENT_TYPE, HTTP_RESPONSE_SIZE_UNKNOWN);
while (true) {
esp_err_t err = camera_run();
if (err != ESP_OK) {
ESP_LOGD(TAG, "Camera capture failed with error = %d", err);
return;
}
err = http_response_begin_multipart(http_ctx, "image/jpg",
camera_get_data_size());
if (err != ESP_OK) {
break;
}
err = write_frame(http_ctx);
if (err != ESP_OK) {
break;
}
err = http_response_end_multipart(http_ctx, STREAM_BOUNDARY);
if (err != ESP_OK) {
break;
}
}
http_response_end(http_ctx);
}
static esp_err_t event_handler(void *ctx, system_event_t *event)
{
switch (event->event_id) {
case SYSTEM_EVENT_STA_START:
esp_wifi_connect();
break;
case SYSTEM_EVENT_STA_GOT_IP:
xEventGroupSetBits(s_wifi_event_group, CONNECTED_BIT);
s_ip_addr = event->event_info.got_ip.ip_info.ip;
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
esp_wifi_connect();
xEventGroupClearBits(s_wifi_event_group, CONNECTED_BIT);
break;
default:
break;
}
return ESP_OK;
}
static void initialise_wifi(void)
{
tcpip_adapter_init();
s_wifi_event_group = xEventGroupCreate();
ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) );
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
wifi_config_t wifi_config = {
.sta = {
.ssid = CONFIG_WIFI_SSID,
.password = CONFIG_WIFI_PASSWORD,
},
};
ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
ESP_ERROR_CHECK( esp_wifi_set_config(WIFI_IF_STA, &wifi_config) );
ESP_ERROR_CHECK( esp_wifi_start() );
ESP_ERROR_CHECK( esp_wifi_set_ps(WIFI_PS_NONE) );
ESP_LOGI(TAG, "Connecting to \"%s\"", wifi_config.sta.ssid);
xEventGroupWaitBits(s_wifi_event_group, CONNECTED_BIT, false, true, portMAX_DELAY);
ESP_LOGI(TAG, "Connected");
}