diff --git a/examples/Camera/motion-capture/camera_pins.h b/examples/Camera/motion-capture/camera_pins.h new file mode 100644 index 0000000..dbb01e3 --- /dev/null +++ b/examples/Camera/motion-capture/camera_pins.h @@ -0,0 +1,563 @@ + +#if defined(CAMERA_MODEL_WROVER_KIT) +#define PWDN_GPIO_NUM -1 +#define RESET_GPIO_NUM -1 +#define XCLK_GPIO_NUM 21 +#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 19 +#define Y4_GPIO_NUM 18 +#define Y3_GPIO_NUM 5 +#define Y2_GPIO_NUM 4 +#define VSYNC_GPIO_NUM 25 +#define HREF_GPIO_NUM 23 +#define PCLK_GPIO_NUM 22 + +#elif defined(CAMERA_MODEL_ESP_EYE) +#define PWDN_GPIO_NUM -1 +#define RESET_GPIO_NUM -1 +#define XCLK_GPIO_NUM 4 +#define SIOD_GPIO_NUM 18 +#define SIOC_GPIO_NUM 23 + +#define Y9_GPIO_NUM 36 +#define Y8_GPIO_NUM 37 +#define Y7_GPIO_NUM 38 +#define Y6_GPIO_NUM 39 +#define Y5_GPIO_NUM 35 +#define Y4_GPIO_NUM 14 +#define Y3_GPIO_NUM 13 +#define Y2_GPIO_NUM 34 +#define VSYNC_GPIO_NUM 5 +#define HREF_GPIO_NUM 27 +#define PCLK_GPIO_NUM 25 + +#define LED_GPIO_NUM 22 + +#elif defined(CAMERA_MODEL_M5STACK_PSRAM) +#define PWDN_GPIO_NUM -1 +#define RESET_GPIO_NUM 15 +#define XCLK_GPIO_NUM 27 +#define SIOD_GPIO_NUM 25 +#define SIOC_GPIO_NUM 23 + +#define Y9_GPIO_NUM 19 +#define Y8_GPIO_NUM 36 +#define Y7_GPIO_NUM 18 +#define Y6_GPIO_NUM 39 +#define Y5_GPIO_NUM 5 +#define Y4_GPIO_NUM 34 +#define Y3_GPIO_NUM 35 +#define Y2_GPIO_NUM 32 +#define VSYNC_GPIO_NUM 22 +#define HREF_GPIO_NUM 26 +#define PCLK_GPIO_NUM 21 + +#elif defined(CAMERA_MODEL_M5STACK_V2_PSRAM) +#define PWDN_GPIO_NUM -1 +#define RESET_GPIO_NUM 15 +#define XCLK_GPIO_NUM 27 +#define SIOD_GPIO_NUM 22 +#define SIOC_GPIO_NUM 23 + +#define Y9_GPIO_NUM 19 +#define Y8_GPIO_NUM 36 +#define Y7_GPIO_NUM 18 +#define Y6_GPIO_NUM 39 +#define Y5_GPIO_NUM 5 +#define Y4_GPIO_NUM 34 +#define Y3_GPIO_NUM 35 +#define Y2_GPIO_NUM 32 +#define VSYNC_GPIO_NUM 25 +#define HREF_GPIO_NUM 26 +#define PCLK_GPIO_NUM 21 + +#elif defined(CAMERA_MODEL_M5STACK_WIDE) +#define PWDN_GPIO_NUM -1 +#define RESET_GPIO_NUM 15 +#define XCLK_GPIO_NUM 27 +#define SIOD_GPIO_NUM 22 +#define SIOC_GPIO_NUM 23 + +#define Y9_GPIO_NUM 19 +#define Y8_GPIO_NUM 36 +#define Y7_GPIO_NUM 18 +#define Y6_GPIO_NUM 39 +#define Y5_GPIO_NUM 5 +#define Y4_GPIO_NUM 34 +#define Y3_GPIO_NUM 35 +#define Y2_GPIO_NUM 32 +#define VSYNC_GPIO_NUM 25 +#define HREF_GPIO_NUM 26 +#define PCLK_GPIO_NUM 21 + +#define LED_GPIO_NUM 2 + +#elif defined(CAMERA_MODEL_M5STACK_ESP32CAM) +#define PWDN_GPIO_NUM -1 +#define RESET_GPIO_NUM 15 +#define XCLK_GPIO_NUM 27 +#define SIOD_GPIO_NUM 25 +#define SIOC_GPIO_NUM 23 + +#define Y9_GPIO_NUM 19 +#define Y8_GPIO_NUM 36 +#define Y7_GPIO_NUM 18 +#define Y6_GPIO_NUM 39 +#define Y5_GPIO_NUM 5 +#define Y4_GPIO_NUM 34 +#define Y3_GPIO_NUM 35 +#define Y2_GPIO_NUM 17 +#define VSYNC_GPIO_NUM 22 +#define HREF_GPIO_NUM 26 +#define PCLK_GPIO_NUM 21 + +#elif defined(CAMERA_MODEL_M5STACK_UNITCAM) +#define PWDN_GPIO_NUM -1 +#define RESET_GPIO_NUM 15 +#define XCLK_GPIO_NUM 27 +#define SIOD_GPIO_NUM 25 +#define SIOC_GPIO_NUM 23 + +#define Y9_GPIO_NUM 19 +#define Y8_GPIO_NUM 36 +#define Y7_GPIO_NUM 18 +#define Y6_GPIO_NUM 39 +#define Y5_GPIO_NUM 5 +#define Y4_GPIO_NUM 34 +#define Y3_GPIO_NUM 35 +#define Y2_GPIO_NUM 32 +#define VSYNC_GPIO_NUM 22 +#define HREF_GPIO_NUM 26 +#define PCLK_GPIO_NUM 21 + +#elif defined(CAMERA_MODEL_M5STACK_CAMS3_UNIT) +#define PWDN_GPIO_NUM -1 +#define RESET_GPIO_NUM 21 +#define XCLK_GPIO_NUM 11 +#define SIOD_GPIO_NUM 17 +#define SIOC_GPIO_NUM 41 + +#define Y9_GPIO_NUM 13 +#define Y8_GPIO_NUM 4 +#define Y7_GPIO_NUM 10 +#define Y6_GPIO_NUM 5 +#define Y5_GPIO_NUM 7 +#define Y4_GPIO_NUM 16 +#define Y3_GPIO_NUM 15 +#define Y2_GPIO_NUM 6 +#define VSYNC_GPIO_NUM 42 +#define HREF_GPIO_NUM 18 +#define PCLK_GPIO_NUM 12 + +#define LED_GPIO_NUM 14 + +#elif defined(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 + +// 4 for flash led or 33 for normal led +#define LED_GPIO_NUM 4 + +#elif defined(CAMERA_MODEL_TTGO_T_JOURNAL) +#define PWDN_GPIO_NUM 0 +#define RESET_GPIO_NUM 15 +#define XCLK_GPIO_NUM 27 +#define SIOD_GPIO_NUM 25 +#define SIOC_GPIO_NUM 23 + +#define Y9_GPIO_NUM 19 +#define Y8_GPIO_NUM 36 +#define Y7_GPIO_NUM 18 +#define Y6_GPIO_NUM 39 +#define Y5_GPIO_NUM 5 +#define Y4_GPIO_NUM 34 +#define Y3_GPIO_NUM 35 +#define Y2_GPIO_NUM 17 +#define VSYNC_GPIO_NUM 22 +#define HREF_GPIO_NUM 26 +#define PCLK_GPIO_NUM 21 + +#elif defined(CAMERA_MODEL_TTGO_T_V16_VERSION) +#define PWDN_GPIO_NUM -1 +#define RESET_GPIO_NUM -1 +#define XCLK_GPIO_NUM 4 +#define SIOD_GPIO_NUM 18 +#define SIOC_GPIO_NUM 23 + +#define Y9_GPIO_NUM 36 +#define Y8_GPIO_NUM 15 +#define Y7_GPIO_NUM 12 +#define Y6_GPIO_NUM 39 +#define Y5_GPIO_NUM 35 +#define Y4_GPIO_NUM 14 +#define Y3_GPIO_NUM 13 +#define Y2_GPIO_NUM 34 +#define VSYNC_GPIO_NUM 5 +#define HREF_GPIO_NUM 27 +#define PCLK_GPIO_NUM 25 + +#define AS312_PIN 19 +#define BUTTON_1 0 + +#define I2C_SDA 21 +#define I2C_SCL 22 + +#define SSD130_MODLE_TYPE 0 // 0 : GEOMETRY_128_64 // 1: GEOMETRY_128_32 + +#define IIS_SCK 26 +#define IIS_WS 32 +#define IIS_DOUT 33 + +#elif defined(CAMERA_MODEL_TTGO_T_V05_VERSION) + +#define PWDN_GPIO_NUM 26 +#define RESET_GPIO_NUM -1 +#define XCLK_GPIO_NUM 32 +#define SIOD_GPIO_NUM 13 +#define SIOC_GPIO_NUM 12 + +#define Y9_GPIO_NUM 39 +#define Y8_GPIO_NUM 36 +#define Y7_GPIO_NUM 23 +#define Y6_GPIO_NUM 18 +#define Y5_GPIO_NUM 15 +#define Y4_GPIO_NUM 4 +#define Y3_GPIO_NUM 14 +#define Y2_GPIO_NUM 5 + +#define VSYNC_GPIO_NUM 27 +#define HREF_GPIO_NUM 25 +#define PCLK_GPIO_NUM 19 + +#define AS312_PIN 33 +#define BUTTON_1 34 + +#define I2C_SDA 21 +#define I2C_SCL 22 + +#define SSD130_MODLE_TYPE 0 // 0 : GEOMETRY_128_64 // 1: GEOMETRY_128_32 + + +#elif defined(CAMERA_MODEL_TTGO_T_JORNAL_VERSION) + +#define PWDN_GPIO_NUM -1 +#define RESET_GPIO_NUM 15 +#define XCLK_GPIO_NUM 27 +#define SIOD_GPIO_NUM 25 +#define SIOC_GPIO_NUM 23 + +#define Y9_GPIO_NUM 19 +#define Y8_GPIO_NUM 36 +#define Y7_GPIO_NUM 18 +#define Y6_GPIO_NUM 39 +#define Y5_GPIO_NUM 5 +#define Y4_GPIO_NUM 34 +#define Y3_GPIO_NUM 35 +#define Y2_GPIO_NUM 17 + +#define VSYNC_GPIO_NUM 22 +#define HREF_GPIO_NUM 26 +#define PCLK_GPIO_NUM 21 + +#define I2C_SDA 14 +#define I2C_SCL 13 + +#define BUTTON_1 32 + +#define SSD130_MODLE_TYPE 1 // 0 : GEOMETRY_128_64 // 1: GEOMETRY_128_32 + +#elif defined(CAMERA_MODEL_TTGO_T_PLUS_VERSION) +#define PWDN_GPIO_NUM -1 +#define RESET_GPIO_NUM -1 +#define XCLK_GPIO_NUM 4 +#define SIOD_GPIO_NUM 18 +#define SIOC_GPIO_NUM 23 + +#define Y9_GPIO_NUM 36 +#define Y8_GPIO_NUM 37 +#define Y7_GPIO_NUM 38 +#define Y6_GPIO_NUM 39 +#define Y5_GPIO_NUM 35 +#define Y4_GPIO_NUM 26 +#define Y3_GPIO_NUM 13 +#define Y2_GPIO_NUM 34 +#define VSYNC_GPIO_NUM 5 +#define HREF_GPIO_NUM 27 +#define PCLK_GPIO_NUM 25 + +#define I2C_SDA 18 +#define I2C_SCL 23 + +#define IIS_SCK 14 +#define IIS_WS 32 +#define IIS_DOUT 33 + +#define TFT_CS_PIN 12 +#define TFT_DC_PIN 15 +#define TFT_MOSI_PIN 19 +#define TFT_MISO_PIN 22 +#define TFT_SCLK_PIN 21 +#define TFT_BL_PIN 2 + +#define SDCARD_CS_PIN 0 + +#define ENABLE_IP5306 +#define ENABLE_TFT + +#elif defined(CAMERA_MODEL_TTGO_T_V162_VERSION) + +#define PWDN_GPIO_NUM -1 +#define RESET_GPIO_NUM -1 +#define XCLK_GPIO_NUM 4 +#define SIOD_GPIO_NUM 18 +#define SIOC_GPIO_NUM 23 + +#define Y9_GPIO_NUM 36 +#define Y8_GPIO_NUM 37 +#define Y7_GPIO_NUM 38 +#define Y6_GPIO_NUM 39 +#define Y5_GPIO_NUM 35 +#define Y4_GPIO_NUM 14 +#define Y3_GPIO_NUM 13 +#define Y2_GPIO_NUM 34 +#define VSYNC_GPIO_NUM 5 +#define HREF_GPIO_NUM 27 +#define PCLK_GPIO_NUM 25 + +#define I2C_SDA 21 +#define I2C_SCL 22 + +#define BUTTON_1 15 + +#define SSD130_MODLE_TYPE 0 // 0 : GEOMETRY_128_64 // 1: GEOMETRY_128_32 + +#define AS312_PIN 19 //not rtc io ,can't form deepsleep wakeup + +#define IIS_SCK 26 +#define IIS_WS 32 +#define IIS_DOUT 33 + +#define ENABLE_IP5306 + +#elif defined(CAMERA_MODEL_TTGO_T_MINI_VERSION) + +#define PWDN_GPIO_NUM -1 +#define RESET_GPIO_NUM -1 +#define XCLK_GPIO_NUM 32 +#define SIOD_GPIO_NUM 13 +#define SIOC_GPIO_NUM 12 + +#define Y9_GPIO_NUM 39 +#define Y8_GPIO_NUM 36 +#define Y7_GPIO_NUM 38 +#define Y6_GPIO_NUM 37 +#define Y5_GPIO_NUM 15 +#define Y4_GPIO_NUM 4 +#define Y3_GPIO_NUM 14 +#define Y2_GPIO_NUM 5 + +#define VSYNC_GPIO_NUM 27 +#define HREF_GPIO_NUM 25 +#define PCLK_GPIO_NUM 19 +#define CAMERA_PWR_PIN 26 + +#define I2C_SDA 21 +#define I2C_SCL 22 + +#define BUTTON_1 33 + +#define POWER_CONTROL_PIN 26 + +#define ENABLE_AXP192 + +#elif defined(CAMERA_MODEL_TTGO_T_V17_VERSION) + +#define PWDN_GPIO_NUM -1 +#define RESET_GPIO_NUM -1 +#define XCLK_GPIO_NUM 32 +#define SIOD_GPIO_NUM 13 +#define SIOC_GPIO_NUM 12 + +#define Y9_GPIO_NUM 39 +#define Y8_GPIO_NUM 36 +#define Y7_GPIO_NUM 23 +#define Y6_GPIO_NUM 18 +#define Y5_GPIO_NUM 15 +#define Y4_GPIO_NUM 4 +#define Y3_GPIO_NUM 14 +#define Y2_GPIO_NUM 5 + +#define VSYNC_GPIO_NUM 27 +#define HREF_GPIO_NUM 25 +#define PCLK_GPIO_NUM 19 + +#define I2C_SDA 21 +#define I2C_SCL 22 + +#define BUTTON_1 34 + +#define SSD130_MODLE_TYPE 0 // 0 : GEOMETRY_128_64 // 1: GEOMETRY_128_32 + +#define AS312_PIN 33 + +#define ENABLE_IP5306 + +#elif defined(CAMERA_MODEL_XIAO_ESP32S3) +#define PWDN_GPIO_NUM -1 +#define RESET_GPIO_NUM -1 +#define XCLK_GPIO_NUM 10 +#define SIOD_GPIO_NUM 40 +#define SIOC_GPIO_NUM 39 + +#define Y9_GPIO_NUM 48 +#define Y8_GPIO_NUM 11 +#define Y7_GPIO_NUM 12 +#define Y6_GPIO_NUM 14 +#define Y5_GPIO_NUM 16 +#define Y4_GPIO_NUM 18 +#define Y3_GPIO_NUM 17 +#define Y2_GPIO_NUM 15 +#define VSYNC_GPIO_NUM 38 +#define HREF_GPIO_NUM 47 +#define PCLK_GPIO_NUM 13 + +#elif defined(CAMERA_MODEL_ESP32_CAM_BOARD) +// The 18 pin header on the board has Y5 and Y3 swapped +#define USE_BOARD_HEADER 0 +#define PWDN_GPIO_NUM 32 +#define RESET_GPIO_NUM 33 +#define XCLK_GPIO_NUM 4 +#define SIOD_GPIO_NUM 18 +#define SIOC_GPIO_NUM 23 + +#define Y9_GPIO_NUM 36 +#define Y8_GPIO_NUM 19 +#define Y7_GPIO_NUM 21 +#define Y6_GPIO_NUM 39 +#if USE_BOARD_HEADER +#define Y5_GPIO_NUM 13 +#else +#define Y5_GPIO_NUM 35 +#endif +#define Y4_GPIO_NUM 14 +#if USE_BOARD_HEADER +#define Y3_GPIO_NUM 35 +#else +#define Y3_GPIO_NUM 13 +#endif +#define Y2_GPIO_NUM 34 +#define VSYNC_GPIO_NUM 5 +#define HREF_GPIO_NUM 27 +#define PCLK_GPIO_NUM 25 + +#elif defined(CAMERA_MODEL_ESP32S3_CAM_LCD) +#define PWDN_GPIO_NUM -1 +#define RESET_GPIO_NUM -1 +#define XCLK_GPIO_NUM 40 +#define SIOD_GPIO_NUM 17 +#define SIOC_GPIO_NUM 18 + +#define Y9_GPIO_NUM 39 +#define Y8_GPIO_NUM 41 +#define Y7_GPIO_NUM 42 +#define Y6_GPIO_NUM 12 +#define Y5_GPIO_NUM 3 +#define Y4_GPIO_NUM 14 +#define Y3_GPIO_NUM 47 +#define Y2_GPIO_NUM 13 +#define VSYNC_GPIO_NUM 21 +#define HREF_GPIO_NUM 38 +#define PCLK_GPIO_NUM 11 + +#elif defined(CAMERA_MODEL_ESP32S2_CAM_BOARD) +// The 18 pin header on the board has Y5 and Y3 swapped +#define USE_BOARD_HEADER 0 +#define PWDN_GPIO_NUM 1 +#define RESET_GPIO_NUM 2 +#define XCLK_GPIO_NUM 42 +#define SIOD_GPIO_NUM 41 +#define SIOC_GPIO_NUM 18 + +#define Y9_GPIO_NUM 16 +#define Y8_GPIO_NUM 39 +#define Y7_GPIO_NUM 40 +#define Y6_GPIO_NUM 15 +#if USE_BOARD_HEADER +#define Y5_GPIO_NUM 12 +#else +#define Y5_GPIO_NUM 13 +#endif +#define Y4_GPIO_NUM 5 +#if USE_BOARD_HEADER +#define Y3_GPIO_NUM 13 +#else +#define Y3_GPIO_NUM 12 +#endif +#define Y2_GPIO_NUM 14 +#define VSYNC_GPIO_NUM 38 +#define HREF_GPIO_NUM 4 +#define PCLK_GPIO_NUM 3 + +#elif defined(CAMERA_MODEL_ESP32S3_EYE) +#define PWDN_GPIO_NUM -1 +#define RESET_GPIO_NUM -1 +#define XCLK_GPIO_NUM 15 +#define SIOD_GPIO_NUM 4 +#define SIOC_GPIO_NUM 5 + +#define Y2_GPIO_NUM 11 +#define Y3_GPIO_NUM 9 +#define Y4_GPIO_NUM 8 +#define Y5_GPIO_NUM 10 +#define Y6_GPIO_NUM 12 +#define Y7_GPIO_NUM 18 +#define Y8_GPIO_NUM 17 +#define Y9_GPIO_NUM 16 + +#define VSYNC_GPIO_NUM 6 +#define HREF_GPIO_NUM 7 +#define PCLK_GPIO_NUM 13 + +#elif defined(CAMERA_MODEL_DFRobot_FireBeetle2_ESP32S3) || defined(CAMERA_MODEL_DFRobot_Romeo_ESP32S3) +#define PWDN_GPIO_NUM -1 +#define RESET_GPIO_NUM -1 +#define XCLK_GPIO_NUM 45 +#define SIOD_GPIO_NUM 1 +#define SIOC_GPIO_NUM 2 + +#define Y9_GPIO_NUM 48 +#define Y8_GPIO_NUM 46 +#define Y7_GPIO_NUM 8 +#define Y6_GPIO_NUM 7 +#define Y5_GPIO_NUM 4 +#define Y4_GPIO_NUM 41 +#define Y3_GPIO_NUM 40 +#define Y2_GPIO_NUM 39 +#define VSYNC_GPIO_NUM 6 +#define HREF_GPIO_NUM 42 +#define PCLK_GPIO_NUM 5 + +#else +#error "Camera model not selected" +#endif \ No newline at end of file diff --git a/examples/Camera/motion-capture/motion-capture.ino b/examples/Camera/motion-capture/motion-capture.ino new file mode 100644 index 0000000..77fc12e --- /dev/null +++ b/examples/Camera/motion-capture/motion-capture.ino @@ -0,0 +1,367 @@ +/* + * Example for ESP32 Camera Motion Detection System: + * - Create a ESP32 Camera device from portal. + * - Copy the secrets below. + * - Update WiFi settings + * + * If you encounter any issues: + * - check the readme.md at https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md + * - ensure all dependent libraries are installed + * - see https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md#arduinoide + * - see https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md#dependencies + * - open serial monitor and check whats happening + * - check full user documentation at https://sinricpro.github.io/esp8266-esp32-sdk + * - visit https://github.com/sinricpro/esp8266-esp32-sdk/issues and check for existing issues or open a new one + */ + +// Uncomment the following line to enable serial debug output +#define ENABLE_DEBUG + +#include +#include +#include +#include +#include +#include +#include "esp_http_client.h" +#include "HTTPClient.h" +#include + +// =================== +// Select camera model +// =================== +// #define CAMERA_MODEL_WROVER_KIT // Has PSRAM + #define CAMERA_MODEL_ESP_EYE // Has PSRAM +// #define CAMERA_MODEL_ESP32S3_EYE // Has PSRAM +// #define CAMERA_MODEL_M5STACK_PSRAM // Has PSRAM +// #define CAMERA_MODEL_M5STACK_V2_PSRAM // M5Camera version B Has PSRAM +// #define CAMERA_MODEL_M5STACK_WIDE // Has PSRAM +// #define CAMERA_MODEL_M5STACK_ESP32CAM // No PSRAM +// #define CAMERA_MODEL_M5STACK_UNITCAM // No PSRAM +// #define CAMERA_MODEL_M5STACK_CAMS3_UNIT // Has PSRAM +// #define CAMERA_MODEL_AI_THINKER // Has PSRAM +// #define CAMERA_MODEL_TTGO_T_JOURNAL // No PSRAM +// #define CAMERA_MODEL_TTGO_T_V16_VERSION +// #define CAMERA_MODEL_TTGO_T_V05_VERSION +// #define CAMERA_MODEL_TTGO_T_PLUS_VERSION +// #define CAMERA_MODEL_TTGO_T_V162_VERSION +// #define CAMERA_MODEL_TTGO_T_MINI_VERSION +// #define CAMERA_MODEL_TTGO_T_V17_VERSION +// #define CAMERA_MODEL_XIAO_ESP32S3 // Has PSRAM +// ** Espressif Internal Boards ** +// #define CAMERA_MODEL_ESP32_CAM_BOARD +// #define CAMERA_MODEL_ESP32S2_CAM_BOARD +// #define CAMERA_MODEL_ESP32S3_CAM_LCD +// #define CAMERA_MODEL_DFRobot_FireBeetle2_ESP32S3 // Has PSRAM +// #define CAMERA_MODEL_DFRobot_Romeo_ESP32S3 // Has PSRAM +#include "camera_pins.h" + +#define MOTION_SENSOR_PIN 5 // GPIO pin connected to PIR motion sensor +#define FRAME_CAPTURE_COUNT 10 // Number of frames to capture when motion is detected +#define FRAME_CAPTURE_DELAY_MS 200 // Delay between frame captures (milliseconds) +#define MOTION_DEBOUNCE_DELAY 250 // Debounce delay for motion sensor (milliseconds) +#define TEMP_FILE_PATH "/mjpeg.tmp" // Temporary file path in SPIFFS for storing frames + +#define WIFI_SSID "" // Your WiFi network SSID +#define WIFI_PASS "" // Your WiFi network password +#define APP_KEY "" // SinricPro APP_KEY +#define APP_SECRET "" // SinricPro APP_SECRET +#define CAMERA_ID "" // SinricPro CAMERA_ID + +bool lastMotionState = false; // Last recorded state of the motion sensor +unsigned long lastChangeTime = 0; // Timestamp of last motion state change +unsigned long lastSendTime = 0; // Timestamp of last successful motion data send +const unsigned long SEND_COOLDOWN = 60000; // Minimum time between sends (60 seconds) + +/** + * Initializes the SPIFFS file system + */ +bool initFS() { + if (!SPIFFS.begin(true)) { + Serial.println("SPIFFS initialization failed!"); + return false; + } + return true; +} + +/** + * Initializes the camera with the specified configuration + * + * @return bool True if initialization successful, false otherwise + */ +bool initCamera() { + camera_config_t config; + + // Basic pin configuration + 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_sccb_sda = SIOD_GPIO_NUM; + config.pin_sccb_scl = SIOC_GPIO_NUM; + config.pin_pwdn = PWDN_GPIO_NUM; + config.pin_reset = RESET_GPIO_NUM; + + // Camera settings + config.xclk_freq_hz = 20000000; + config.pixel_format = PIXFORMAT_JPEG; + config.frame_size = FRAMESIZE_SVGA; + config.grab_mode = CAMERA_GRAB_LATEST; + config.jpeg_quality = 12; + + // Adjust settings based on PSRAM availability + if (psramFound()) { + config.fb_count = 2; + config.fb_location = CAMERA_FB_IN_PSRAM; + } else { + config.fb_count = 1; + config.frame_size = FRAMESIZE_SVGA; + config.fb_location = CAMERA_FB_IN_DRAM; + } + +#if defined(CAMERA_MODEL_ESP_EYE) + pinMode(13, INPUT_PULLUP); + pinMode(14, INPUT_PULLUP); +#endif + + // camera init + esp_err_t err = esp_camera_init(&config); + if (err != ESP_OK) { + Serial.printf("Camera init failed with error 0x%x", err); + return false; + } + + sensor_t* s = esp_camera_sensor_get(); + // initial sensors are flipped vertically and colors are a bit saturated + if (s->id.PID == OV3660_PID) { + s->set_vflip(s, 1); // flip it back + s->set_brightness(s, 1); // up the brightness just a bit + s->set_saturation(s, -2); // lower the saturation + } + +#if defined(CAMERA_MODEL_M5STACK_WIDE) || defined(CAMERA_MODEL_M5STACK_ESP32CAM) + s->set_vflip(s, 1); + s->set_hmirror(s, 1); +#endif + +#if defined(CAMERA_MODEL_ESP32S3_EYE) + s->set_vflip(s, 1); +#endif + + return true; +} + +/** + * Sets up WiFi connection + */ +void setupWiFi() { + Serial.print("\nConnecting to WiFi"); + + WiFi.begin(WIFI_SSID, WIFI_PASS); + WiFi.setSleep(false); // Disable WiFi sleep mode for better performance + + // Wait for connection + while (WiFi.status() != WL_CONNECTED) { + Serial.print("."); + delay(250); + } + + Serial.printf("\nWiFi connected! IP address: %s\n", + WiFi.localIP().toString().c_str()); +} + +/** + * Sets up SinricPro connection and event handlers + */ +void setupSinricPro() { + SinricProCamera& camera = SinricPro[CAMERA_ID]; + + // Setup power state callback + camera.onPowerState([](const String& deviceId, bool& state) { + return true; + }); + + camera.onSnapshot([](const String& deviceId) { + camera_fb_t *fb = esp_camera_fb_get(); + + if (!fb) { + Serial.println("Failed to grab image"); + return false; + } + + SinricProCamera &myCamera = SinricPro[CAMERA_ID]; + int result = myCamera.sendSnapshot(fb->buf, fb->len); + esp_camera_fb_return(fb); + return result == 200; + }); + + // Setup connection event handlers + SinricPro.onConnected([]() { + Serial.println("Connected to SinricPro"); + }); + + SinricPro.onDisconnected([]() { + Serial.println("Disconnected from SinricPro"); + }); + + // Start SinricPro + //SinricPro.begin(APP_KEY, APP_SECRET); + SinricPro.begin(APP_KEY, APP_SECRET, "testws.sinric.pro"); +} + +/** + * Captures multiple frames from the camera and writes them to file + * + * @param file Reference to opened file for writing frames + * @return size_t Total size of captured frames in bytes + */ +bool captureFrames(File& file) { + size_t totalSize = 0; + unsigned long startTime = millis(); + + for (int i = 0; i < FRAME_CAPTURE_COUNT; i++) { + // Get frame from camera + camera_fb_t* fb = esp_camera_fb_get(); + if (!fb) { + Serial.println("Camera capture failed"); + continue; + } + + // Write frame to file + if (file.write(fb->buf, fb->len)) { + totalSize += fb->len; + Serial.printf("Frame %d saved, size: %d bytes\n", i + 1, fb->len); + } else { + Serial.println("Failed to write frame"); + } + + // Return frame buffer to be reused + esp_camera_fb_return(fb); + delay(FRAME_CAPTURE_DELAY_MS); + } + + Serial.printf("Motion capture completed in %lu ms\n", millis() - startTime); + Serial.printf("Motion file size: %d\n", totalSize); + return totalSize > 0; +} + +/** + * Sends captured motion data to SinricPro server + * Handles different response codes from the server + */ +void sendMotionToServer() { + SinricProCamera& camera = SinricPro[CAMERA_ID]; + int responseCode = camera.sendMotion(SPIFFS, TEMP_FILE_PATH); + + // Handle different response codes + switch (responseCode) { + case 200: + Serial.println("Motion data sent successfully"); + break; + case 413: + Serial.println("Error: File exceeds maximum allowed size"); + break; + case 429: + Serial.println("Error: Rate limit exceeded"); + break; + default: + Serial.printf("Error: Send failed with code %d\n", responseCode); + break; + } +} + +/** + * Captures frames when motion is detected and sends them to server + * Handles temporary file creation and cleanup + */ +void captureAndSendMotion() { + // Check if enough time has passed since last send + unsigned long currentTime = millis(); + if (currentTime - lastSendTime < SEND_COOLDOWN) { + Serial.println("Motion detected but skipping - cooldown period active"); + return; + } + + // Open temporary file for writing frames + File mjpegFile = SPIFFS.open(TEMP_FILE_PATH, FILE_WRITE); + if (!mjpegFile) { + Serial.println("Failed to open temporary file for writing"); + return; + } + + // Capture frames and send if successful + bool captured = captureFrames(mjpegFile); + mjpegFile.close(); + + if (captured) { + sendMotionToServer(); + } + + // Cleanup temporary file + SPIFFS.remove(TEMP_FILE_PATH); +} + +/** + * Handles motion sensor state changes + * Implements debouncing and triggers capture when motion is detected + */ +void handleMotionSensor() { + unsigned long currentTime = millis(); + // Check debounce period + if (currentTime - lastChangeTime < MOTION_DEBOUNCE_DELAY) return; + + // Read current motion state + bool currentMotionState = digitalRead(MOTION_SENSOR_PIN); + if (currentMotionState != lastMotionState) { + Serial.printf("Motion %s\n", currentMotionState ? "detected" : "not detected"); + lastMotionState = currentMotionState; + lastChangeTime = currentTime; + + // Trigger capture on motion detection + if (currentMotionState) { + captureAndSendMotion(); + } + } +} + +bool onPowerState(const String& deviceId, bool& state) { + return true; +} + +/** + * Setup function - initializes all components + */ +void setup() { + Serial.begin(115200); + + // Initialize systems + if (!initFS()) { + Serial.println("File system initialization failed!"); + return; + } + + pinMode(MOTION_SENSOR_PIN, INPUT); + + if (!initCamera()) { + Serial.println("Camera initialization failed!"); + return; + } + + setupWiFi(); + setupSinricPro(); +} + +void loop() { + SinricPro.handle(); + handleMotionSensor(); +} diff --git a/examples/Camera/snapshot-camera/camera_pins.h b/examples/Camera/snapshot-camera/camera_pins.h index a007ffd..dbb01e3 100644 --- a/examples/Camera/snapshot-camera/camera_pins.h +++ b/examples/Camera/snapshot-camera/camera_pins.h @@ -198,6 +198,231 @@ #define HREF_GPIO_NUM 26 #define PCLK_GPIO_NUM 21 +#elif defined(CAMERA_MODEL_TTGO_T_V16_VERSION) +#define PWDN_GPIO_NUM -1 +#define RESET_GPIO_NUM -1 +#define XCLK_GPIO_NUM 4 +#define SIOD_GPIO_NUM 18 +#define SIOC_GPIO_NUM 23 + +#define Y9_GPIO_NUM 36 +#define Y8_GPIO_NUM 15 +#define Y7_GPIO_NUM 12 +#define Y6_GPIO_NUM 39 +#define Y5_GPIO_NUM 35 +#define Y4_GPIO_NUM 14 +#define Y3_GPIO_NUM 13 +#define Y2_GPIO_NUM 34 +#define VSYNC_GPIO_NUM 5 +#define HREF_GPIO_NUM 27 +#define PCLK_GPIO_NUM 25 + +#define AS312_PIN 19 +#define BUTTON_1 0 + +#define I2C_SDA 21 +#define I2C_SCL 22 + +#define SSD130_MODLE_TYPE 0 // 0 : GEOMETRY_128_64 // 1: GEOMETRY_128_32 + +#define IIS_SCK 26 +#define IIS_WS 32 +#define IIS_DOUT 33 + +#elif defined(CAMERA_MODEL_TTGO_T_V05_VERSION) + +#define PWDN_GPIO_NUM 26 +#define RESET_GPIO_NUM -1 +#define XCLK_GPIO_NUM 32 +#define SIOD_GPIO_NUM 13 +#define SIOC_GPIO_NUM 12 + +#define Y9_GPIO_NUM 39 +#define Y8_GPIO_NUM 36 +#define Y7_GPIO_NUM 23 +#define Y6_GPIO_NUM 18 +#define Y5_GPIO_NUM 15 +#define Y4_GPIO_NUM 4 +#define Y3_GPIO_NUM 14 +#define Y2_GPIO_NUM 5 + +#define VSYNC_GPIO_NUM 27 +#define HREF_GPIO_NUM 25 +#define PCLK_GPIO_NUM 19 + +#define AS312_PIN 33 +#define BUTTON_1 34 + +#define I2C_SDA 21 +#define I2C_SCL 22 + +#define SSD130_MODLE_TYPE 0 // 0 : GEOMETRY_128_64 // 1: GEOMETRY_128_32 + + +#elif defined(CAMERA_MODEL_TTGO_T_JORNAL_VERSION) + +#define PWDN_GPIO_NUM -1 +#define RESET_GPIO_NUM 15 +#define XCLK_GPIO_NUM 27 +#define SIOD_GPIO_NUM 25 +#define SIOC_GPIO_NUM 23 + +#define Y9_GPIO_NUM 19 +#define Y8_GPIO_NUM 36 +#define Y7_GPIO_NUM 18 +#define Y6_GPIO_NUM 39 +#define Y5_GPIO_NUM 5 +#define Y4_GPIO_NUM 34 +#define Y3_GPIO_NUM 35 +#define Y2_GPIO_NUM 17 + +#define VSYNC_GPIO_NUM 22 +#define HREF_GPIO_NUM 26 +#define PCLK_GPIO_NUM 21 + +#define I2C_SDA 14 +#define I2C_SCL 13 + +#define BUTTON_1 32 + +#define SSD130_MODLE_TYPE 1 // 0 : GEOMETRY_128_64 // 1: GEOMETRY_128_32 + +#elif defined(CAMERA_MODEL_TTGO_T_PLUS_VERSION) +#define PWDN_GPIO_NUM -1 +#define RESET_GPIO_NUM -1 +#define XCLK_GPIO_NUM 4 +#define SIOD_GPIO_NUM 18 +#define SIOC_GPIO_NUM 23 + +#define Y9_GPIO_NUM 36 +#define Y8_GPIO_NUM 37 +#define Y7_GPIO_NUM 38 +#define Y6_GPIO_NUM 39 +#define Y5_GPIO_NUM 35 +#define Y4_GPIO_NUM 26 +#define Y3_GPIO_NUM 13 +#define Y2_GPIO_NUM 34 +#define VSYNC_GPIO_NUM 5 +#define HREF_GPIO_NUM 27 +#define PCLK_GPIO_NUM 25 + +#define I2C_SDA 18 +#define I2C_SCL 23 + +#define IIS_SCK 14 +#define IIS_WS 32 +#define IIS_DOUT 33 + +#define TFT_CS_PIN 12 +#define TFT_DC_PIN 15 +#define TFT_MOSI_PIN 19 +#define TFT_MISO_PIN 22 +#define TFT_SCLK_PIN 21 +#define TFT_BL_PIN 2 + +#define SDCARD_CS_PIN 0 + +#define ENABLE_IP5306 +#define ENABLE_TFT + +#elif defined(CAMERA_MODEL_TTGO_T_V162_VERSION) + +#define PWDN_GPIO_NUM -1 +#define RESET_GPIO_NUM -1 +#define XCLK_GPIO_NUM 4 +#define SIOD_GPIO_NUM 18 +#define SIOC_GPIO_NUM 23 + +#define Y9_GPIO_NUM 36 +#define Y8_GPIO_NUM 37 +#define Y7_GPIO_NUM 38 +#define Y6_GPIO_NUM 39 +#define Y5_GPIO_NUM 35 +#define Y4_GPIO_NUM 14 +#define Y3_GPIO_NUM 13 +#define Y2_GPIO_NUM 34 +#define VSYNC_GPIO_NUM 5 +#define HREF_GPIO_NUM 27 +#define PCLK_GPIO_NUM 25 + +#define I2C_SDA 21 +#define I2C_SCL 22 + +#define BUTTON_1 15 + +#define SSD130_MODLE_TYPE 0 // 0 : GEOMETRY_128_64 // 1: GEOMETRY_128_32 + +#define AS312_PIN 19 //not rtc io ,can't form deepsleep wakeup + +#define IIS_SCK 26 +#define IIS_WS 32 +#define IIS_DOUT 33 + +#define ENABLE_IP5306 + +#elif defined(CAMERA_MODEL_TTGO_T_MINI_VERSION) + +#define PWDN_GPIO_NUM -1 +#define RESET_GPIO_NUM -1 +#define XCLK_GPIO_NUM 32 +#define SIOD_GPIO_NUM 13 +#define SIOC_GPIO_NUM 12 + +#define Y9_GPIO_NUM 39 +#define Y8_GPIO_NUM 36 +#define Y7_GPIO_NUM 38 +#define Y6_GPIO_NUM 37 +#define Y5_GPIO_NUM 15 +#define Y4_GPIO_NUM 4 +#define Y3_GPIO_NUM 14 +#define Y2_GPIO_NUM 5 + +#define VSYNC_GPIO_NUM 27 +#define HREF_GPIO_NUM 25 +#define PCLK_GPIO_NUM 19 +#define CAMERA_PWR_PIN 26 + +#define I2C_SDA 21 +#define I2C_SCL 22 + +#define BUTTON_1 33 + +#define POWER_CONTROL_PIN 26 + +#define ENABLE_AXP192 + +#elif defined(CAMERA_MODEL_TTGO_T_V17_VERSION) + +#define PWDN_GPIO_NUM -1 +#define RESET_GPIO_NUM -1 +#define XCLK_GPIO_NUM 32 +#define SIOD_GPIO_NUM 13 +#define SIOC_GPIO_NUM 12 + +#define Y9_GPIO_NUM 39 +#define Y8_GPIO_NUM 36 +#define Y7_GPIO_NUM 23 +#define Y6_GPIO_NUM 18 +#define Y5_GPIO_NUM 15 +#define Y4_GPIO_NUM 4 +#define Y3_GPIO_NUM 14 +#define Y2_GPIO_NUM 5 + +#define VSYNC_GPIO_NUM 27 +#define HREF_GPIO_NUM 25 +#define PCLK_GPIO_NUM 19 + +#define I2C_SDA 21 +#define I2C_SCL 22 + +#define BUTTON_1 34 + +#define SSD130_MODLE_TYPE 0 // 0 : GEOMETRY_128_64 // 1: GEOMETRY_128_32 + +#define AS312_PIN 33 + +#define ENABLE_IP5306 + #elif defined(CAMERA_MODEL_XIAO_ESP32S3) #define PWDN_GPIO_NUM -1 #define RESET_GPIO_NUM -1 diff --git a/examples/Camera/snapshot-camera/snapshot-camera.ino b/examples/Camera/snapshot-camera/snapshot-camera.ino index a146d7a..d457c69 100644 --- a/examples/Camera/snapshot-camera/snapshot-camera.ino +++ b/examples/Camera/snapshot-camera/snapshot-camera.ino @@ -27,7 +27,7 @@ // Select camera model // =================== // #define CAMERA_MODEL_WROVER_KIT // Has PSRAM -// #define CAMERA_MODEL_ESP_EYE // Has PSRAM + #define CAMERA_MODEL_ESP_EYE // Has PSRAM // #define CAMERA_MODEL_ESP32S3_EYE // Has PSRAM // #define CAMERA_MODEL_M5STACK_PSRAM // Has PSRAM // #define CAMERA_MODEL_M5STACK_V2_PSRAM // M5Camera version B Has PSRAM @@ -37,6 +37,12 @@ // #define CAMERA_MODEL_M5STACK_CAMS3_UNIT // Has PSRAM // #define CAMERA_MODEL_AI_THINKER // Has PSRAM // #define CAMERA_MODEL_TTGO_T_JOURNAL // No PSRAM +// #define CAMERA_MODEL_TTGO_T_V16_VERSION +// #define CAMERA_MODEL_TTGO_T_V05_VERSION +// #define CAMERA_MODEL_TTGO_T_PLUS_VERSION +// #define CAMERA_MODEL_TTGO_T_V162_VERSION +// #define CAMERA_MODEL_TTGO_T_MINI_VERSION +// #define CAMERA_MODEL_TTGO_T_V17_VERSION // #define CAMERA_MODEL_XIAO_ESP32S3 // Has PSRAM // ** Espressif Internal Boards ** // #define CAMERA_MODEL_ESP32_CAM_BOARD @@ -46,11 +52,12 @@ // #define CAMERA_MODEL_DFRobot_Romeo_ESP32S3 // Has PSRAM #include "camera_pins.h" -#define WIFI_SSID "YOUR-WIFI-SSID" -#define WIFI_PASS "YOUR-WIFI-PASSWORD" -#define APP_KEY "YOUR-APP-KEY" // Should look like "de0bxxxx-1x3x-4x3x-ax2x-5dabxxxxxxxx" -#define APP_SECRET "YOUR-APP-SECRET" // Should look like "5f36xxxx-x3x7-4x3x-xexe-e86724a9xxxx-4c4axxxx-3x3x-x5xe-x9x3-333d65xxxxxx" -#define CAMERA_ID "YOUR-ESP32-CAMERA-ID" // Should look like "5dc1564130xxxxxxxxxxxxxx" +#define WIFI_SSID "" // Your WiFi network SSID +#define WIFI_PASS "" // Your WiFi network password + +#define APP_KEY "" // Should look like "de0bxxxx-1x3x-4x3x-ax2x-5dabxxxxxxxx" +#define APP_SECRET "" // Should look like "5f36xxxx-x3x7-4x3x-xexe-e86724a9xxxx-4c4axxxx-3x3x-x5xe-x9x3-333d65xxxxxx" +#define CAMERA_ID "" // Should look like "5dc1564130xxxxxxxxxxxxxx" #define BAUD_RATE 115200 // Change baudrate to your need @@ -63,9 +70,25 @@ bool onSnapshot(const String &deviceId) { } SinricProCamera &myCamera = SinricPro[deviceId]; - int result = myCamera.sendSnapshot(fb->buf, fb->len); - esp_camera_fb_return(fb); + int responseCode = myCamera.sendSnapshot(fb->buf, fb->len); + + // Handle different response codes + switch (responseCode) { + case 200: + Serial.println("Snapshot sent successfully"); + break; + case 413: + Serial.println("Error: File exceeds maximum allowed size"); + break; + case 429: + Serial.println("Error: Rate limit exceeded"); + break; + default: + Serial.printf("Error: Send failed with code %d\n", responseCode); + break; + } + esp_camera_fb_return(fb); return result == 200; } @@ -124,7 +147,6 @@ void setupCamera() { config.xclk_freq_hz = 20000000; config.frame_size = FRAMESIZE_XGA; config.pixel_format = PIXFORMAT_JPEG; // do not change! - // config.pixel_format = PIXFORMAT_RGB565; // for face detection/recognition config.grab_mode = CAMERA_GRAB_WHEN_EMPTY; config.fb_location = CAMERA_FB_IN_PSRAM; config.jpeg_quality = 12; @@ -133,7 +155,7 @@ void setupCamera() { if (psramFound()) { config.jpeg_quality = 10; config.fb_count = 2; - config.grab_mode = CAMERA_GRAB_LATEST; + config.grab_mode = CAMERA_GRAB_LATEST; } else { // Limit the frame size when PSRAM is not available config.frame_size = FRAMESIZE_SVGA; @@ -172,9 +194,6 @@ void setupCamera() { void setup() { Serial.begin(BAUD_RATE); - Serial.setDebugOutput(true); - Serial.println(); - delay(1500); setupCamera(); setupWiFi(); diff --git a/src/Capabilities/CameraController.h b/src/Capabilities/CameraController.h index 924a336..050b9bb 100644 --- a/src/Capabilities/CameraController.h +++ b/src/Capabilities/CameraController.h @@ -1,24 +1,27 @@ #pragma once -// Required header includes -#include "../EventLimiter.h" -#include "../SinricProNamespace.h" #include "../SinricProRequest.h" + +#include "../EventLimiter.h" #include "../SinricProStrings.h" -#include +#include "../SinricProNamespace.h" -#if defined(ESP32) +#include + +#if defined(ESP32) + #include #include #endif namespace SINRICPRO_NAMESPACE { FSTR(CAMERA, getSnapshot); // "getSnapshot" +FSTR(CAMERA, POST); // "POST" using SnapshotCallback = std::function; /** - * @brief Camera + * @brief CameraController class for managing camera operations in SinricPro * @ingroup Capabilities **/ template @@ -36,10 +39,19 @@ class CameraController { * @brief Sends a camera snapshot to the SinricPro server * @param buffer Pointer to the image data buffer * @param len Length of the image data in bytes - * @return HTTP response code + * @return error code */ int sendSnapshot(uint8_t* buffer, size_t len); + /** + * @brief Sends motion detection data from file + * @param fs Filesystem containing the motion data + * @param path Path to motion data file + * @return error code + */ + int sendMotion(fs::FS &fs, const char * path); + + protected: /** * @brief Handles incoming camera control requests @@ -49,12 +61,45 @@ class CameraController { bool handleCameraController(SinricProRequest &request); private: - // Callback handler for snapshot requests SnapshotCallback getSnapshotCallback = nullptr; + EventLimiter event_limiter; + +#if defined(ESP32) + std::unique_ptr createClient() { +#ifdef SINRICPRO_NOSSL + auto client = std::make_unique(); +#else + auto client = std::make_unique(); + client->setInsecure(); +#endif + if (client) client->setTimeout(TCP_CONNECTION_TIMEOUT_VALUE); + return client; + } + + bool setupHttpConnection(HTTPClient& http, WiFiClient* client, const char* path) { +#ifdef SINRICPRO_NOSSL + return http.begin(*client, SINRICPRO_CAMERA_URL, 80, path, false); +#else + return http.begin(*client, SINRICPRO_CAMERA_URL, 443, path, true); +#endif + } + + void setupHttpHeaders(HTTPClient& http, T* device) { + const String& deviceId = device->getDeviceId(); + String createdAt = String(device->getTimestamp()); + String signature = device->sign(deviceId + createdAt); + + http.addHeader(FSTR_SINRICPRO_deviceId, deviceId); + http.addHeader(FSTR_SINRICPRO_createdAt, createdAt); + http.addHeader(FSTR_SINRICPRO_signature, signature); + http.setTimeout(HTTP_TIMEOUT_VALUE); + } +#endif }; template -CameraController::CameraController() { +CameraController::CameraController() +: event_limiter (EVENT_LIMIT_STATE) { T *device = static_cast(this); device->registerRequestHandler(std::bind(&CameraController::handleCameraController, this, std::placeholders::_1)); } @@ -66,6 +111,7 @@ void CameraController::onSnapshot(SnapshotCallback cb) { template bool CameraController::handleCameraController(SinricProRequest &request) { + if (event_limiter) return false; T *device = static_cast(this); bool success = false; @@ -80,46 +126,60 @@ bool CameraController::handleCameraController(SinricProRequest &request) { template int CameraController::sendSnapshot(uint8_t* buffer, size_t len) { - int resCode = -1; - + #if defined(ESP32) - T* device = static_cast(this); - - if (!buffer) return resCode; + if (!buffer || len == 0) return -1; + T* device = static_cast(this); HTTPClient http; - bool beginSuccess = false; + auto client = createClient(); + + if (!setupHttpConnection(http, client.get(), SINRICPRO_CAMERA_API_SNAPSHOT_PATH)) { + http.end(); + return -1; + } -#ifdef SINRICPRO_NOSSL - std::unique_ptr client = std::make_unique(); - if (!client) return resCode; - beginSuccess = http.begin(*client, SINRICPRO_CAMERA_URL, 80, SINRICPRO_CAMERA_PATH, false); + setupHttpHeaders(http, device); + + int resCode = http.POST(buffer, len); + http.end(); + + return resCode; #else - std::unique_ptr secureClient = std::make_unique(); - if (!secureClient) return resCode; - secureClient->setInsecure(); - beginSuccess = http.begin(*secureClient, SINRICPRO_CAMERA_URL, 443, SINRICPRO_CAMERA_PATH, true); -#endif + return -1; +#endif +} + +template +int CameraController::sendMotion(fs::FS &fs, const char * path) { + +#if defined(ESP32) + File file = fs.open(path); + if (!file) return -1; - if (!beginSuccess) { + T* device = static_cast(this); + HTTPClient http; + auto client = createClient(); + + if (!setupHttpConnection(http, client.get(), SINRICPRO_CAMERA_API_MOTION_PATH)) { http.end(); - return resCode; + return -1; } - const String& deviceId = device->getDeviceId(); - String createdAt = String(device->getTimestamp()); - String signature = device->sign(deviceId + createdAt); - - http.addHeader(FSTR_SINRICPRO_deviceId, deviceId); - http.addHeader(FSTR_SINRICPRO_createdAt, createdAt); - http.addHeader(FSTR_SINRICPRO_signature, signature); + setupHttpHeaders(http, device); - resCode = http.POST(buffer, len); + int resCode = http.sendRequest(FSTR_CAMERA_POST, &file, file.size()); + + file.close(); http.end(); -#endif return resCode; +#else + return -1; +#endif } +} // namespace SINRICPRO_NAMESPACE -} // namespace SINRICPRO_NAMESPACE \ No newline at end of file +template +using CameraController = SINRICPRO_NAMESPACE::CameraController; \ No newline at end of file diff --git a/src/SinricProCamera.h b/src/SinricProCamera.h index cc85494..cb19a5f 100644 --- a/src/SinricProCamera.h +++ b/src/SinricProCamera.h @@ -18,7 +18,7 @@ namespace SINRICPRO_NAMESPACE { /** * @class SinricProCamera - * @brief Camera suporting basic on / off command + * @brief Camera suporting snapshots, motions and on / off commands * @ingroup Devices **/ class SinricProCamera : public SinricProDevice, diff --git a/src/SinricProConfig.h b/src/SinricProConfig.h index c50d005..2a36d14 100644 --- a/src/SinricProConfig.h +++ b/src/SinricProConfig.h @@ -41,8 +41,12 @@ #define SINRICPRO_CAMERA_URL "camera.sinric.pro" #endif -#ifndef SINRICPRO_CAMERA_PATH -#define SINRICPRO_CAMERA_PATH "/snapshot" +#ifndef SINRICPRO_CAMERA_API_SNAPSHOT_PATH +#define SINRICPRO_CAMERA_API_SNAPSHOT_PATH "/snapshot" +#endif + +#ifndef SINRICPRO_CAMERA_API_MOTION_PATH +#define SINRICPRO_CAMERA_API_MOTION_PATH "/motion-capture" #endif // UDP Configuration @@ -75,3 +79,12 @@ #ifndef EVENT_LIMIT_SENSOR_VALUE #define EVENT_LIMIT_SENSOR_VALUE 60000 #endif + +// Used when making a HTTP request in API +#ifndef TCP_CONNECTION_TIMEOUT_VALUE +#define TCP_CONNECTION_TIMEOUT_VALUE 5000 +#endif + +#ifndef HTTP_TIMEOUT_VALUE +#define HTTP_TIMEOUT_VALUE 10000 +#endif