Update esp32cam-demo.ino

master
Alan 2022-01-17 17:17:44 +00:00 zatwierdzone przez GitHub
rodzic 99efce9cb1
commit 79a480c646
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
1 zmienionych plików z 60 dodań i 51 usunięć

Wyświetl plik

@ -77,7 +77,6 @@
int requestWebPage(String*, String*, int); int requestWebPage(String*, String*, int);
void handleTest(); void handleTest();
void brightLed(byte ledBrightness); void brightLed(byte ledBrightness);
//void sendText(WiFiClient &client, String theText);
// --------------------------------------------------------------- // ---------------------------------------------------------------
@ -100,7 +99,7 @@
// --------------------------------------------------------------- // ---------------------------------------------------------------
const char* stitle = "ESP32Cam-demo"; // title of this sketch const char* stitle = "ESP32Cam-demo"; // title of this sketch
const char* sversion = "06Jan22"; // Sketch version const char* sversion = "17Jan22"; // Sketch version
bool sendRGBfile = 0; // if set '/rgb' will send the rgb data as a file rather than display a HTML page bool sendRGBfile = 0; // if set '/rgb' will send the rgb data as a file rather than display a HTML page
@ -571,9 +570,8 @@ void flashLED(int reps) {
} }
// send a standard html header // send a standard html header (i.e. start of web page)
void sendHeader(WiFiClient &client) { void sendHeader(WiFiClient &client) {
// html header
client.write("HTTP/1.1 200 OK\r\n"); client.write("HTTP/1.1 200 OK\r\n");
client.write("Content-Type: text/html\r\n"); client.write("Content-Type: text/html\r\n");
client.write("Connection: close\r\n"); client.write("Connection: close\r\n");
@ -585,6 +583,21 @@ void sendHeader(WiFiClient &client) {
} }
// send a standard html footer (i.e. end of web page)
void sendFooter(WiFiClient &client) {
client.write("</body></html>\n");
delay(3);
client.stop();
}
// send line of text to both serial port and web page
void sendText(WiFiClient &client, String theText) {
if (!sendRGBfile) client.print(theText + "<br>\n");
if (serialDebug || theText.indexOf("error") > 0) Serial.println(theText); // if text contains "error"
}
// ****************************************************************************************************************** // ******************************************************************************************************************
@ -815,10 +828,7 @@ void handleRoot() {
// -------------------------------------------------------------------- // --------------------------------------------------------------------
// end html sendFooter(client); // close web page
client.write("</form></body></html>\n");
delay(3);
client.stop();
} // handleRoot } // handleRoot
@ -858,10 +868,7 @@ void handlePhoto() {
client.write("<p>Error: Failed to save image to sd card</p>\n"); client.write("<p>Error: Failed to save image to sd card</p>\n");
} }
// end html sendFooter(client); // close web page
client.write("</body></html>\n");
delay(3);
client.stop();
} // handlePhoto } // handlePhoto
@ -882,7 +889,6 @@ bool handleImg() {
IPAddress cIP = client.remoteIP(); IPAddress cIP = client.remoteIP();
if (serialDebug) Serial.println("Display stored image requested by " + cIP.toString()); if (serialDebug) Serial.println("Display stored image requested by " + cIP.toString());
int imgToShow = imageCounter; // default to showing most recent file int imgToShow = imageCounter; // default to showing most recent file
// get image number from url parameter // get image number from url parameter
@ -931,6 +937,7 @@ bool handleImg() {
} }
} }
return pRes; return pRes;
} // handleImg } // handleImg
@ -970,24 +977,17 @@ void handleNotFound() {
// ****************************************************************************************************************** // ******************************************************************************************************************
// send line of text to both serial port and web page
void sendText(WiFiClient &client, String theText) {
if (!sendRGBfile) client.print(theText + "<br>\n");
if (serialDebug || theText.indexOf("error") > 0) Serial.println(theText); // if text contains "error"
}
// ---------------------------------------------------------------- // ----------------------------------------------------------------
// -access image data as RGB - i.e. http://x.x.x.x/rgb // -access image data as RGB - i.e. http://x.x.x.x/rgb
// ---------------------------------------------------------------- // ----------------------------------------------------------------
//Demonstration on how to access raw RGB data from the camera //Demonstration on how to access raw RGB data from the camera
// Notes: // Notes:
// Set sendRGBfile to 1 in the settings at top of sketch to just send the rgb data as a file which can then be used with // Set sendRGBfile to 1 in the settings at top of sketch to just send the raw rgb data as a file which can then be used with
// the Processing sketch: https://github.com/alanesq/esp32cam-demo/blob/master/Misc/displayRGB.pde // the Processing sketch: https://github.com/alanesq/esp32cam-demo/blob/master/Misc/displayRGB.pde
// otherwise a web page is displayed showing some sample rgb data usage. // otherwise a web page is displayed showing some sample rgb data usage.
// You may want to disable auto white balance when experimenting with RGB otherwise the camera is always trying to adjust the // You may want to disable auto white balance when experimenting with RGB otherwise the camera is always trying to adjust the
// image colours to mainly white. (disable in the 'cameraImageSettings' procedure). // image colours to mainly white. (disable in the 'cameraImageSettings' procedure).
// It will fail on the highest resolution (1600x1200) as it requires more than the 4mb of available psram to store the data (1600x1200x3 bytes) // It will fail on the higher resolutions as it requires more than the 4mb of available psram to store the data (1600x1200x3 bytes)
// I learned how to read the RGB data from: https://github.com/Makerfabs/Project_Touch-Screen-Camera/blob/master/Camera_v2/Camera_v2.ino // I learned how to read the RGB data from: https://github.com/Makerfabs/Project_Touch-Screen-Camera/blob/master/Camera_v2/Camera_v2.ino
void readRGBImage() { void readRGBImage() {
@ -995,18 +995,20 @@ void readRGBImage() {
WiFiClient client = server.client(); WiFiClient client = server.client();
uint32_t tTimer; // used to time tasks // open link with client uint32_t tTimer; // used to time tasks // open link with client
// basic html header if (!sendRGBfile) {
if (!sendRGBfile) client.write("<!DOCTYPE html> <html lang='en'> <head> <title>Show RGB data</title> </head> <body>\n"); // basic html header // html header
sendHeader(client);
client.write("<title>Show RGB data</title> </head> <body>\n"); // basic html header
// display page title including clients IP // page title including clients IP
IPAddress cIP = client.remoteIP(); IPAddress cIP = client.remoteIP();
sendText(client, "Live image as rgb data, requested by " + cIP.toString()); // 'sendText' sends the String to both serial port and web page sendText(client, "Live image as rgb data, requested by " + cIP.toString()); // 'sendText' sends the String to both serial port and web page
}
// make sure psram is available // make sure psram is available
if (!psramFound()) { if (!psramFound()) {
sendText(client,"error: Unable to get RGB data as no psram available to store it in"); sendText(client,"error: no psram available to store the RGB data");
delay(3); if (!sendRGBfile) sendFooter(client); // close web page
client.stop();
return; return;
} }
@ -1018,7 +1020,9 @@ void readRGBImage() {
tTimer = millis(); // store time that image capture started tTimer = millis(); // store time that image capture started
fb = esp_camera_fb_get(); fb = esp_camera_fb_get();
if (!fb) { if (!fb) {
sendText(client,"error: failed to capture image"); sendText(client,"error: failed to capture image from camera");
if (!sendRGBfile) sendFooter(client); // close web page
return;
} else { } else {
sendText(client, "-JPG image capture took " + String(millis() - tTimer) + " milliseconds"); // report time it took to capture an image sendText(client, "-JPG image capture took " + String(millis() - tTimer) + " milliseconds"); // report time it took to capture an image
sendText(client,"-Image resolution=" + String(fb->width) + "x" + String(fb->height)); sendText(client,"-Image resolution=" + String(fb->width) + "x" + String(fb->height));
@ -1029,7 +1033,11 @@ void readRGBImage() {
sendText(client,"Free psram before rgb data allocated = " + String(heap_caps_get_free_size(MALLOC_CAP_SPIRAM) / 1024) + "K"); sendText(client,"Free psram before rgb data allocated = " + String(heap_caps_get_free_size(MALLOC_CAP_SPIRAM) / 1024) + "K");
void *ptrVal = NULL; // create a pointer for memory location to store the data void *ptrVal = NULL; // create a pointer for memory location to store the data
uint32_t ARRAY_LENGTH = fb->width * fb->height * 3; // calculate memory required to store the RGB data (i.e. number of pixels in the jpg image x 3) uint32_t ARRAY_LENGTH = fb->width * fb->height * 3; // calculate memory required to store the RGB data (i.e. number of pixels in the jpg image x 3)
if (heap_caps_get_free_size( MALLOC_CAP_SPIRAM) < ARRAY_LENGTH) sendText(client," -error: not enough free psram to store the rgb data- "); if (heap_caps_get_free_size( MALLOC_CAP_SPIRAM) < ARRAY_LENGTH) {
sendText(client,"error: not enough free psram to store the rgb data");
if (!sendRGBfile) sendFooter(client); // close web page
return;
}
ptrVal = heap_caps_malloc(ARRAY_LENGTH, MALLOC_CAP_SPIRAM); // allocate memory space for the rgb data ptrVal = heap_caps_malloc(ARRAY_LENGTH, MALLOC_CAP_SPIRAM); // allocate memory space for the rgb data
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
sendText(client,"Free psram after rgb data allocated = " + String(heap_caps_get_free_size(MALLOC_CAP_SPIRAM) / 1024) + "K"); sendText(client,"Free psram after rgb data allocated = " + String(heap_caps_get_free_size(MALLOC_CAP_SPIRAM) / 1024) + "K");
@ -1037,7 +1045,11 @@ void readRGBImage() {
// 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);
if (!jpeg_converted) sendText(client," -error: failed to convert image to RGB- "); if (!jpeg_converted) {
sendText(client,"error: failed to convert image to RGB data");
if (!sendRGBfile) sendFooter(client); // close web page
return;
}
sendText(client, "Conversion from jpg to RGB took " + String(millis() - tTimer) + " milliseconds");// report how long the conversion took sendText(client, "Conversion from jpg to RGB took " + String(millis() - tTimer) + " milliseconds");// report how long the conversion took
@ -1047,8 +1059,8 @@ void readRGBImage() {
// ****** examples of using the resulting RGB data ***** // ****** examples of using the resulting RGB data *****
// display some of the resulting data // display some of the resulting data
uint32_t resultsToShow = 60; // how much data to display uint32_t resultsToShow = 50; // how much data to display
sendText(client,"<br>R , G , B"); sendText(client,"<br>R , G , B - for first " + String(resultsToShow) + "pixels of image");
for (uint32_t i = 0; i < resultsToShow-2; i+=3) { for (uint32_t i = 0; i < resultsToShow-2; i+=3) {
sendText(client,String(rgb[i+2]) + "," + String(rgb[i+1]) + "," + String(rgb[i+0])); // Red , Green , Blue sendText(client,String(rgb[i+2]) + "," + String(rgb[i+1]) + "," + String(rgb[i+0])); // Red , Green , Blue
// // calculate the x and y coordinate of the current pixel // // calculate the x and y coordinate of the current pixel
@ -1076,10 +1088,7 @@ void readRGBImage() {
// ******************************************************* // *******************************************************
// end html if (!sendRGBfile) sendFooter(client); // close web page
if (!sendRGBfile) client.write("</body></html>\n");
delay(3);
client.stop();
// finished with the data so free up the memory space used in psram // finished with the data so free up the memory space used in psram
esp_camera_fb_return(fb); // camera frame buffer esp_camera_fb_return(fb); // camera frame buffer
@ -1088,6 +1097,7 @@ void readRGBImage() {
} // readRGBImage } // readRGBImage
// ****************************************************************************************************************** // ******************************************************************************************************************
@ -1169,7 +1179,7 @@ bool handleJPG() {
// ---------------------------------------------------------------- // ----------------------------------------------------------------
// -stream requested i.e. http://x.x.x.x/stream // -stream requested i.e. http://x.x.x.x/stream
// ---------------------------------------------------------------- // ----------------------------------------------------------------
// Sends cam stream - thanks to Uwe Gerlach for the code showing me how to do this // Sends video stream - thanks to Uwe Gerlach for the code showing me how to do this
void handleStream(){ void handleStream(){
@ -1200,13 +1210,15 @@ void handleStream(){
fb = esp_camera_fb_get(); // capture live image as jpg fb = esp_camera_fb_get(); // capture live image as jpg
if (!fb) { if (!fb) {
if (serialDebug) Serial.println("Error: failed to capture jpg image"); if (serialDebug) Serial.println("Error: failed to capture jpg image");
} } else {
client.write(CTNTTYPE, cntLen); // send content type html (i.e. jpg image) // send image
sprintf( buf, "%d\r\n\r\n", fb->len); // format the image's size as html and put in to 'buf' client.write(CTNTTYPE, cntLen); // send content type html (i.e. jpg image)
client.write(buf, strlen(buf)); // send result (image size) sprintf( buf, "%d\r\n\r\n", fb->len); // format the image's size as html and put in to 'buf'
client.write((char *)fb->buf, fb->len); // send the image data client.write(buf, strlen(buf)); // send result (image size)
client.write(BOUNDARY, bdrLen); // send html boundary see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type client.write((char *)fb->buf, fb->len); // send the image data
esp_camera_fb_return(fb); // return image so memory can be released 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 buffer so memory can be released
}
} }
if (serialDebug) Serial.println("Video stream stopped"); if (serialDebug) Serial.println("Video stream stopped");
@ -1322,10 +1334,7 @@ void handleTest() {
// ------------------------------------------------------------------- // -------------------------------------------------------------------
// end html sendFooter(client); // close web page
client.write("</body></html>\n");
delay(3);
client.stop();
} // handleTest } // handleTest