From d92fa52f5d9b3a5056c035dd86335baab2ef58e8 Mon Sep 17 00:00:00 2001 From: Jon Roeber Date: Mon, 29 Aug 2022 21:54:40 -0400 Subject: [PATCH] initial commit --- .gitignore | 5 ++ .vscode/extensions.json | 10 +++ README.md | 11 +++ include/README | 39 +++++++++ lib/README | 46 +++++++++++ platformio.ini | 19 +++++ src/main.cpp | 176 ++++++++++++++++++++++++++++++++++++++++ test/README | 11 +++ 8 files changed, 317 insertions(+) create mode 100644 .gitignore create mode 100644 .vscode/extensions.json create mode 100644 README.md create mode 100644 include/README create mode 100644 lib/README create mode 100644 platformio.ini create mode 100644 src/main.cpp create mode 100644 test/README diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..89cc49c --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +.pio +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ipch diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..080e70d --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,10 @@ +{ + // See http://go.microsoft.com/fwlink/?LinkId=827846 + // for the documentation about the extensions.json format + "recommendations": [ + "platformio.platformio-ide" + ], + "unwantedRecommendations": [ + "ms-vscode.cpptools-extension-pack" + ] +} diff --git a/README.md b/README.md new file mode 100644 index 0000000..640aa85 --- /dev/null +++ b/README.md @@ -0,0 +1,11 @@ +# ESP32 Cam + +A basic program for getting pictures from the ESP32-cam over the network. + +## Instructions + +- Add your SSID/password to `src/main.cpp`. +- (Optional) [Set a static IP address](https://randomnerdtutorials.com/esp8266-nodemcu-static-fixed-ip-address-arduino/). +- Flash to the ESP32-cam with PlatformIO. +- Get the IP address from the serial output if you did not set a static IP. +- Run `curl -o test.jpg ` to take and save a picture to your local machine. diff --git a/include/README b/include/README new file mode 100644 index 0000000..194dcd4 --- /dev/null +++ b/include/README @@ -0,0 +1,39 @@ + +This directory is intended for project header files. + +A header file is a file containing C declarations and macro definitions +to be shared between several project source files. You request the use of a +header file in your project source file (C, C++, etc) located in `src` folder +by including it, with the C preprocessing directive `#include'. + +```src/main.c + +#include "header.h" + +int main (void) +{ + ... +} +``` + +Including a header file produces the same results as copying the header file +into each source file that needs it. Such copying would be time-consuming +and error-prone. With a header file, the related declarations appear +in only one place. If they need to be changed, they can be changed in one +place, and programs that include the header file will automatically use the +new version when next recompiled. The header file eliminates the labor of +finding and changing all the copies as well as the risk that a failure to +find one copy will result in inconsistencies within a program. + +In C, the usual convention is to give header files names that end with `.h'. +It is most portable to use only letters, digits, dashes, and underscores in +header file names, and at most one dot. + +Read more about using header files in official GCC documentation: + +* Include Syntax +* Include Operation +* Once-Only Headers +* Computed Includes + +https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html diff --git a/lib/README b/lib/README new file mode 100644 index 0000000..6debab1 --- /dev/null +++ b/lib/README @@ -0,0 +1,46 @@ + +This directory is intended for project specific (private) libraries. +PlatformIO will compile them to static libraries and link into executable file. + +The source code of each library should be placed in a an own separate directory +("lib/your_library_name/[here are source files]"). + +For example, see a structure of the following two libraries `Foo` and `Bar`: + +|--lib +| | +| |--Bar +| | |--docs +| | |--examples +| | |--src +| | |- Bar.c +| | |- Bar.h +| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html +| | +| |--Foo +| | |- Foo.c +| | |- Foo.h +| | +| |- README --> THIS FILE +| +|- platformio.ini +|--src + |- main.c + +and a contents of `src/main.c`: +``` +#include +#include + +int main (void) +{ + ... +} + +``` + +PlatformIO Library Dependency Finder will find automatically dependent +libraries scanning project source files. + +More information about PlatformIO Library Dependency Finder +- https://docs.platformio.org/page/librarymanager/ldf.html diff --git a/platformio.ini b/platformio.ini new file mode 100644 index 0000000..1bb9665 --- /dev/null +++ b/platformio.ini @@ -0,0 +1,19 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[env:esp32cam] +platform = espressif32 +board = esp32cam +board_build.partitions = default.csv +;board_build.partitions = min_spiffs.csv # required for OTA to work, see https://community.platformio.org/t/esp32-cam-ota-upload-problem/17179/15 +framework = arduino +monitor_speed = 115200 +;upload_protocol = espota +;upload_port = 192.168.0.190 diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..4a716b0 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,176 @@ +#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 +#define RESET_GPIO_NUM -1 +#define XCLK_GPIO_NUM 0 +#define SIOD_GPIO_NUM 26 +#define SIOC_GPIO_NUM 27 +#define Y9_GPIO_NUM 35 +#define Y8_GPIO_NUM 34 +#define Y7_GPIO_NUM 39 +#define Y6_GPIO_NUM 36 +#define Y5_GPIO_NUM 21 +#define Y4_GPIO_NUM 19 +#define Y3_GPIO_NUM 18 +#define Y2_GPIO_NUM 5 +#define VSYNC_GPIO_NUM 25 +#define HREF_GPIO_NUM 23 +#define PCLK_GPIO_NUM 22 + +// Replace with your network credentials +const char* ssid = "..."; +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 + // so use a different Timer for the LED flash. Use Timer 1 (Channel 2 / Channel 3). +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); + + // Connect to Wi-Fi + WiFi.begin(ssid, password); + while (WiFi.status() != WL_CONNECTED) { + 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://"); + Serial.println(WiFi.localIP()); + + // Turn off the 'brownout detector' + WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); + + // OV2640 camera module + camera_config_t config; + config.ledc_channel = LEDC_CHANNEL_0; + config.ledc_timer = LEDC_TIMER_0; + config.pin_d0 = Y2_GPIO_NUM; + config.pin_d1 = Y3_GPIO_NUM; + config.pin_d2 = Y4_GPIO_NUM; + config.pin_d3 = Y5_GPIO_NUM; + config.pin_d4 = Y6_GPIO_NUM; + config.pin_d5 = Y7_GPIO_NUM; + config.pin_d6 = Y8_GPIO_NUM; + config.pin_d7 = Y9_GPIO_NUM; + config.pin_xclk = XCLK_GPIO_NUM; + config.pin_pclk = PCLK_GPIO_NUM; + config.pin_vsync = VSYNC_GPIO_NUM; + config.pin_href = HREF_GPIO_NUM; + config.pin_sscb_sda = SIOD_GPIO_NUM; + config.pin_sscb_scl = SIOC_GPIO_NUM; + config.pin_pwdn = PWDN_GPIO_NUM; + config.pin_reset = RESET_GPIO_NUM; + config.xclk_freq_hz = 20000000; + config.pixel_format = PIXFORMAT_JPEG; + + config.frame_size = FRAMESIZE_HD; + config.jpeg_quality = 10; + config.fb_count = 1; + + // Camera init + esp_err_t err = esp_camera_init(&config); + if (err != ESP_OK) { + Serial.printf("Camera init failed with error 0x%x", err); + ESP.restart(); + } + + server.on("/", HTTP_GET, []() { + captureAndSave(); + File file = LittleFS.open(FILE_PHOTO); + server.streamFile(file, "image/jpeg"); + file.close(); + }); + + // Start server + server.begin(); +} + +void loop() { + server.handleClient(); + delay(1); +} diff --git a/test/README b/test/README new file mode 100644 index 0000000..9b1e87b --- /dev/null +++ b/test/README @@ -0,0 +1,11 @@ + +This directory is intended for PlatformIO Test Runner and project tests. + +Unit Testing is a software testing method by which individual units of +source code, sets of one or more MCU program modules together with associated +control data, usage procedures, and operating procedures, are tested to +determine whether they are fit for use. Unit testing finds problems early +in the development cycle. + +More information about PlatformIO Unit Testing: +- https://docs.platformio.org/en/latest/advanced/unit-testing/index.html