forked from igrr/esp32-cam-demo
-
Notifications
You must be signed in to change notification settings - Fork 4
/
app_main.c
executable file
·330 lines (300 loc) · 11.8 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
// Copyright 2015-2016 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 "driver/gpio.h"
#include "esp_err.h"
#include "esp_log.h"
#include "camera.h"
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "esp_event_loop.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "driver/gpio.h"
#include "lwip/sys.h"
#include "lwip/netdb.h"
#include "lwip/api.h"
//-----------------------------------------------------------------------
///#include <stdbool.h>
///#include <nvs.h>
#include "driver/i2c.h"
//#include "esp_wifi.h"
#include "xi2c.h"
#include "fonts.h"
#include "ssd1306.h"
#include "nvs_flash.h"
//#define BLINK_GPIO 4
#define I2C_EXAMPLE_MASTER_SCL_IO 14 /*!< gpio number for I2C master clock */////////////
#define I2C_EXAMPLE_MASTER_SDA_IO 13 /*!< gpio number for I2C master data *//////////////
#define I2C_EXAMPLE_MASTER_NUM I2C_NUM_1 /*!< I2C port number for master dev */
#define I2C_EXAMPLE_MASTER_TX_BUF_DISABLE 0 /*!< I2C master do not need buffer */
#define I2C_EXAMPLE_MASTER_RX_BUF_DISABLE 0 /*!< I2C master do not need buffer */
#define I2C_EXAMPLE_MASTER_FREQ_HZ 100000 /*!< I2C master clock frequency */
/////--------------------------------------------------------------------
static const char* TAG = "camera_demo";
int state = 0;
const static char http_hdr[] = "HTTP/1.1 200 OK\r\n";
const static char http_stream_hdr[] =
"Content-type: multipart/x-mixed-replace; boundary=123456789000000000000987654321\r\n\r\n";
const static char http_jpg_hdr[] =
"Content-type: image/jpg\r\n\r\n";
const static char http_pgm_hdr[] =
"Content-type: image/x-portable-graymap\r\n\r\n";
const static char http_stream_boundary[] = "--123456789000000000000987654321\r\n";
static EventGroupHandle_t wifi_event_group;
const int CONNECTED_BIT = BIT0;
static ip4_addr_t s_ip_addr;
static camera_pixelformat_t s_pixel_format;
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(wifi_event_group, CONNECTED_BIT);
s_ip_addr = event->event_info.got_ip.ip_info.ip;
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
/* This is a workaround as ESP32 WiFi libs don't currently
auto-reassociate. */
esp_wifi_connect();
xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
break;
default:
break;
}
return ESP_OK;
}
static void initialise_wifi(void)
{
tcpip_adapter_init();
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(wifi_event_group, CONNECTED_BIT, false, true, portMAX_DELAY);
ESP_LOGI(TAG, "Connected");
}
static void http_server_netconn_serve(struct netconn *conn)
{
struct netbuf *inbuf;
char *buf;
u16_t buflen;
err_t err;
/* Read the data from the port, blocking if nothing yet there.
We assume the request (the part we care about) is in one netbuf */
err = netconn_recv(conn, &inbuf);
if (err == ERR_OK) {
netbuf_data(inbuf, (void**) &buf, &buflen);
/* Is this an HTTP GET command? (only check the first 5 chars, since
there are other formats for GET, and we're keeping it very simple )*/
if (buflen >= 5 && buf[0] == 'G' && buf[1] == 'E' && buf[2] == 'T'
&& buf[3] == ' ' && buf[4] == '/') {
/* Send the HTTP header
* subtract 1 from the size, since we dont send the \0 in the string
* NETCONN_NOCOPY: our data is const static, so no need to copy it
*/
netconn_write(conn, http_hdr, sizeof(http_hdr) - 1,
NETCONN_NOCOPY);
//check if a stream is requested.
if (buf[5] == 's') {
//Send mjpeg stream header
err = netconn_write(conn, http_stream_hdr, sizeof(http_stream_hdr) - 1,
NETCONN_NOCOPY);
ESP_LOGD(TAG, "Stream started.");
//Run while everyhting is ok and connection open.
while(err == ERR_OK) {
ESP_LOGD(TAG, "Capture frame");
err = camera_run();
if (err != ESP_OK) {
ESP_LOGD(TAG, "Camera capture failed with error = %d", err);
} else {
ESP_LOGD(TAG, "Done");
//Send jpeg header
err = netconn_write(conn, http_jpg_hdr, sizeof(http_jpg_hdr) - 1,
NETCONN_NOCOPY);
//Send frame
err = netconn_write(conn, camera_get_fb(), camera_get_data_size(),
NETCONN_NOCOPY);
if(err == ERR_OK)
{
//Send boundary to next jpeg
err = netconn_write(conn, http_stream_boundary,
sizeof(http_stream_boundary) -1, NETCONN_NOCOPY);
}
}
}
ESP_LOGD(TAG, "Stream ended.");
} else {
if (s_pixel_format == CAMERA_PF_JPEG) {
netconn_write(conn, http_jpg_hdr, sizeof(http_jpg_hdr) - 1, NETCONN_NOCOPY);
} else if (s_pixel_format == CAMERA_PF_GRAYSCALE) {
netconn_write(conn, http_pgm_hdr, sizeof(http_pgm_hdr) - 1, NETCONN_NOCOPY);
if (memcmp(&buf[5], "pgm", 3) == 0) {
char pgm_header[32];
snprintf(pgm_header, sizeof(pgm_header), "P5 %d %d %d\n", camera_get_fb_width(), camera_get_fb_height(), 255);
netconn_write(conn, pgm_header, strlen(pgm_header), NETCONN_COPY);
}
}
ESP_LOGD(TAG, "Image requested.");
err = camera_run();
if (err != ESP_OK) {
ESP_LOGD(TAG, "Camera capture failed with error = %d", err);
} else {
ESP_LOGD(TAG, "Done");
//Send jpeg
err = netconn_write(conn, camera_get_fb(), camera_get_data_size(),
NETCONN_NOCOPY);
}
}
}
}
/* Close the connection (server closes in HTTP) */
netconn_close(conn);
/* Delete the buffer (netconn_recv gives us ownership,
so we have to make sure to deallocate the buffer) */
netbuf_delete(inbuf);
}
static void http_server(void *pvParameters)
{
struct netconn *conn, *newconn;
err_t err;
conn = netconn_new(NETCONN_TCP);
netconn_bind(conn, NULL, 80);
netconn_listen(conn);
do {
err = netconn_accept(conn, &newconn);
if (err == ERR_OK) {
http_server_netconn_serve(newconn);
netconn_delete(newconn);
}
} while (err == ERR_OK);
netconn_close(conn);
netconn_delete(conn);
}
/**
* @brief i2c master initialization
*/
void i2c_example_master_init()
{
int i2c_master_port = I2C_EXAMPLE_MASTER_NUM;
i2c_config_t conf;
conf.mode = I2C_MODE_MASTER;
conf.sda_io_num = I2C_EXAMPLE_MASTER_SDA_IO;
conf.sda_pullup_en = GPIO_PULLUP_ENABLE;
conf.scl_io_num = I2C_EXAMPLE_MASTER_SCL_IO;
conf.scl_pullup_en = GPIO_PULLUP_ENABLE;
conf.master.clk_speed = I2C_EXAMPLE_MASTER_FREQ_HZ;
i2c_param_config(i2c_master_port, &conf);
i2c_driver_install(i2c_master_port, conf.mode,
I2C_EXAMPLE_MASTER_RX_BUF_DISABLE,
I2C_EXAMPLE_MASTER_TX_BUF_DISABLE, 0);
}
void app_main()
{
esp_log_level_set("wifi", ESP_LOG_WARN);
esp_log_level_set("gpio", ESP_LOG_WARN);
nvs_flash_init();
i2c_example_master_init();
SSD1306_Init();
camera_config_t 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;
esp_err_t err = camera_probe(&config, &camera_model);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Camera probe failed with error 0x%x", err);
return;
}
if (camera_model == CAMERA_OV7725) {
ESP_LOGI(TAG, "Detected OV7725 camera, using grayscale bitmap format");
s_pixel_format = CAMERA_PF_GRAYSCALE;
config.frame_size = CAMERA_FS_QVGA;
} else if (camera_model == CAMERA_OV2640) {
ESP_LOGI(TAG, "Detected OV2640 camera, using JPEG format");
s_pixel_format = CAMERA_PF_JPEG;
config.frame_size = CAMERA_FS_VGA;
config.jpeg_quality = 15;
} else {
ESP_LOGE(TAG, "Camera not supported");
return;
}
config.pixel_format = s_pixel_format;
err = camera_init(&config);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Camera init failed with error 0x%x", err);
return;
}
initialise_wifi();
ESP_LOGI(TAG, "Free heap: %u", xPortGetFreeHeapSize());
ESP_LOGI(TAG, "Camera demo ready");
ESP_LOGI(TAG, "open http://" IPSTR "/get for single frame", IP2STR(&s_ip_addr));
if (s_pixel_format == CAMERA_PF_GRAYSCALE) {
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_JPEG) {
ESP_LOGI(TAG, "open http://" IPSTR "/stream for multipart/x-mixed-replace stream (use with JPEGs)", IP2STR(&s_ip_addr));
}
SSD1306_GotoXY(32,32);
SSD1306_Puts("ESP32 CAM",&Font_7x10, SSD1306_COLOR_WHITE);
SSD1306_GotoXY(32, 43);
if (camera_model == CAMERA_OV2640){
SSD1306_Puts("OV2640", &Font_7x10, SSD1306_COLOR_WHITE);
}else{
SSD1306_Puts("OV7725", &Font_7x10, SSD1306_COLOR_WHITE);
}
SSD1306_GotoXY(0,53);
SSD1306_Puts(ip4addr_ntoa(&s_ip_addr), &Font_7x10, SSD1306_COLOR_WHITE);
SSD1306_UpdateScreen();
xTaskCreate(&http_server, "http_server", 2048, NULL, 5, NULL);
}