From 518899b4fcad8a9e2200431439a4805debfb19a3 Mon Sep 17 00:00:00 2001 From: Alan <60433566+alanesq@users.noreply.github.com> Date: Thu, 20 Jan 2022 10:22:57 +0000 Subject: [PATCH] Update esp32cam-demo.ino --- esp32cam-demo.ino | 114 ++++++++++++++++++++++++++++++---------------- 1 file changed, 76 insertions(+), 38 deletions(-) diff --git a/esp32cam-demo.ino b/esp32cam-demo.ino index 895c71d..1554b51 100644 --- a/esp32cam-demo.ino +++ b/esp32cam-demo.ino @@ -76,6 +76,7 @@ void handleTest(); void brightLed(byte ledBrightness); void setupFlashPWM(); + void changeResolution(framesize_t); // --------------------------------------------------------------- @@ -83,7 +84,7 @@ // --------------------------------------------------------------- const char* stitle = "ESP32Cam-demo"; // title of this sketch - const char* sversion = "19Jan22"; // Sketch version + const char* sversion = "20Jan22"; // Sketch version bool sendRGBfile = 0; // if set '/rgb' will just return raw rgb data which can be saved as a file rather than display a HTML pag @@ -524,12 +525,12 @@ bool cameraImageSettings() { // ****************************************************************************************************************** -// ---------------------------------------------------------------- -// Misc small procedures -// ---------------------------------------------------------------- +// Misc small procedures -// set up PWM for the illumination LED (flash) +// ---------------------------------------------------------------- +// set up PWM for the illumination LED (flash) +// ---------------------------------------------------------------- // note: I am not sure PWM is very reliable on the esp32cam - requires more testing void setupFlashPWM() { ledcSetup(ledChannel, ledFreq, ledRresolution); @@ -537,6 +538,7 @@ void setupFlashPWM() { brightLed(brightLEDbrightness); } + // change illumination LED brightness void brightLed(byte ledBrightness){ brightLEDbrightness = ledBrightness; // store setting @@ -545,7 +547,9 @@ void setupFlashPWM() { } -// returns the current real time as a String +// ---------------------------------------------------------------- +// returns the current real time as a String +// ---------------------------------------------------------------- // see: https://randomnerdtutorials.com/esp32-date-time-ntp-client-server-arduino/ String localTime() { struct tm timeinfo; @@ -556,7 +560,9 @@ String localTime() { } -// flash the indicator led 'reps' number of times +// ---------------------------------------------------------------- +// flash the indicator led 'reps' number of times +// ---------------------------------------------------------------- void flashLED(int reps) { for(int x=0; x < reps; x++) { digitalWrite(indicatorLED,LOW); @@ -567,7 +573,9 @@ void flashLED(int reps) { } -// send a standard html header (i.e. start of web page) +// ---------------------------------------------------------------- +// send a standard html header (i.e. start of web page) +// ---------------------------------------------------------------- void sendHeader(WiFiClient &client, String wTitle) { client.write("HTTP/1.1 200 OK\r\n"); client.write("Content-Type: text/html\r\n"); @@ -581,7 +589,9 @@ void sendHeader(WiFiClient &client, String wTitle) { } -// send a standard html footer (i.e. end of web page) +// ---------------------------------------------------------------- +// send a standard html footer (i.e. end of web page) +// ---------------------------------------------------------------- void sendFooter(WiFiClient &client) { client.write("\n"); delay(3); @@ -589,14 +599,18 @@ void sendFooter(WiFiClient &client) { } -// send line of text to both serial port and web page - used by readRGBImage +// ---------------------------------------------------------------- +// send line of text to both serial port and web page - used by readRGBImage +// ---------------------------------------------------------------- void sendText(WiFiClient &client, String theText) { if (!sendRGBfile) client.print(theText + "
\n"); if (serialDebug || theText.indexOf("error") > 0) Serial.println(theText); // if text contains "error" } -// reset the camera example - see handleTest() for example +// ---------------------------------------------------------------- +// reset the camera example - see handleTest() for example +// ---------------------------------------------------------------- void resetCamera() { // power cycle the camera module (handy if camera stops responding) digitalWrite(PWDN_GPIO_NUM, HIGH); // turn power off to camera module @@ -610,6 +624,26 @@ void resetCamera() { } +// ---------------------------------------------------------------- +// -change image resolution +// ---------------------------------------------------------------- +// if required resolution not supplied it cycles through several +// note: this stops PWM on the flash working for some reason +void changeResolution(framesize_t tRes = FRAMESIZE_96X96) { + esp_camera_deinit(); // disable camera + delay(50); + if (tRes == FRAMESIZE_96X96) { // taken as none supplied so cycle through several + if (FRAME_SIZE_IMAGE == FRAMESIZE_QVGA) tRes = FRAMESIZE_VGA; + else if (FRAME_SIZE_IMAGE == FRAMESIZE_VGA) tRes = FRAMESIZE_XGA; + else if (FRAME_SIZE_IMAGE == FRAMESIZE_XGA) tRes = FRAMESIZE_UXGA; + else tRes = FRAMESIZE_QVGA; + } + FRAME_SIZE_IMAGE = tRes; + initialiseCamera(); + if (serialDebug) Serial.println("Camera resolution changed to " + String(tRes)); +} + + // ****************************************************************************************************************** @@ -717,7 +751,7 @@ void handleRoot() { digitalWrite(iopinB,!digitalRead(iopinB)); // toggle output pin on/off } - // if button3 was pressed (toggle flash LED) + // if button2 was pressed (toggle flash LED) if (server.hasArg("button2")) { if (serialDebug) Serial.println("Button 2 pressed"); if (brightLEDbrightness == 0) brightLed(10); // turn led on dim @@ -726,7 +760,7 @@ void handleRoot() { else brightLed(0); // turn led off } - // if button4 was pressed (format SPIFFS) + // if button3 was pressed (format SPIFFS) if (server.hasArg("button3")) { if (serialDebug) Serial.println("Button 3 pressed"); if (!SPIFFS.format()) { @@ -736,6 +770,12 @@ void handleRoot() { } } + // if button4 was pressed (change resolution) + if (server.hasArg("button4")) { + if (serialDebug) Serial.println("Button 4 pressed"); + changeResolution(); // cycle through some options + } + // if exposure was adjusted - cameraImageExposure if (server.hasArg("exp")) { if (serialDebug) Serial.println("Exposure has been changed"); @@ -807,7 +847,8 @@ void handleRoot() { // Control bottons client.write(" \n"); client.write(" \n"); - client.write("
\n"); + client.write(" \n"); + client.write("
\n"); // Image setting controls client.write("
CAMERA SETTINGS: \n"); @@ -1062,7 +1103,10 @@ void readRGBImage() { 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 (!sendRGBfile) sendFooter(client); // close web page + if (!sendRGBfile) { + client.write("
Return\n"); // link back + sendFooter(client); // close web page + } return; } ptrVal = heap_caps_malloc(ARRAY_LENGTH, MALLOC_CAP_SPIRAM); // allocate memory space for the rgb data @@ -1074,7 +1118,10 @@ void readRGBImage() { bool jpeg_converted = fmt2rgb888(fb->buf, fb->len, PIXFORMAT_JPEG, rgb); if (!jpeg_converted) { sendText(client,"error: failed to convert image to RGB data"); - if (!sendRGBfile) sendFooter(client); // close web page + if (!sendRGBfile) { + client.write("
Return\n"); // link back + sendFooter(client); // close web page + } return; } sendText(client, "Conversion from jpg to RGB took " + String(millis() - tTimer) + " milliseconds");// report how long the conversion took @@ -1336,40 +1383,31 @@ void handleTest() { // demo of drawing on the camera image using javascript / html canvas -// could be of use to show area of interest on the image etc. +// could be of use to show area of interest on the image etc. - see https://www.w3schools.com/html/html5_canvas.asp // creat a DIV and put image in it with a html canvas on top of it - int tWidth = 640; // image dimensions on web page - int tHeight = 480; + int imageWidth = 640; // image dimensions on web page + int imageHeight = 480; client.println("
"); - client.println(""); - client.println(""); + client.println(""); + client.println(""); client.println("
"); // javascript to draw on the canvas - client.print (R"=====(\n)====="); -/* -// demo of how to change image resolution - note: this stops PWM on the flash working for some reason - esp_camera_deinit(); // disable camera - delay(50); - // cycle through a selection of resolutions - if (FRAME_SIZE_IMAGE == FRAMESIZE_QVGA) FRAME_SIZE_IMAGE = FRAMESIZE_VGA; - else if (FRAME_SIZE_IMAGE == FRAMESIZE_VGA) FRAME_SIZE_IMAGE = FRAMESIZE_XGA; - else if (FRAME_SIZE_IMAGE == FRAMESIZE_XGA) FRAME_SIZE_IMAGE = FRAMESIZE_SXGA; - else FRAME_SIZE_IMAGE = FRAMESIZE_QVGA; - initialiseCamera(); - client.println("Camera resolution changed to " + String(FRAME_SIZE_IMAGE)); -*/ - - /* // demo of how to request a web page String page = "http://urlhere.com"; // url to request