Skip to content

Commit 29d7600

Browse files
author
Raphael Brandão
committed
add sd_jpg example
1 parent 6f0994c commit 29d7600

10 files changed

+239
-2
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# ESP32-CAM - A.I. Thinker
1+
# ESP32-CAM - A.I. Thinker
22

33
The goal of this repository is to centralize information and examples for the ESP32-CAM A.I. Thinker.
44

@@ -36,6 +36,7 @@ This board and the included OV2640 can acomplish the following:
3636
# Flashing
3737

3838
## Requirements
39+
3940
To flash the board you need a USB to TTL dongle.
4041

4142
Also it is very important to **use a external power supply**. USBs usually do not provide sufficient current which makes the board loops `brownout` errors.
@@ -62,6 +63,7 @@ This board also has a built in reset button.
6263
To run the examples, make sure to have `xtensa` and `esp-idf` configured in your machine. If you haven't, refer to [this tutorial](https://docs.espressif.com/projects/esp-idf/en/latest/get-started/index.html).
6364

6465
- [JPG via Http server](./examples/http_jpg)
66+
- [Store pictures in SD](./examples/sd_jpg)
6567

6668
# Contributions
6769

examples/http_jpg/README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Example http_jpg
22

3+
Creates a http server and listen to `GET` requests at `http://[board-ip]/jpg`. When the request is triggered, it returns a UXGA JPEG image from the camera.
4+
35
## Instructions
46

