diff --git a/src/main.cpp b/src/main.cpp index 4a716b0..d85946e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,13 +1,8 @@ #include "WebServer.h" #include "WiFi.h" #include "esp_camera.h" -#include "esp_timer.h" -#include "img_converters.h" -#include "Arduino.h" #include "soc/soc.h" // Disable brownout problems #include "soc/rtc_cntl_reg.h" // Disable brownout problems -#include "driver/rtc_io.h" -#include // OV2640 camera module pins (CAMERA_MODEL_AI_THINKER) #define PWDN_GPIO_NUM 32 @@ -34,9 +29,6 @@ const char* password = "..."; // Create AsyncWebServer object on port 80 WebServer server(80); -// Photo File Name to save in LittleFS -#define FILE_PHOTO "/photo.jpg" - //LED flash setup parameters - the LED flash illuminates the meter when the photo is taken const int freq = 5000; const int ledChannel = 2; // Timer 0 is used to generate the camera pixel clock @@ -45,61 +37,6 @@ const int res = 8; // select 8-bit resolution const int ledPin = 4; // the flash LED is connected to pin 4 const int brightness = 255; // set the led PWM ( 0 - 255 ) -void captureAndSave( void ) { - camera_fb_t * fb = NULL; - bool ok = 0; - - do { - // Take a photo with the camera - Serial.println("Taking a photo..."); - - // Dump first frame - fb = esp_camera_fb_get(); - esp_camera_fb_return(fb); - fb = NULL; - - // Configure and turn on the LED flash - ledcAttachPin(ledPin, ledChannel); - ledcSetup(ledChannel, freq, res); - ledcWrite(ledChannel, brightness); - delay(1); - - fb = esp_camera_fb_get(); - - // Turn off the LED flash - ledcWrite(ledChannel, 0); - ledcDetachPin(ledPin); - - if (!fb) { - Serial.println("Camera capture failed"); - return; - } - - // Photo file name - File file = LittleFS.open(FILE_PHOTO, FILE_WRITE); - - // Insert the data in the photo file - if (!file) { - Serial.println("Failed to open file in writing mode"); - } - else { - file.write(fb->buf, fb->len); // payload (image), payload length - Serial.println("Photo saved"); - } - // Close the file - file.close(); - esp_camera_fb_return(fb); - - // check if file has been correctly saved in LittleFS - File f_pic = LittleFS.open( FILE_PHOTO ); - unsigned int pic_sz = f_pic.size(); - Serial.print("Saved file is "); - Serial.print(pic_sz); - Serial.println(" bytes"); - pic_sz > 100 ? ok = true : ok = false; - } while ( !ok ); -} - void setup() { Serial.begin(115200); @@ -109,14 +46,6 @@ void setup() { delay(1000); Serial.println("Connecting to WiFi..."); } - if (!LittleFS.begin(true)) { - Serial.println("An Error has occurred while mounting LittleFS"); - ESP.restart(); - } - else { - delay(500); - Serial.println("LittleFS mounted successfully"); - } // Print ESP32 Local IP Address Serial.print("IP Address: http://"); @@ -159,14 +88,32 @@ void setup() { ESP.restart(); } + // Take and send a picture when a client requests "/" server.on("/", HTTP_GET, []() { - captureAndSave(); - File file = LittleFS.open(FILE_PHOTO); - server.streamFile(file, "image/jpeg"); - file.close(); + camera_fb_t * fb = NULL; + + // Dump first frame due to framebuffer issues + fb = esp_camera_fb_get(); + esp_camera_fb_return(fb); + fb = NULL; + + // Configure and turn on the LED flash + ledcAttachPin(ledPin, ledChannel); + ledcSetup(ledChannel, freq, res); + ledcWrite(ledChannel, brightness); + delay(1); + + fb = esp_camera_fb_get(); + + // Turn off the LED flash + ledcWrite(ledChannel, 0); + ledcDetachPin(ledPin); + + server.send_P(200, "image/jpeg", (char *) fb->buf, fb->len); + + esp_camera_fb_return(fb); }); - // Start server server.begin(); }