-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathtimelapse-javascript.ino
177 lines (149 loc) · 8.15 KB
/
timelapse-javascript.ino
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
#include "esp_http_server.h"
#include "esp_camera.h"
#include <WiFi.h>
#include "Arduino.h"
const char* ssid = "NSA";
const char* password = "orange";
//Select a sensible image size for image to avoid overloading camera/wifi etc
//#define TIMELAPSE_FRAMESIZE FRAMESIZE_QQVGA // 160x120
//#define TIMELAPSE_FRAMESIZE FRAMESIZE_QQVGA2 // 128x160
//#define TIMELAPSE_FRAMESIZE FRAMESIZE_QCIF // 176x144
//#define TIMELAPSE_FRAMESIZE FRAMESIZE_HQVGA // 240x176
//#define TIMELAPSE_FRAMESIZE FRAMESIZE_QVGA // 320x240
//#define TIMELAPSE_FRAMESIZE FRAMESIZE_CIF // 400x296
//#define TIMELAPSE_FRAMESIZE FRAMESIZE_VGA // 640x480
//#define TIMELAPSE_FRAMESIZE FRAMESIZE_SVGA // 800x600
#define TIMELAPSE_FRAMESIZE FRAMESIZE_XGA // 1024x768
//#define TIMELAPSE_FRAMESIZE FRAMESIZE_SXGA // 1280x1024
//#define TIMELAPSE_FRAMESIZE FRAMESIZE_UXGA // 1600x1200
//#define TIMELAPSE_FRAMESIZE FRAMESIZE_QXGA // 2048*1536
#define index_html_gz_len 3663
// view source in browser to see HTML. See Editing ESP32 HTML tutorial for how to edit.
const uint8_t index_html_gz[] = {
0x1f,0x8b,0x08,0x00,0x97,0xa1,0xb9,0x5c,0x00,0xff,0x7d,0x54,0x61,0x8f,0xe2,0x36,0x10,0xfd,0x4c,0x7e,0xc5,0x34,0x3a,0x29,0x49,0x0f,0x08,0x5c,0x45,0x55,0xdd,0x26,0xa9,0xda,0x05,0xb5,0x54,0xcb,0xde,0xa9,0xcb,0xb7,0xaa,0xea,0x79,0xed,0x21,0xb8,0xe7,0xd8,0x69,0xec,0x84,0xa2,0x15,0xff,0xfd,0xc6,0x49,0x38,0xd1,0xaa,0x57,0x21,0x11,0x87,0x79,0x7e,0xf3,0x66,0xe6,0x0d,0xd9,0x57,0xc2,0x70,0x77,0xae,0x11,0x8e,0xae,0x52,0x45,0x90,0x5d,0x1f,0xc8,0x04,0x3d,0x2a,0x74,0x0c,0xf8,0x91,0x35,0x16,0x5d,0x1e,0xb6,0xee,0x30,0xfb,0x2e,0xbc,0xfe,0xac,0x59,0x85,0x79,0xd8,0x49,0x3c,0xd5,0xa6,0x71,0x21,0x70,0xa3,0x1d,0x6a,0x82,0x9d,0xa4,0x70,0xc7,0x5c,0x60,0x27,0x39,0xce,0xfa,0x97,0xa9,0xd4,0xd2,0x49,0xa6,0x66,0x96,0x33,0x85,0xf9,0xd2,0x73,0x38,0xe9,0x14,0x16,0xbf,0xb0,0x8e,0x3d,0xf1,0x46,0xd6,0x0e,0xb6,0x15,0x2b,0xd1,0x66,0xe9,0x10,0x08,0xb2,0x74,0xd4,0xf0,0x6c,0xc4,0xb9,0x08,0x7e,0xdd,0xdc,0xbf,0xdb,0xed,0x36,0x8f,0xeb,0xcd,0x1a,0x9e,0x36,0xfb,0xfd,0xf6,0xf1,0xa7,0xa7,0xb7,0xd9,0x73,0x53,0x04,0x6f,0xc0,0x22,0xe5,0x16,0x16,0x66,0xf0,0x1e,0x4d,0xad,0x10,0x4e,0x4c,0x7d,0x94,0xba,0x04,0x61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0xeb,0x17,0x5f,0x48,0x30,0x19,0x3a,0x7a,0x17,0x5c,0x12,0x5a,0xf2,0xab,0x3d,0xb2,0x74,0xd8,0x73,0x5a,0x7b,0xff,0x0f,0xf4,0x09,0xe7,0x37,0xcb,0x1c,0x98,0x04,0x00,0x00
};
httpd_handle_t camera_httpd = NULL;
char filename[40];
int file_number = 0;
// CAMERA_MODEL_AI_THINKER
#define PWDN_GPIO_NUM 32
#define RESET_GPIO_NUM -1
#define XCLK_GPIO_NUM 0
#define SIOD_GPIO_NUM 26
#define SIOC_GPIO_NUM 27
#define Y9_GPIO_NUM 35
#define Y8_GPIO_NUM 34
#define Y7_GPIO_NUM 39
#define Y6_GPIO_NUM 36
#define Y5_GPIO_NUM 21
#define Y4_GPIO_NUM 19
#define Y3_GPIO_NUM 18
#define Y2_GPIO_NUM 5
#define VSYNC_GPIO_NUM 25
#define HREF_GPIO_NUM 23
#define PCLK_GPIO_NUM 22
void startCameraServer();
void setup() {
Serial.begin(115200);
Serial.setDebugOutput(true);
Serial.println();
camera_config_t config;
config.ledc_channel = LEDC_CHANNEL_0;
config.ledc_timer = LEDC_TIMER_0;
config.pin_d0 = Y2_GPIO_NUM;
config.pin_d1 = Y3_GPIO_NUM;
config.pin_d2 = Y4_GPIO_NUM;
config.pin_d3 = Y5_GPIO_NUM;
config.pin_d4 = Y6_GPIO_NUM;
config.pin_d5 = Y7_GPIO_NUM;
config.pin_d6 = Y8_GPIO_NUM;
config.pin_d7 = Y9_GPIO_NUM;
config.pin_xclk = XCLK_GPIO_NUM;
config.pin_pclk = PCLK_GPIO_NUM;
config.pin_vsync = VSYNC_GPIO_NUM;
config.pin_href = HREF_GPIO_NUM;
config.pin_sscb_sda = SIOD_GPIO_NUM;
config.pin_sscb_scl = SIOC_GPIO_NUM;
config.pin_pwdn = PWDN_GPIO_NUM;
config.pin_reset = RESET_GPIO_NUM;
config.xclk_freq_hz = 20000000;
config.pixel_format = PIXFORMAT_JPEG;
//init with high specs to pre-allocate larger buffers
if (psramFound()) {
config.frame_size = FRAMESIZE_UXGA;
config.jpeg_quality = 10;
config.fb_count = 2;
} else {
config.frame_size = FRAMESIZE_SVGA;
config.jpeg_quality = 12;
config.fb_count = 1;
}
// camera init
esp_err_t err = esp_camera_init(&config);
if (err != ESP_OK) {
Serial.printf("Camera init failed with error 0x%x", err);
return;
}
//drop down frame size for shorter interval capture
sensor_t * s = esp_camera_sensor_get();
s->set_framesize(s, TIMELAPSE_FRAMESIZE);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
startCameraServer();
Serial.print("Camera Ready! Use 'http://");
Serial.print(WiFi.localIP());
Serial.println("' to connect");
}
static esp_err_t capture_handler(httpd_req_t *req) {
camera_fb_t * fb = NULL;
esp_err_t res = ESP_OK;
fb = esp_camera_fb_get();
if (!fb) {
Serial.println("Camera capture failed");
httpd_resp_send_500(req);
return ESP_FAIL;
}
file_number++;
sprintf(filename, "inline; filename=capture_%d.jpg", file_number);
httpd_resp_set_type(req, "image/jpeg");
httpd_resp_set_hdr(req, "Content-Disposition", filename);
size_t out_len, out_width, out_height;
size_t fb_len = 0;
fb_len = fb->len;
res = httpd_resp_send(req, (const char *)fb->buf, fb->len);
esp_camera_fb_return(fb);
return res;
}
static esp_err_t index_handler(httpd_req_t *req) {
httpd_resp_set_type(req, "text/html");
httpd_resp_set_hdr(req, "Content-Encoding", "gzip");
return httpd_resp_send(req, (const char *)index_html_gz, index_html_gz_len);
}
void startCameraServer() {
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
httpd_uri_t index_uri = {
.uri = "/",
.method = HTTP_GET,
.handler = index_handler,
.user_ctx = NULL
};
httpd_uri_t capture_uri = {
.uri = "/capture",
.method = HTTP_GET,
.handler = capture_handler,
.user_ctx = NULL
};
if (httpd_start(&camera_httpd, &config) == ESP_OK) {
httpd_register_uri_handler(camera_httpd, &index_uri);
httpd_register_uri_handler(camera_httpd, &capture_uri);
}
}
void loop() {
// loop not used
delay(10000);
}