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.

  1. Uploader le code

  2. Enlever le pont entre IO0 et GND

  3. 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
#include "esp_camera.h"
18
#include <WiFi.h>
19
#include "esp_timer.h"
20
#include "img_converters.h"
21
#include "Arduino.h"
22
#include "fb_gfx.h"
23
#include "soc/soc.h" //disable brownout problems
24
#include "soc/rtc_cntl_reg.h"  //disable brownout problems
25
#include "esp_http_server.h"
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
#define PART_BOUNDARY "123456789000000000000987654321"
32
33
// This project was tested with the AI Thinker Model, M5STACK PSRAM Model and M5STACK WITHOUT PSRAM
34
#define CAMERA_MODEL_AI_THINKER
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
#if defined(CAMERA_MODEL_WROVER_KIT)
42
  #define PWDN_GPIO_NUM    -1
43
  #define RESET_GPIO_NUM   -1
44
  #define XCLK_GPIO_NUM    21
45
  #define SIOD_GPIO_NUM    26
46
  #define SIOC_GPIO_NUM    27
47
  
48
  #define Y9_GPIO_NUM      35
49
  #define Y8_GPIO_NUM      34
50
  #define Y7_GPIO_NUM      39
51
  #define Y6_GPIO_NUM      36
52
  #define Y5_GPIO_NUM      19
53
  #define Y4_GPIO_NUM      18
54
  #define Y3_GPIO_NUM       5
55
  #define Y2_GPIO_NUM       4
56
  #define VSYNC_GPIO_NUM   25
57
  #define HREF_GPIO_NUM    23
58
  #define PCLK_GPIO_NUM    22
59
60
#elif defined(CAMERA_MODEL_M5STACK_PSRAM)
61
  #define PWDN_GPIO_NUM     -1
62
  #define RESET_GPIO_NUM    15
63
  #define XCLK_GPIO_NUM     27
64
  #define SIOD_GPIO_NUM     25
65
  #define SIOC_GPIO_NUM     23
66
  
67
  #define Y9_GPIO_NUM       19
68
  #define Y8_GPIO_NUM       36
69
  #define Y7_GPIO_NUM       18
70
  #define Y6_GPIO_NUM       39
71
  #define Y5_GPIO_NUM        5
72
  #define Y4_GPIO_NUM       34
73
  #define Y3_GPIO_NUM       35
74
  #define Y2_GPIO_NUM       32
75
  #define VSYNC_GPIO_NUM    22
76
  #define HREF_GPIO_NUM     26
77
  #define PCLK_GPIO_NUM     21
78
79
#elif defined(CAMERA_MODEL_M5STACK_WITHOUT_PSRAM)
80
  #define PWDN_GPIO_NUM     -1
81
  #define RESET_GPIO_NUM    15
82
  #define XCLK_GPIO_NUM     27
83
  #define SIOD_GPIO_NUM     25
84
  #define SIOC_GPIO_NUM     23
85
  
86
  #define Y9_GPIO_NUM       19
87
  #define Y8_GPIO_NUM       36
88
  #define Y7_GPIO_NUM       18
89
  #define Y6_GPIO_NUM       39
90
  #define Y5_GPIO_NUM        5
91
  #define Y4_GPIO_NUM       34
92
  #define Y3_GPIO_NUM       35
93
  #define Y2_GPIO_NUM       17
94
  #define VSYNC_GPIO_NUM    22
95
  #define HREF_GPIO_NUM     26
96
  #define PCLK_GPIO_NUM     21
97
98
#elif defined(CAMERA_MODEL_AI_THINKER)
99
  #define PWDN_GPIO_NUM     32
100
  #define RESET_GPIO_NUM    -1
101
  #define XCLK_GPIO_NUM      0
102
  #define SIOD_GPIO_NUM     26
103
  #define SIOC_GPIO_NUM     27
104
  
105
  #define Y9_GPIO_NUM       35
106
  #define Y8_GPIO_NUM       34
107
  #define Y7_GPIO_NUM       39
108
  #define Y6_GPIO_NUM       36
109
  #define Y5_GPIO_NUM       21
110
  #define Y4_GPIO_NUM       19
111
  #define Y3_GPIO_NUM       18
112
  #define Y2_GPIO_NUM        5
113
  #define VSYNC_GPIO_NUM    25
114
  #define HREF_GPIO_NUM     23
115
  #define PCLK_GPIO_NUM     22
116
#else
117
  #error "Camera model not selected"
118
#endif
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
}