Update esp32cam-demo.ino

master
Alan 2022-01-04 23:25:21 +00:00 zatwierdzone przez GitHub
rodzic 576906bc9b
commit 3eb3e5bd3a
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
1 zmienionych plików z 25 dodań i 33 usunięć

Wyświetl plik

@ -101,7 +101,7 @@
// --------------------------------------------------------------- // ---------------------------------------------------------------
const char* stitle = "ESP32Cam-demo"; // title of this sketch const char* stitle = "ESP32Cam-demo"; // title of this sketch
const char* sversion = "03Jan22"; // Sketch version const char* sversion = "04Jan22"; // Sketch version
bool sendRGBfile = 0; // if set '/rgb' will send the rgb data as a file rather than display some on a HTML page bool sendRGBfile = 0; // if set '/rgb' will send the rgb data as a file rather than display some on a HTML page
@ -1028,7 +1028,6 @@ void readRGBImage() {
//MessageRGB(client,"-Image size=" + String(pic->len)); //MessageRGB(client,"-Image size=" + String(pic->len));
} }
// allocate memory to store the rgb data (in psram, 3 bytes per pixel) // allocate memory to store the rgb data (in psram, 3 bytes per pixel)
if (!psramFound()) MessageRGB(client," -error no psram found- "); if (!psramFound()) MessageRGB(client," -error no psram found- ");
MessageRGB(client,"Free psram before rgb data stored = " + String(heap_caps_get_free_size(MALLOC_CAP_SPIRAM))); MessageRGB(client,"Free psram before rgb data stored = " + String(heap_caps_get_free_size(MALLOC_CAP_SPIRAM)));
@ -1039,7 +1038,6 @@ void readRGBImage() {
uint8_t *rgb = (uint8_t *)ptrVal; // create the 'rgb' array pointer to the allocated memory space uint8_t *rgb = (uint8_t *)ptrVal; // create the 'rgb' array pointer to the allocated memory space
MessageRGB(client,"Free psram after rgb data stored = " + String(heap_caps_get_free_size(MALLOC_CAP_SPIRAM))); MessageRGB(client,"Free psram after rgb data stored = " + String(heap_caps_get_free_size(MALLOC_CAP_SPIRAM)));
// convert the captured jpg image (fb) to rgb data (store in 'rgb' array) // convert the captured jpg image (fb) to rgb data (store in 'rgb' array)
tTimer = millis(); // store time that image conversion process started tTimer = millis(); // store time that image conversion process started
bool jpeg_converted = fmt2rgb888(fb->buf, fb->len, PIXFORMAT_JPEG, rgb); bool jpeg_converted = fmt2rgb888(fb->buf, fb->len, PIXFORMAT_JPEG, rgb);
@ -1132,11 +1130,12 @@ bool getNTPtime(int sec) {
// ---------------------------------------------------------------- // ----------------------------------------------------------------
// -capture image and send as jpg i.e. http://x.x.x.x/jpg // -capture image and send as jpg i.e. http://x.x.x.x/jpg
// ---------------------------------------------------------------- // ----------------------------------------------------------------
// Capture image and send as a jpg
void handleJPG() { void handleJPG() {
WiFiClient client = server.client(); // open link with client WiFiClient client = server.client(); // open link with client
char buf[32];
camera_fb_t * fb = NULL;
// log page request including clients IP address // log page request including clients IP address
if (serialDebug) { if (serialDebug) {
@ -1144,45 +1143,38 @@ void handleJPG() {
Serial.printf("jpg requested from: %d.%d.%d.%d \n", cip[0], cip[1], cip[2], cip[3]); Serial.printf("jpg requested from: %d.%d.%d.%d \n", cip[0], cip[1], cip[2], cip[3]);
} }
// HTML used in the web page // build and send html
const char HEADER[] = "HTTP/1.1 200 OK\r\n" \ const char HEADER[] = "HTTP/1.1 200 OK\r\nAccess-Control-Allow-Origin: *\r\n";
"Access-Control-Allow-Origin: *\r\n" \ const char CTNTTYPE[] = "Content-Type: image/jpeg\r\nContent-Length: ";
"Content-Type: multipart/x-mixed-replace; boundary=123456789000000000000987654321\r\n";
const char BOUNDARY[] = "\r\n--123456789000000000000987654321\r\n"; // marks end of each image frame
const char CTNTTYPE[] = "Content-Type: image/jpeg\r\nContent-Length: "; // marks start of image data
const int hdrLen = strlen(HEADER); // length of the stored text, used when sending to web page const int hdrLen = strlen(HEADER); // length of the stored text, used when sending to web page
const int bdrLen = strlen(BOUNDARY);
const int cntLen = strlen(CTNTTYPE); const int cntLen = strlen(CTNTTYPE);
// temp stores
char buf[32];
int s;
camera_fb_t * fb = NULL;
// send html header
client.write(HEADER, hdrLen); client.write(HEADER, hdrLen);
client.write(BOUNDARY, bdrLen); client.write(CTNTTYPE, cntLen);
// capture and send image // capture and send image
fb = esp_camera_fb_get(); // capture live image fb = esp_camera_fb_get();
if (!fb) { if (!fb) {
if (serialDebug) Serial.println("Error: failed to capture image"); if (serialDebug) Serial.println("Error: failed to capture image");
return; // capture failed return;
} }
s = fb->len; // store size of image (i.e. buffer length)
client.write(CTNTTYPE, cntLen); // send content type html (i.e. jpg image)
sprintf( buf, "%d\r\n\r\n", s ); // format the image's size as html and put in to 'buf'
client.write(buf, strlen(buf)); // send result (image size)
client.write((char *)fb->buf, s); // send the image data
client.write(BOUNDARY, bdrLen); // send html boundary see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type
esp_camera_fb_return(fb); // return image so memory can be released
// put text in to 'buf' char array and send
sprintf( buf, "%d\r\n\r\n", fb->len);
client.write(buf, strlen(buf));
// send the jpg data
client.write((char *)fb->buf, fb->len);
// close client connection
delay(3); delay(3);
client.stop(); client.stop();
esp_camera_fb_return(fb); // return image so memory can be released
} // handleJPG } // handleJPG
// ---------------------------------------------------------------- // ----------------------------------------------------------------
// -stream requested i.e. http://x.x.x.x/stream // -stream requested i.e. http://x.x.x.x/stream
// ---------------------------------------------------------------- // ----------------------------------------------------------------