initial commit

This commit is contained in:
Jon Roeber 2022-08-29 21:54:40 -04:00
commit d92fa52f5d
8 changed files with 317 additions and 0 deletions

5
.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
.pio
.vscode/.browse.c_cpp.db*
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch

10
.vscode/extensions.json vendored Normal file
View File

@ -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"
]
}

11
README.md Normal file
View File

@ -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 <IP Address>` to take and save a picture to your local machine.

39
include/README Normal file
View File

@ -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

46
lib/README Normal file
View File

@ -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 <Foo.h>
#include <Bar.h>
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

19
platformio.ini Normal file
View File

@ -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

176
src/main.cpp Normal file
View File

@ -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 <LittleFS.h>
// 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);
}

11
test/README Normal file
View File

@ -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