57
In order to run this example, you need to update your `esp-idf` repository to the main branch because it relies uppon `esp_http_server` which aren't released to this date.
@@ -12,7 +14,7 @@ All camera pins are configured by default accordingly to [this A.I. Thinker docu
1214

1315
## Notes
1416

15-
Make sure to read [sdconfig.defaults](./sdconfig.defaults) file to get a grasp of required configurations to enable `PSRAM` and set it to 64MBit.
17+
Make sure to read [sdconfig.defaults](./sdconfig.defaults) file to get a grasp of required configurations to enable `PSRAM` and set it to `64MBit`.
1618

1719
## Demo
1820

examples/sd_jpg/CMakeLists.txt

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# The following lines of boilerplate have to be in your project's CMakeLists
2+
# in this exact order for cmake to work correctly
3+
cmake_minimum_required(VERSION 3.5)
4+
5+
set(EXTRA_COMPONENT_DIRS
6+
$(abspath ../../components)
7+
)
8+
9+
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
10+
project(esp32_cam_sd_jpg)

examples/sd_jpg/Makefile

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
PROJECT_NAME := esp32_cam_sd_jpg
2+
3+
EXTRA_COMPONENT_DIRS := $(abspath ../../components)
4+
5+
include $(IDF_PATH)/make/project.mk

examples/sd_jpg/README.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Example sd_jpg
2+
3+
Take pictures with intervals of 20 seconds and store then to SD card.
4+
5+
## Instructions
6+
7+
In order to run this example, format any MicroSD card with FAT and insert it to the board.
8+
9+
All camera pins are configured by default accordingly to [this A.I. Thinker document](../../assets/ESP32-CAM_Product_Specification.pdf) and you can check then inside [Kconfig.projbuild](./main/Kconfig.projbuild).

examples/sd_jpg/main/CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
set(COMPONENT_SRCS "main.c")
2+
set(COMPONENT_ADD_INCLUDEDIRS ".")
3+
4+
register_component()
+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
menu "Http_jpg example configuration"
2+
3+
config XCLK_FREQ
4+
int "XCLK Frequency"
5+
default "20000000"
6+
help
7+
The XCLK Frequency in Herz.
8+
9+
menu "Pin Configuration"
10+
config D0
11+
int "D0"
12+
default "5"
13+
config D1
14+
int "D1"
15+
default "18"
16+
config D2
17+
int "D2"
18+
default "19"
19+
config D3
20+
int "D3"
21+
default "21"
22+
config D4
23+
int "D4"
24+
default "36"
25+
config D5
26+
int "D5"
27+
default "39"
28+
config D6
29+
int "D6"
30+
default "34"
31+
config D7
32+
int "D7"
33+
default "35"
34+
config XCLK
35+
int "XCLK"
36+
default "0"
37+
config PCLK
38+
int "PCLK"
39+
default "22"
40+
config VSYNC
41+
int "VSYNC"
42+
default "25"
43+
config HREF
44+
int "HREF"
45+
default "23"
46+
config SDA
47+
int "SDA"
48+
default "26"
49+
config SCL
50+
int "SCL"
51+
default "27"
52+
config RESET
53+
int "RESET"
54+
default "32"
55+
endmenu
56+
57+
endmenu

examples/sd_jpg/main/component.mk

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#
2+
# "main" pseudo-component makefile.
3+
#
4+
# (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.)

examples/sd_jpg/main/main.c

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
2+
#include <esp_event_loop.h>
3+
#include <esp_log.h>
4+
#include <esp_system.h>
5+
#include <nvs_flash.h>
6+
#include <sys/param.h>
7+
8+
#include "freertos/FreeRTOS.h"
9+
#include "freertos/task.h"
10+
11+
#include "driver/sdmmc_host.h"
12+
#include "driver/sdmmc_defs.h"
13+
#include "sdmmc_cmd.h"
14+
#include "esp_vfs_fat.h"
15+
16+
#include "esp_camera.h"
17+
18+
static const char *TAG = "example:sd_jpg";
19+
20+
static camera_config_t camera_config = {
21+
.pin_pwdn = -1,
22+
.pin_reset = CONFIG_RESET,
23+
.pin_xclk = CONFIG_XCLK,
24+
.pin_sscb_sda = CONFIG_SDA,
25+
.pin_sscb_scl = CONFIG_SCL,
26+
27+
.pin_d7 = CONFIG_D7,
28+
.pin_d6 = CONFIG_D6,
29+
.pin_d5 = CONFIG_D5,
30+
.pin_d4 = CONFIG_D4,
31+
.pin_d3 = CONFIG_D3,
32+
.pin_d2 = CONFIG_D2,
33+
.pin_d1 = CONFIG_D1,
34+
.pin_d0 = CONFIG_D0,
35+
.pin_vsync = CONFIG_VSYNC,
36+
.pin_href = CONFIG_HREF,
37+
.pin_pclk = CONFIG_PCLK,
38+
39+
//XCLK 20MHz or 10MHz
40+
.xclk_freq_hz = CONFIG_XCLK_FREQ,
41+
.ledc_timer = LEDC_TIMER_0,
42+
.ledc_channel = LEDC_CHANNEL_0,
43+
44+
.pixel_format = PIXFORMAT_JPEG, //YUV422,GRAYSCALE,RGB565,JPEG
45+
.frame_size = FRAMESIZE_UXGA, //QQVGA-UXGA Do not use sizes above QVGA when not JPEG
46+
47+
.jpeg_quality = 12, //0-63 lower number means higher quality
48+
.fb_count = 1 //if more than one, i2s runs in continuous mode. Use only with JPEG
49+
};
50+
51+
static esp_err_t init_camera()
52+
{
53+
//initialize the camera
54+
esp_err_t err = esp_camera_init(&camera_config);
55+
if (err != ESP_OK)
56+
{
57+
ESP_LOGE(TAG, "Camera Init Failed");
58+
return err;
59+
}
60+
61+
return ESP_OK;
62+
}
63+
64+
static void init_sdcard()
65+
{
66+
esp_err_t ret = ESP_FAIL;
67+
sdmmc_host_t host = SDMMC_HOST_DEFAULT();
68+
sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
69+
esp_vfs_fat_sdmmc_mount_config_t mount_config = {
70+
.format_if_mount_failed = false,
71+
.max_files = 3,
72+
};
73+
sdmmc_card_t *card;
74+
75+
ESP_LOGI(TAG, "Mounting SD card...");
76+
ret = esp_vfs_fat_sdmmc_mount("/sdcard", &host, &slot_config, &mount_config, &card);
77+
78+
if (ret == ESP_OK)
79+
{
80+
ESP_LOGI(TAG, "SD card mount successfully!");
81+
}
82+
else
83+
{
84+
ESP_LOGE(TAG, "Failed to mount SD card VFAT filesystem. Error: %s", esp_err_to_name(ret));
85+
}
86+
}
87+
88+
void app_main()
89+
{
90+
init_sdcard();
91+
init_camera();
92+
93+
while (1)
94+
{
95+
ESP_LOGI(TAG, "Taking picture...");
96+
camera_fb_t *pic = esp_camera_fb_get();
97+
int64_t timestamp = esp_timer_get_time();
98+
99+
char *pic_name = malloc(17 + sizeof(int64_t));
100+
sprintf(pic_name, "/sdcard/pic_%lli.jpg", timestamp);
101+
FILE *file = fopen(pic_name, "w");
102+
if (file != NULL)
103+
{
104+
size_t err = fwrite(pic->buf, 1, pic->len, file);
105+
ESP_LOGI(TAG, "File saved: %s", pic_name);
106+
}
107+
else
108+
{
109+
ESP_LOGE(TAG, "Could not open file =(");
110+
}
111+
fclose(file);
112+
free(pic_name);
113+
114+
vTaskDelay(20000 / portTICK_RATE_MS);
115+
}
116+
}

examples/sd_jpg/sdkconfig.defaults

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#
2+
# Compiler options DEBUG
3+
#
4+
CONFIG_OPTIMIZATION_LEVEL_DEBUG=y
5+
CONFIG_OPTIMIZATION_ASSERTIONS_ENABLED=y
6+
CONFIG_STACK_CHECK_ALL=y
7+
CONFIG_STACK_CHECK=y
8+
CONFIG_WARN_WRITE_STRINGS=y
9+
CONFIG_LOG_DEFAULT_LEVEL_DEBUG=y
10+
11+
#
12+
# Serial flasher config
13+
#
14+
CONFIG_ESPTOOLPY_BAUD_230400B=y
15+
16+
#
17+
# ESP32-specific
18+
#
19+
CONFIG_SPIRAM_SUPPORT=y
20+
CONFIG_SPIRAM_IGNORE_NOTFOUND=y
21+
CONFIG_WIFI_LWIP_ALLOCATION_FROM_SPIRAM_FIRST=y
22+
CONFIG_SPIRAM_TYPE_ESPPSRAM64=y
23+
24+
# Enable FATFS read only with long filename support
25+
# for loading Cert/CA/etc from filesystem
26+
# (if enabled in config)
27+
CONFIG_FATFS_CODEPAGE_437=y
28+
CONFIG_FATFS_LFN_HEAP=y

0 commit comments

Comments
 (0)