|
| 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 | + |
1 | 19 | #include "esp_camera.h"
|
2 | 20 | #include <WiFi.h>
|
3 | 21 |
|
@@ -57,18 +75,49 @@ void setupWiFi() {
|
57 | 75 | }
|
58 | 76 |
|
59 | 77 | 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"); |
61 | 79 | return true; // request handled properly
|
62 | 80 | }
|
63 | 81 |
|
| 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 | +} |
64 | 112 |
|
65 | 113 | // setup function for SinricPro
|
66 | 114 | void setupSinricPro() {
|
67 | 115 | // add device to SinricPro
|
68 |
| - SinricProCamera& mySwitch = SinricPro[CAMERA_ID]; |
| 116 | + SinricProCamera& myCamera = SinricPro[CAMERA_ID]; |
69 | 117 |
|
70 | 118 | // set callback function to device
|
71 |
| - mySwitch.onPowerState(onPowerState); |
| 119 | + myCamera.onPowerState(onPowerState); |
| 120 | + myCamera.onSnapshot(onSnapshot); |
72 | 121 |
|
73 | 122 | // setup SinricPro
|
74 | 123 | SinricPro.onConnected([](){ Serial.printf("Connected to SinricPro\r\n"); });
|
@@ -145,7 +194,6 @@ void cameraInit(void){
|
145 | 194 | // for larger pre-allocated frame buffer.
|
146 | 195 | if(config.pixel_format == PIXFORMAT_JPEG){
|
147 | 196 | if(psramFound()){
|
148 |
| - Serial.printf("PSRAM found!\n"); |
149 | 197 | config.jpeg_quality = 10;
|
150 | 198 | config.fb_count = 2;
|
151 | 199 | config.grab_mode = CAMERA_GRAB_LATEST;
|
|
0 commit comments