Skip to content

Commit 6de16ea

Browse files
committed
chore: formatting and camera init
1 parent 312c7f3 commit 6de16ea

File tree

1 file changed

+127
-108
lines changed

1 file changed

+127
-108
lines changed

examples/Camera/snapshot-camera/snapshot-camera.ino

+127-108
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Example for how to use SinricPro Camera device:
33
* - Create a ESP32 Camera device from portal.
44
* - Copy the secrets below.
5-
*
5+
*
66
* If you encounter any issues:
77
* - check the readme.md at https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md
88
* - ensure all dependent libraries are installed
@@ -26,60 +26,59 @@
2626
// ===================
2727
// Select camera model
2828
// ===================
29-
//#define CAMERA_MODEL_WROVER_KIT // Has PSRAM
30-
//#define CAMERA_MODEL_ESP_EYE // Has PSRAM
31-
//#define CAMERA_MODEL_ESP32S3_EYE // Has PSRAM
32-
//#define CAMERA_MODEL_M5STACK_PSRAM // Has PSRAM
33-
//#define CAMERA_MODEL_M5STACK_V2_PSRAM // M5Camera version B Has PSRAM
34-
//#define CAMERA_MODEL_M5STACK_WIDE // Has PSRAM
35-
//#define CAMERA_MODEL_M5STACK_ESP32CAM // No PSRAM
36-
//#define CAMERA_MODEL_M5STACK_UNITCAM // No PSRAM
37-
//#define CAMERA_MODEL_M5STACK_CAMS3_UNIT // Has PSRAM
38-
//#define CAMERA_MODEL_AI_THINKER // Has PSRAM
39-
//#define CAMERA_MODEL_TTGO_T_JOURNAL // No PSRAM
40-
//#define CAMERA_MODEL_XIAO_ESP32S3 // Has PSRAM
29+
// #define CAMERA_MODEL_WROVER_KIT // Has PSRAM
30+
// #define CAMERA_MODEL_ESP_EYE // Has PSRAM
31+
// #define CAMERA_MODEL_ESP32S3_EYE // Has PSRAM
32+
// #define CAMERA_MODEL_M5STACK_PSRAM // Has PSRAM
33+
// #define CAMERA_MODEL_M5STACK_V2_PSRAM // M5Camera version B Has PSRAM
34+
// #define CAMERA_MODEL_M5STACK_WIDE // Has PSRAM
35+
// #define CAMERA_MODEL_M5STACK_ESP32CAM // No PSRAM
36+
// #define CAMERA_MODEL_M5STACK_UNITCAM // No PSRAM
37+
// #define CAMERA_MODEL_M5STACK_CAMS3_UNIT // Has PSRAM
38+
// #define CAMERA_MODEL_AI_THINKER // Has PSRAM
39+
// #define CAMERA_MODEL_TTGO_T_JOURNAL // No PSRAM
40+
// #define CAMERA_MODEL_XIAO_ESP32S3 // Has PSRAM
4141
// ** Espressif Internal Boards **
42-
//#define CAMERA_MODEL_ESP32_CAM_BOARD
43-
//#define CAMERA_MODEL_ESP32S2_CAM_BOARD
44-
//#define CAMERA_MODEL_ESP32S3_CAM_LCD
45-
//#define CAMERA_MODEL_DFRobot_FireBeetle2_ESP32S3 // Has PSRAM
46-
//#define CAMERA_MODEL_DFRobot_Romeo_ESP32S3 // Has PSRAM
47-
#include "camera_pins.h"
42+
// #define CAMERA_MODEL_ESP32_CAM_BOARD
43+
// #define CAMERA_MODEL_ESP32S2_CAM_BOARD
44+
// #define CAMERA_MODEL_ESP32S3_CAM_LCD
45+
// #define CAMERA_MODEL_DFRobot_FireBeetle2_ESP32S3 // Has PSRAM
46+
// #define CAMERA_MODEL_DFRobot_Romeo_ESP32S3 // Has PSRAM
47+
#include "camera_pins.h"
4848

