Porównaj commity

...

4 Commity

Autor SHA1 Wiadomość Data
Alan 6121a28752
Update ESP32cam-demo.ino 2023-10-14 10:20:33 +01:00
Alan e6b72fd8bc
Update ESP32cam-demo.ino 2023-10-14 09:45:13 +01:00
Alan e43abd27a1
Update README.md 2023-10-14 09:35:26 +01:00
Alan ffa32a7f4f
Update ESP32cam-demo.ino 2023-10-14 09:33:48 +01:00
2 zmienionych plików z 100 dodań i 92 usunięć

Wyświetl plik

@ -33,6 +33,7 @@
* esp32cam-demo is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the * esp32cam-demo is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* *
* https://alanesq.github.io/
* *
*******************************************************************************************************************/ *******************************************************************************************************************/
@ -47,46 +48,46 @@
// Enter your Wifi Settings here // Enter your Wifi Settings here
#define SSID_NAME "<your wifi ssid here>" #define SSID_NAME "<wifi name>"
#define SSID_PASWORD "<your wifi password here>" #define SSID_PASWORD "<wifi password>"
// --------------------------------------------------------------------------------------------------------- // ---------------------------------------------------------------------------------------------------------
/*
// Required by PlatformIO // Required by PlatformIO
#include <Arduino.h> #include <Arduino.h>
// forward declarations // forward declarations
bool initialiseCamera(); bool initialiseCamera(int);
bool cameraImageSettings(); bool cameraImageSettings();
String localTime(); String localTime();
void flashLED(int reps); void flashLED(int);
byte storeImage(); byte storeImage();
void handleRoot(); void handleRoot();
void handlePhoto(); void handlePhoto();
bool handleImg(); bool handleImg();
void handleNotFound(); void handleNotFound();
void readRGBImage(); void readRGBImage();
bool getNTPtime(int sec); bool getNTPtime(int);
bool handleJPG(); bool handleJPG();
bool handleJpeg(); void handleJpeg();
void handleStream(); void handleStream();
int requestWebPage(String*, String*, int); int requestWebPage(String*, String*, int);
void handleTest(); void handleTest();
void brightLed(byte ledBrightness); void brightLed(byte);
void setupFlashPWM(); void setupFlashPWM();
void changeResolution(framesize_t); void changeResolution(framesize_t);
void handleData(); void handleData();
*/ void readGreyscaleImage();
// --------------------------------------------------------------- // ---------------------------------------------------------------
// -SETTINGS // -SETTINGS
// --------------------------------------------------------------- // ---------------------------------------------------------------
char* stitle = "ESP32Cam-demo"; // title of this sketch char* stitle = "ESP32Cam-demo"; // title of this sketch
char* sversion = "28Jul23"; // Sketch version char* sversion = "14oct23"; // 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 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
@ -95,7 +96,7 @@
const bool serialDebug = 1; // show debug info. on serial port (1=enabled, disable if using pins 1 and 3 as gpio) const bool serialDebug = 1; // show debug info. on serial port (1=enabled, disable if using pins 1 and 3 as gpio)
#define useMCP23017 0 // if MCP23017 IO expander chip is being used (on pins 12 and 13) #define useMCP23017 0 // set if MCP23017 IO expander chip is being used (on pins 12 and 13)
// Camera related // Camera related
bool flashRequired = 1; // If flash to be used when capturing image (1 = yes) bool flashRequired = 1; // If flash to be used when capturing image (1 = yes)
@ -104,7 +105,6 @@
// 160x120 (QQVGA), 128x160 (QQVGA2), 176x144 (QCIF), 240x176 (HQVGA), // 160x120 (QQVGA), 128x160 (QQVGA2), 176x144 (QCIF), 240x176 (HQVGA),
// 320x240 (QVGA), 400x296 (CIF), 640x480 (VGA, default), 800x600 (SVGA), // 320x240 (QVGA), 400x296 (CIF), 640x480 (VGA, default), 800x600 (SVGA),
// 1024x768 (XGA), 1280x1024 (SXGA), 1600x1200 (UXGA) // 1024x768 (XGA), 1280x1024 (SXGA), 1600x1200 (UXGA)
#define PIXFORMAT PIXFORMAT_JPEG; // image format, Options = YUV422, GRAYSCALE, RGB565, JPEG, RGB888
int cameraImageExposure = 0; // Camera exposure (0 - 1200) If gain and exposure both set to zero then auto adjust is enabled int cameraImageExposure = 0; // Camera exposure (0 - 1200) If gain and exposure both set to zero then auto adjust is enabled
int cameraImageGain = 0; // Image gain (0 - 30) int cameraImageGain = 0; // Image gain (0 - 30)
int cameraImageBrightness = 0; // Image brightness (-2 to +2) int cameraImageBrightness = 0; // Image brightness (-2 to +2)
@ -150,6 +150,7 @@
// ****************************************************************************************************************** // ******************************************************************************************************************
//#include "esp_camera.h" // https://github.com/espressif/esp32-camera //#include "esp_camera.h" // https://github.com/espressif/esp32-camera
// #include "camera_pins.h" // #include "camera_pins.h"
#include <WString.h> // this is required for base64.h otherwise get errors with esp32 core 1.0.6 - jan23 #include <WString.h> // this is required for base64.h otherwise get errors with esp32 core 1.0.6 - jan23
@ -199,9 +200,6 @@ WebServer server(80); // serve web pages on port 80
String ImageResDetails = "Unknown"; // image resolution info String ImageResDetails = "Unknown"; // image resolution info
// ******************************************************************************************************************
// --------------------------------------------------------------- // ---------------------------------------------------------------
// -SETUP SETUP SETUP SETUP SETUP SETUP // -SETUP SETUP SETUP SETUP SETUP SETUP
// --------------------------------------------------------------- // ---------------------------------------------------------------
@ -254,6 +252,7 @@ void setup() {
server.on("/photo", handlePhoto); // save image to sd card server.on("/photo", handlePhoto); // save image to sd card
server.on("/img", handleImg); // show image from sd card server.on("/img", handleImg); // show image from sd card
server.on("/rgb", readRGBImage); // demo converting image to RGB server.on("/rgb", readRGBImage); // demo converting image to RGB
server.on("/greydata", readGreyscaleImage); // look at greyscale image data
server.on("/test", handleTest); // Testing procedure server.on("/test", handleTest); // Testing procedure
server.on("/reboot", handleReboot); // restart device server.on("/reboot", handleReboot); // restart device
server.onNotFound(handleNotFound); // invalid url requested server.onNotFound(handleNotFound); // invalid url requested
@ -270,7 +269,7 @@ void setup() {
// set up camera // set up camera
if (serialDebug) Serial.print(("\nInitialising camera: ")); if (serialDebug) Serial.print(("\nInitialising camera: "));
if (initialiseCamera()) { if (initialiseCamera(0)) {
if (serialDebug) Serial.println("OK"); if (serialDebug) Serial.println("OK");
} }
else { else {
@ -362,9 +361,6 @@ setupFlashPWM(); // configure PWM for the illumination LED
} // setup } // setup
// ******************************************************************************************************************
// ---------------------------------------------------------------- // ----------------------------------------------------------------
// -LOOP LOOP LOOP LOOP LOOP LOOP LOOP // -LOOP LOOP LOOP LOOP LOOP LOOP LOOP
// ---------------------------------------------------------------- // ----------------------------------------------------------------
@ -403,16 +399,14 @@ void loop() {
} // loop } // loop
// ******************************************************************************************************************
// ---------------------------------------------------------------- // ----------------------------------------------------------------
// Initialise the camera // Initialise the camera
// ---------------------------------------------------------------- // ----------------------------------------------------------------
// returns TRUE if successful // returns TRUE if successful
// custom:
// 1 = greyscale image format (defailt is JPG)
bool initialiseCamera() { bool initialiseCamera(int custom = 0) {
camera_config_t config; camera_config_t config;
config.ledc_channel = LEDC_CHANNEL_0; config.ledc_channel = LEDC_CHANNEL_0;
@ -434,12 +428,15 @@ bool initialiseCamera() {
config.pin_pwdn = PWDN_GPIO_NUM; config.pin_pwdn = PWDN_GPIO_NUM;
config.pin_reset = RESET_GPIO_NUM; config.pin_reset = RESET_GPIO_NUM;
config.xclk_freq_hz = 20000000; // XCLK 20MHz or 10MHz for OV2640 double FPS (Experimental) config.xclk_freq_hz = 20000000; // XCLK 20MHz or 10MHz for OV2640 double FPS (Experimental)
config.pixel_format = PIXFORMAT; // Options = YUV422, GRAYSCALE, RGB565, JPEG, RGB888 // image format
config.frame_size = FRAME_SIZE_IMAGE; // Image sizes: 160x120 (QQVGA), 128x160 (QQVGA2), 176x144 (QCIF), 240x176 (HQVGA), 320x240 (QVGA), if (custom == 1) config.pixel_format = PIXFORMAT_GRAYSCALE; // greyscale format
// 400x296 (CIF), 640x480 (VGA, default), 800x600 (SVGA), 1024x768 (XGA), 1280x1024 (SXGA), else config.pixel_format = PIXFORMAT_JPEG; // colour jpg format
// 1600x1200 (UXGA) // image size
config.jpeg_quality = 12; // 0-63 lower number means higher quality (can cause failed image capture if set too low at higher resolutions) config.frame_size = FRAME_SIZE_IMAGE; // Image sizes: 160x120 (QQVGA), 128x160 (QQVGA2), 176x144 (QCIF), 240x176 (HQVGA), 320x240 (QVGA),
config.fb_count = 1; // if more than one, i2s runs in continuous mode. Use only with JPEG // 400x296 (CIF), 640x480 (VGA, default), 800x600 (SVGA), 1024x768 (XGA), 1280x1024 (SXGA),
// 1600x1200 (UXGA)
config.jpeg_quality = 12; // 0-63 lower number means higher quality (can cause failed image capture if set too low at higher resolutions)
config.fb_count = 1; // if more than one, i2s runs in continuous mode. Use only with JPEG
// check the esp32cam board has a psram chip installed (extra memory used for storing captured images) // check the esp32cam board has a psram chip installed (extra memory used for storing captured images)
// Note: if not using "AI thinker esp32 cam" in the Arduino IDE, SPIFFS must be enabled // Note: if not using "AI thinker esp32 cam" in the Arduino IDE, SPIFFS must be enabled
@ -458,15 +455,12 @@ bool initialiseCamera() {
if (serialDebug) Serial.printf("ERROR: Camera init failed with error 0x%x", camerr); if (serialDebug) Serial.printf("ERROR: Camera init failed with error 0x%x", camerr);
} }
cameraImageSettings(); // apply custom camera settings cameraImageSettings(); // apply custom camera image settings
return (camerr == ESP_OK); // return boolean result of camera initialisation return (camerr == ESP_OK); // return boolean result of camera initialisation
} }
// ******************************************************************************************************************
// ---------------------------------------------------------------- // ----------------------------------------------------------------
// -Change camera image settings // -Change camera image settings
// ---------------------------------------------------------------- // ----------------------------------------------------------------
@ -537,12 +531,6 @@ bool cameraImageSettings() {
// s->set_wpc(s, 0); // white pixel correction // s->set_wpc(s, 0); // white pixel correction
// ******************************************************************************************************************
// Misc small procedures
// ---------------------------------------------------------------- // ----------------------------------------------------------------
// set up PWM for the illumination LED (flash) // set up PWM for the illumination LED (flash)
// ---------------------------------------------------------------- // ----------------------------------------------------------------
@ -662,12 +650,12 @@ void resetCamera(bool type = 0) {
delay(300); delay(300);
digitalWrite(PWDN_GPIO_NUM, LOW); digitalWrite(PWDN_GPIO_NUM, LOW);
delay(300); delay(300);
initialiseCamera(); initialiseCamera(0);
} else { } else {
// reset via software (handy if you wish to change resolution or image type etc. - see test procedure) // reset via software (handy if you wish to change resolution or image type etc. - see test procedure)
esp_camera_deinit(); esp_camera_deinit();
delay(50); delay(50);
initialiseCamera(); initialiseCamera(0);
} }
} }
@ -687,15 +675,13 @@ void changeResolution(framesize_t tRes = FRAMESIZE_96X96) {
else tRes = FRAMESIZE_QVGA; else tRes = FRAMESIZE_QVGA;
} }
FRAME_SIZE_IMAGE = tRes; FRAME_SIZE_IMAGE = tRes;
initialiseCamera();
initialiseCamera(0);
if (serialDebug) Serial.println("Camera resolution changed to " + String(tRes)); if (serialDebug) Serial.println("Camera resolution changed to " + String(tRes));
ImageResDetails = "Unknown"; // set next time image captured ImageResDetails = "Unknown"; // set next time image captured
} }
// ******************************************************************************************************************
// ---------------------------------------------------------------- // ----------------------------------------------------------------
// Capture image from camera and save to spiffs or sd card // Capture image from camera and save to spiffs or sd card
// ---------------------------------------------------------------- // ----------------------------------------------------------------
@ -745,6 +731,7 @@ byte storeImage() {
if (serialDebug) Serial.println("Error: failed to write image data to spiffs file"); if (serialDebug) Serial.println("Error: failed to write image data to spiffs file");
} }
} }
esp_camera_fb_return(fb); // return camera frame buffer
if (sRes == 1 && serialDebug) { if (sRes == 1 && serialDebug) {
Serial.print("The picture has been saved to Spiffs as " + spiffsFilename); Serial.print("The picture has been saved to Spiffs as " + spiffsFilename);
Serial.print(" - Size: "); Serial.print(" - Size: ");
@ -781,9 +768,6 @@ byte storeImage() {
} // storeImage } // storeImage
// ******************************************************************************************************************
// ---------------------------------------------------------------- // ----------------------------------------------------------------
// -Action any user input on root web page // -Action any user input on root web page
// ---------------------------------------------------------------- // ----------------------------------------------------------------
@ -1051,9 +1035,6 @@ void handleData(){
} }
// ******************************************************************************************************************
// ---------------------------------------------------------------- // ----------------------------------------------------------------
// -photo save to sd card/spiffs i.e. http://x.x.x.x/photo // -photo save to sd card/spiffs i.e. http://x.x.x.x/photo
// ---------------------------------------------------------------- // ----------------------------------------------------------------
@ -1170,9 +1151,6 @@ bool handleImg() {
} // handleImg } // handleImg
// ******************************************************************************************************************
// ---------------------------------------------------------------- // ----------------------------------------------------------------
// -invalid web page requested // -invalid web page requested
// ---------------------------------------------------------------- // ----------------------------------------------------------------
@ -1203,9 +1181,6 @@ void handleNotFound() {
} // handleNotFound } // handleNotFound
// ******************************************************************************************************************
// ---------------------------------------------------------------- // ----------------------------------------------------------------
// -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
// ---------------------------------------------------------------- // ----------------------------------------------------------------
@ -1353,10 +1328,6 @@ void readRGBImage() {
} // readRGBImage } // readRGBImage
// ******************************************************************************************************************
// ---------------------------------------------------------------- // ----------------------------------------------------------------
// -get time from ntp server // -get time from ntp server
// ---------------------------------------------------------------- // ----------------------------------------------------------------
@ -1421,12 +1392,11 @@ bool handleJPG() {
// send the captured jpg data // send the captured jpg data
client.write((char *)fb->buf, fb->len); client.write((char *)fb->buf, fb->len);
// close client connection // close network client connection
delay(3); delay(3);
client.stop(); client.stop();
// return image frame so memory can be released esp_camera_fb_return(fb); // return camera frame buffer
esp_camera_fb_return(fb);
return 1; return 1;
@ -1475,7 +1445,7 @@ void handleStream(){
client.write(buf, strlen(buf)); // send result (image size) client.write(buf, strlen(buf)); // send result (image size)
client.write((char *)fb->buf, fb->len); // send the image data client.write((char *)fb->buf, fb->len); // send the image data
client.write(BOUNDARY, bdrLen); // send html boundary see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type 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 esp_camera_fb_return(fb); // return camera frame buffer
} }
} }
@ -1487,9 +1457,6 @@ void handleStream(){
} // handleStream } // handleStream
// ******************************************************************************************************************
// ---------------------------------------------------------------- // ----------------------------------------------------------------
// request a web page // request a web page
// ---------------------------------------------------------------- // ----------------------------------------------------------------
@ -1526,9 +1493,6 @@ int requestWebPage(String* page, String* received, int maxWaitTime=5000){
} // requestWebPage } // requestWebPage
// ******************************************************************************************************************
// ---------------------------------------------------------------- // ----------------------------------------------------------------
// -show refreshing image i.e. http://x.x.x.x/jpeg // -show refreshing image i.e. http://x.x.x.x/jpeg
// ---------------------------------------------------------------- // ----------------------------------------------------------------
@ -1567,11 +1531,72 @@ void handleJpeg() {
sendFooter(client); // close web page sendFooter(client); // close web page
} // handleJpeg } // handleJpeg
// ****************************************************************************************************************** // ----------------------------------------------------------------
// Capture greyscale image data
// ----------------------------------------------------------------
void readGreyscaleImage() {
WiFiClient client = server.client(); // open link with client
// change camera to greyscale mode (as by default it is in JPG colour mode)
esp_camera_deinit(); // disable camera
delay(50);
initialiseCamera(1); // restart the camera with greyscale custom setting (1)
// capture the image and use flash if required
int currentBrightness = brightLEDbrightness;
if (flashRequired) {
brightLed(255); // change LED brightness (0 - 255)
delay(100);
}
camera_fb_t *fb = esp_camera_fb_get(); // capture image
if (flashRequired){
delay(100);
brightLed(currentBrightness); // change LED brightness back to previous state
}
if (!fb) client.println("Error: Camera image capture failed");
// read image data and calculate average pixel value (as demonstration of reading the image data)
unsigned long dataSize = fb->width * fb->height;
unsigned long avrg = 0;
for (int i=0; i < dataSize; i++) { // Note: pixels x position = i % fb->width y position = floor(i / fb->width)
avrg += fb->buf[i];
}
client.println("<br>Greyscale Image: Average pixel = " + String(avrg / dataSize));
// close network client connection
delay(3);
client.stop();
// return image frame to free up memory
esp_camera_fb_return(fb); // return camera frame buffer
// change camera back to JPG mode
esp_camera_deinit();
delay(50);
initialiseCamera(0); // default settings (0)
}
// ----------------------------------------------------------------
// -reboot web page requested i.e. http://x.x.x.x/reboot
// ----------------------------------------------------------------
// note: this can fail if the esp has just been reflashed and not restarted
void handleReboot(){
String message = "Rebooting....";
server.send(200, "text/plain", message); // send reply as plain text
// rebooting
delay(500); // give time to send the above html
ESP.restart();
delay(5000); // restart fails without this delay
}
// ---------------------------------------------------------------- // ----------------------------------------------------------------
@ -1666,23 +1691,5 @@ void handleTest() {
} // handleTest } // handleTest
// ----------------------------------------------------------------
// -reboot web page requested i.e. http://x.x.x.x/reboot
// ----------------------------------------------------------------
// note: this can fail if the esp has just been reflashed and not restarted
void handleReboot(){
String message = "Rebooting....";
server.send(200, "text/plain", message); // send reply as plain text
// rebooting
delay(500); // give time to send the above html
ESP.restart();
delay(5000); // restart fails without this delay
}
// ****************************************************************************************************************** // ******************************************************************************************************************
// end // end

Wyświetl plik

@ -91,6 +91,7 @@ URLs:
<br>http://x.x.x.x/stream Show live streaming video <br>http://x.x.x.x/stream Show live streaming video
<br>http://x.x.x.x/img Show most recent image saved to sd card <br>http://x.x.x.x/img Show most recent image saved to sd card
<br>http://x.x.x.x/img?img=1 Show image number 1 on sd card <br>http://x.x.x.x/img?img=1 Show image number 1 on sd card
<br>http://x.x.x.x/rgb Show how to capture a greyscale image and look at the raw data
<br>http://x.x.x.x/rgb Captures an image and converts to RGB data (will not work with the highest <br>http://x.x.x.x/rgb Captures an image and converts to RGB data (will not work with the highest
resolution images as there is not enough memory) resolution images as there is not enough memory)