Des codes fonctionnels avec la bibliothèque espressif()
Exemple serveur jpeg sans interface de configuration
Reste juste à compléter les codes Wifi.
l'image proposée est en 1600 sur 1200.

Uploader le code
Enlever le pont entre IO0 et GND
Cliquer sur la console
4. Appuyer sur reset et récupérer l'IP
5 Se connecter sur http://IP

1
/*********
2
Rui Santos
3
Complete project details at https://RandomNerdTutorials.com/esp32-cam-video-streaming-web-server-camera-home-assistant/
4
5
IMPORTANT!!!
6
- Select Board "AI Thinker ESP32-CAM"
7
- GPIO 0 must be connected to GND to upload a sketch
8
- After connecting GPIO 0 to GND, press the ESP32-CAM on-board RESET button to put your board in flashing mode
9
10
Permission is hereby granted, free of charge, to any person obtaining a copy
11
of this software and associated documentation files.
12
13
The above copyright notice and this permission notice shall be included in all
14
copies or substantial portions of the Software.
15
*********/
16
17
18
19
20
21
22
23
//disable brownout problems
24
//disable brownout problems
25
26
27
//Replace with your network credentials
28
const char* ssid = "REPLACE_WITH_YOUR_SSID";
29
const char* password = "REPLACE_WITH_YOUR_PASSWORD";
30
31
32
33
// This project was tested with the AI Thinker Model, M5STACK PSRAM Model and M5STACK WITHOUT PSRAM
34
35
//#define CAMERA_MODEL_M5STACK_PSRAM
36
//#define CAMERA_MODEL_M5STACK_WITHOUT_PSRAM
37
38
// Not tested with this model
39
//#define CAMERA_MODEL_WROVER_KIT
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
static const char* _STREAM_CONTENT_TYPE = "multipart/x-mixed-replace;boundary=" PART_BOUNDARY;
121
static const char* _STREAM_BOUNDARY = "\r\n--" PART_BOUNDARY "\r\n";
122
static const char* _STREAM_PART = "Content-Type: image/jpeg\r\nContent-Length: %u\r\n\r\n";
123
124
httpd_handle_t stream_httpd = NULL;
125
126
static esp_err_t stream_handler(httpd_req_t *req){
127
camera_fb_t * fb = NULL;
128
esp_err_t res = ESP_OK;
129
size_t _jpg_buf_len = 0;
130
uint8_t * _jpg_buf = NULL;
131
char * part_buf[64];
132
133
res = httpd_resp_set_type(req, _STREAM_CONTENT_TYPE);
134
if(res != ESP_OK){
135
return res;
136
}
137
138
while(true){
139
fb = esp_camera_fb_get();
140
if (!fb) {
141
Serial.println("Camera capture failed");
142
res = ESP_FAIL;
143
} else {
144
if(fb->width > 400){
145
if(fb->format != PIXFORMAT_JPEG){
146
bool jpeg_converted = frame2jpg(fb, 80, &_jpg_buf, &_jpg_buf_len);
147
esp_camera_fb_return(fb);
148
fb = NULL;
149
if(!jpeg_converted){
150
Serial.println("JPEG compression failed");
151
res = ESP_FAIL;
152
}
153
} else {
154
_jpg_buf_len = fb->len;
155
_jpg_buf = fb->buf;
156
}
157
}
158
}
159
if(res == ESP_OK){
160
size_t hlen = snprintf((char *)part_buf, 64, _STREAM_PART, _jpg_buf_len);
161
res = httpd_resp_send_chunk(req, (const char *)part_buf, hlen);
162
}
163
if(res == ESP_OK){
164
res = httpd_resp_send_chunk(req, (const char *)_jpg_buf, _jpg_buf_len);
165
}
166
if(res == ESP_OK){
167
res = httpd_resp_send_chunk(req, _STREAM_BOUNDARY, strlen(_STREAM_BOUNDARY));
168
}
169
if(fb){
170
esp_camera_fb_return(fb);
171
fb = NULL;
172
_jpg_buf = NULL;
173
} else if(_jpg_buf){
174
free(_jpg_buf);
175
_jpg_buf = NULL;
176
}
177
if(res != ESP_OK){
178
break;
179
}
180
//Serial.printf("MJPG: %uB\n",(uint32_t)(_jpg_buf_len));
181
}
182
return res;
183
}
184
185
void startCameraServer(){
186
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
187
config.server_port = 80;
188
189
httpd_uri_t index_uri = {
190
.uri = "/",
191
.method = HTTP_GET,
192
.handler = stream_handler,
193
.user_ctx = NULL
194
};
195
196
//Serial.printf("Starting web server on port: '%d'\n", config.server_port);
197
if (httpd_start(&stream_httpd, &config) == ESP_OK) {
198
httpd_register_uri_handler(stream_httpd, &index_uri);
199
}
200
}
201
202
void setup() {
203
WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); //disable brownout detector
204
205
Serial.begin(115200);
206
Serial.setDebugOutput(false);
207
208
camera_config_t config;
209
config.ledc_channel = LEDC_CHANNEL_0;
210
config.ledc_timer = LEDC_TIMER_0;
211
config.pin_d0 = Y2_GPIO_NUM;
212
config.pin_d1 = Y3_GPIO_NUM;
213
config.pin_d2 = Y4_GPIO_NUM;
214
config.pin_d3 = Y5_GPIO_NUM;
215
config.pin_d4 = Y6_GPIO_NUM;
216
config.pin_d5 = Y7_GPIO_NUM;
217
config.pin_d6 = Y8_GPIO_NUM;
218
config.pin_d7 = Y9_GPIO_NUM;
219
config.pin_xclk = XCLK_GPIO_NUM;
220
config.pin_pclk = PCLK_GPIO_NUM;
221
config.pin_vsync = VSYNC_GPIO_NUM;
222
config.pin_href = HREF_GPIO_NUM;
223
config.pin_sscb_sda = SIOD_GPIO_NUM;
224
config.pin_sscb_scl = SIOC_GPIO_NUM;
225
config.pin_pwdn = PWDN_GPIO_NUM;
226
config.pin_reset = RESET_GPIO_NUM;
227
config.xclk_freq_hz = 20000000;
228
config.pixel_format = PIXFORMAT_JPEG;
229
230
if(psramFound()){
231
config.frame_size = FRAMESIZE_UXGA;
232
config.jpeg_quality = 10;
233
config.fb_count = 2;
234
} else {
235
config.frame_size = FRAMESIZE_SVGA;
236
config.jpeg_quality = 12;
237
config.fb_count = 1;
238
}
239
240
// Camera init
241
esp_err_t err = esp_camera_init(&config);
242
if (err != ESP_OK) {
243
Serial.printf("Camera init failed with error 0x%x", err);
244
return;
245
}
246
// Wi-Fi connection
247
WiFi.begin(ssid, password);
248
while (WiFi.status() != WL_CONNECTED) {
249
delay(500);
250
Serial.print(".");
251
}
252
Serial.println("");
253
Serial.println("WiFi connected");
254
255
Serial.print("Camera Stream Ready! Go to: http://");
256
Serial.print(WiFi.localIP());
257
258
// Start streaming web server
259
startCameraServer();
260
}
261
262
void loop() {
263
delay(1);
264
}