Skip to content

Commit 56766b2

Browse files
committed
clean up and snapshot supprot added.
1 parent 4acb10e commit 56766b2

File tree

2 files changed

+171
-122
lines changed

2 files changed

+171
-122
lines changed

examples/Camera/mjpeg-camera/mjpeg-camera.ino

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
/*
2+
* Example for ESP32 Camera Streaming:
3+
* - Create a Camera device from portal.
4+
* - Copy the secrets below.
5+
* - Update WiFi settings
6+
* - This example needs SDK v3.4.0 or newer
7+
*
8+
* If you encounter any issues:
9+
* - check the readme.md at https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md
10+
* - ensure all dependent libraries are installed
11+
* - see https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md#arduinoide
12+
* - see https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md#dependencies
13+
* - open serial monitor and check whats happening
14+
* - check full user documentation at https://sinricpro.github.io/esp8266-esp32-sdk
15+
* - visit https://github.com/sinricpro/esp8266-esp32-sdk/issues and check for existing issues or open a new one
16+
*/
17+
18+
119
#include "esp_camera.h"
220
#include <WiFi.h>
321

@@ -57,18 +75,49 @@ void setupWiFi() {
5775
}
5876

5977
bool onPowerState(const String &deviceId, bool &state) {
60-
Serial.printf("Device %s turned %s (via SinricPro) \r\n", deviceId.c_str(), state?"on":"off");
78+
Serial.printf("Camera %s turned %s (via SinricPro) \r\n", deviceId.c_str(), state?"on":"off");
6179
return true; // request handled properly
6280
}
6381

82+
bool onSnapshot(const String &deviceId) {
83+
camera_fb_t *fb = esp_camera_fb_get();
84+
85+
if (!fb) {
86+
Serial.println("Failed to grab image");
87+
return false;
88+
}
89+
90+
SinricProCamera &myCamera = SinricPro[deviceId];
91+
int responseCode = myCamera.sendSnapshot(fb->buf, fb->len);
92+
93+
// Handle different response codes
94+
switch (responseCode) {
95+
case 200:
96+
Serial.println("Snapshot sent successfully");
97+
break;
98+
case 413:
99+
Serial.println("Error: File exceeds maximum allowed size");
100+
break;
101+
case 429:
102+
Serial.println("Error: Rate limit exceeded");
103+
break;
104+
default:
105+
Serial.printf("Error: Send failed with code %d\n", responseCode);
106+
break;
107+
}
108+
109+
esp_camera_fb_return(fb);
110+
return responseCode == 200;
111+
}
64112

65113
// setup function for SinricPro
66114
void setupSinricPro() {
67115
// add device to SinricPro
68-
SinricProCamera& mySwitch = SinricPro[CAMERA_ID];
116+
SinricProCamera& myCamera = SinricPro[CAMERA_ID];
69117

70118
// set callback function to device
71-
mySwitch.onPowerState(onPowerState);
119+
myCamera.onPowerState(onPowerState);
120+
myCamera.onSnapshot(onSnapshot);
72121

73122
// setup SinricPro
74123
SinricPro.onConnected([](){ Serial.printf("Connected to SinricPro\r\n"); });
@@ -145,7 +194,6 @@ void cameraInit(void){
145194
// for larger pre-allocated frame buffer.
146195
if(config.pixel_format == PIXFORMAT_JPEG){
147196
if(psramFound()){
148-
Serial.printf("PSRAM found!\n");
149197
config.jpeg_quality = 10;
150198
config.fb_count = 2;
151199
config.grab_mode = CAMERA_GRAB_LATEST;

0 commit comments

Comments
 (0)