4949
#define WIFI_SSID "YOUR-WIFI-SSID"
5050
#define WIFI_PASS "YOUR-WIFI-PASSWORD"
5151
#define APP_KEY "YOUR-APP-KEY" // Should look like "de0bxxxx-1x3x-4x3x-ax2x-5dabxxxxxxxx"
5252
#define APP_SECRET "YOUR-APP-SECRET" // Should look like "5f36xxxx-x3x7-4x3x-xexe-e86724a9xxxx-4c4axxxx-3x3x-x5xe-x9x3-333d65xxxxxx"
5353
#define CAMERA_ID "YOUR-ESP32-CAMERA-ID" // Should look like "5dc1564130xxxxxxxxxxxxxx"
54-
#define BAUD_RATE 115200 // Change baudrate to your need
55-
56-
bool onSnapshot(const String& deviceId) {
57-
camera_fb_t* fb = esp_camera_fb_get();
58-
59-
if (!fb) {
60-
Serial.println("Failed to grab image");
61-
return false;
62-
}
63-
64-
SinricProCamera& myCamera = SinricPro[deviceId];
65-
int result = myCamera.sendSnapshot(fb->buf, fb->len);
66-
esp_camera_fb_return(fb);
67-
68-
return result == 200;
54+
55+
#define BAUD_RATE 115200 // Change baudrate to your need
56+
57+
bool onSnapshot(const String &deviceId) {
58+
camera_fb_t *fb = esp_camera_fb_get();
59+
60+
if (!fb) {
61+
Serial.println("Failed to grab image");
62+
return false;
63+
}
64+
65+
SinricProCamera &myCamera = SinricPro[deviceId];
66+
int result = myCamera.sendSnapshot(fb->buf, fb->len);
67+
esp_camera_fb_return(fb);
68+
69+
return result == 200;
6970
}
7071

71-
bool onPowerState(const String& deviceId, bool& state) {
72-
return true;
72+
bool onPowerState(const String &deviceId, bool &state) {
73+
return true;
7374
}
7475

7576
// setup function for WiFi connection
7677
void setupWiFi() {
7778
Serial.printf("\r\n[Wifi]: Connecting");
7879

79-
WiFi.setSleep(false);
80-
WiFi.setAutoReconnect(true);
81-
82-
WiFi.begin(WIFI_SSID, WIFI_PASS);
80+
WiFi.begin(WIFI_SSID, WIFI_PASS);
81+
WiFi.setSleep(false);
8382

8483
while (WiFi.status() != WL_CONNECTED) {
8584
Serial.printf(".");
@@ -89,79 +88,99 @@ void setupWiFi() {
8988
}
9089

9190
void setupSinricPro() {
92-
SinricProCamera& myCamera = SinricPro[CAMERA_ID];
93-
myCamera.onPowerState(onPowerState);
94-
myCamera.onSnapshot(onSnapshot);
95-
SinricPro.onConnected([]() { Serial.printf("Connected to SinricPro\r\n"); });
96-
SinricPro.onDisconnected([]() { Serial.printf("Disconnected from SinricPro\r\n"); });
97-
98-
SinricPro.begin(APP_KEY, APP_SECRET);
91+
SinricProCamera &myCamera = SinricPro[CAMERA_ID];
92+
myCamera.onPowerState(onPowerState);
93+
myCamera.onSnapshot(onSnapshot);
94+
SinricPro.onConnected([]() {
95+
Serial.printf("Connected to SinricPro\r\n");
96+
});
97+
SinricPro.onDisconnected([]() {
98+
Serial.printf("Disconnected from SinricPro\r\n");
99+
});
100+
101+
SinricPro.begin(APP_KEY, APP_SECRET);
99102
}
100103

101-
esp_err_t setupCamera() {
102-
camera_config_t config;
103-
config.ledc_channel = LEDC_CHANNEL_0;
104-
config.ledc_timer = LEDC_TIMER_0;
105-
config.pin_d0 = Y2_GPIO_NUM;
106-
config.pin_d1 = Y3_GPIO_NUM;
107-
config.pin_d2 = Y4_GPIO_NUM;
108-
config.pin_d3 = Y5_GPIO_NUM;
109-
config.pin_d4 = Y6_GPIO_NUM;
110-
config.pin_d5 = Y7_GPIO_NUM;
111-
config.pin_d6 = Y8_GPIO_NUM;
112-
config.pin_d7 = Y9_GPIO_NUM;
113-
config.pin_xclk = XCLK_GPIO_NUM;
114-
config.pin_pclk = PCLK_GPIO_NUM;
115-
config.pin_vsync = VSYNC_GPIO_NUM;
116-
config.pin_href = HREF_GPIO_NUM;
117-
config.pin_sccb_sda = SIOD_GPIO_NUM;
118-
config.pin_sccb_scl = SIOC_GPIO_NUM;
119-
config.pin_pwdn = PWDN_GPIO_NUM;
120-
config.pin_reset = RESET_GPIO_NUM;
121-
config.xclk_freq_hz = 20000000;
122-
config.frame_size = FRAMESIZE_XGA;
123-
config.pixel_format = PIXFORMAT_JPEG;
124-
config.grab_mode = CAMERA_GRAB_WHEN_EMPTY;
125-
config.fb_location = CAMERA_FB_IN_PSRAM;
126-
config.jpeg_quality = 12;
127-
config.fb_count = 1;
128-
129-
// if PSRAM IC present, init with UXGA resolution and higher JPEG quality
130-
// for larger pre-allocated frame buffer.
131-
if (psramFound()) {
132-
config.jpeg_quality = 10;
133-
config.fb_count = 2;
134-
config.grab_mode = CAMERA_GRAB_LATEST;
135-
} else {
136-
// Limit the frame size when PSRAM is not available
137-
config.frame_size = FRAMESIZE_SVGA;
138-
config.fb_location = CAMERA_FB_IN_DRAM;
139-
}
140-
141-
// camera init
142-
esp_err_t err = esp_camera_init(&config);
143-
if (err != ESP_OK) {
144-
return err;
145-
}
146-
147-
sensor_t* s = esp_camera_sensor_get();
148-
// initial sensors are flipped vertically and colors are a bit saturated
149-
s->set_vflip(s, 1); // flip it back
150-
s->set_brightness(s, 1); // up the brightness just a bit
151-
s->set_saturation(s, 0); // lower the saturation
152-
153-
return ESP_OK;
104+
void setupCamera() {
105+
camera_config_t config;
106+
config.ledc_channel = LEDC_CHANNEL_0;
107+
config.ledc_timer = LEDC_TIMER_0;
108+
config.pin_d0 = Y2_GPIO_NUM;
109+
config.pin_d1 = Y3_GPIO_NUM;
110+
config.pin_d2 = Y4_GPIO_NUM;
111+
config.pin_d3 = Y5_GPIO_NUM;
112+
config.pin_d4 = Y6_GPIO_NUM;
113+
config.pin_d5 = Y7_GPIO_NUM;
114+
config.pin_d6 = Y8_GPIO_NUM;
115+
config.pin_d7 = Y9_GPIO_NUM;
116+
config.pin_xclk = XCLK_GPIO_NUM;
117+
config.pin_pclk = PCLK_GPIO_NUM;
118+
config.pin_vsync = VSYNC_GPIO_NUM;
119+
config.pin_href = HREF_GPIO_NUM;
120+
config.pin_sccb_sda = SIOD_GPIO_NUM;
121+
config.pin_sccb_scl = SIOC_GPIO_NUM;
122+
config.pin_pwdn = PWDN_GPIO_NUM;
123+
config.pin_reset = RESET_GPIO_NUM;
124+
config.xclk_freq_hz = 20000000;
125+
config.frame_size = FRAMESIZE_XGA;
126+
config.pixel_format = PIXFORMAT_JPEG; // do not change!
127+
// config.pixel_format = PIXFORMAT_RGB565; // for face detection/recognition
128+
config.grab_mode = CAMERA_GRAB_WHEN_EMPTY;
129+
config.fb_location = CAMERA_FB_IN_PSRAM;
130+
config.jpeg_quality = 12;
131+
config.fb_count = 1;
132+
133+
if (psramFound()) {
134+
config.jpeg_quality = 10;
135+
config.fb_count = 2;
136+
config.grab_mode = CAMERA_GRAB_LATEST;
137+
} else {
138+
// Limit the frame size when PSRAM is not available
139+
config.frame_size = FRAMESIZE_SVGA;
140+
config.fb_location = CAMERA_FB_IN_DRAM;
141+
}
142+
143+
#if defined(CAMERA_MODEL_ESP_EYE)
144+
pinMode(13, INPUT_PULLUP);
145+
pinMode(14, INPUT_PULLUP);
146+
#endif
147+
148+
// camera init
149+
esp_err_t err = esp_camera_init(&config);
150+
if (err != ESP_OK) {
151+
Serial.printf("Camera init failed with error 0x%x", err);
152+
return;
153+
}
154+
155+
sensor_t *s = esp_camera_sensor_get();
156+
// initial sensors are flipped vertically and colors are a bit saturated
157+
if (s->id.PID == OV3660_PID) {
158+
s->set_vflip(s, 1); // flip it back
159+
s->set_brightness(s, 1); // up the brightness just a bit
160+
s->set_saturation(s, -2); // lower the saturation
161+
}
162+
163+
#if defined(CAMERA_MODEL_M5STACK_WIDE) || defined(CAMERA_MODEL_M5STACK_ESP32CAM)
164+
s->set_vflip(s, 1);
165+
s->set_hmirror(s, 1);
166+
#endif
167+
168+
#if defined(CAMERA_MODEL_ESP32S3_EYE)
169+
s->set_vflip(s, 1);
170+
#endif
154171
}
155172

156173
void setup() {
157-
Serial.begin(BAUD_RATE); Serial.printf("\r\n\r\n");
158-
setupWiFi();
159-
setupSinricPro();
160-
setupCamera();
174+
Serial.begin(BAUD_RATE);
175+
Serial.setDebugOutput(true);
176+
Serial.println();
177+
delay(1500);
178+
179+
setupCamera();
180+
setupWiFi();
181+
setupSinricPro();
161182
}
162183

163184
void loop() {
164-
SinricPro.handle();
165-
}
166-
167-
185+
SinricPro.handle();
186+
}

0 commit comments

Comments
 (0)