From 7ff7fb06f5a0f2ef99f57430fc575473c7aa5bfa Mon Sep 17 00:00:00 2001 From: Miroslav Pivovarsky Date: Thu, 27 Jun 2024 22:16:15 +0200 Subject: [PATCH] added WEB api --- ESP32_PrusaConnectCam/WebServer.cpp | 46 +++++++++++++++++++++++++++ ESP32_PrusaConnectCam/camera.cpp | 8 ++--- ESP32_PrusaConnectCam/connect_types.h | 11 +++++++ ESP32_PrusaConnectCam/mcu_cfg.h | 2 +- 4 files changed, 61 insertions(+), 6 deletions(-) diff --git a/ESP32_PrusaConnectCam/WebServer.cpp b/ESP32_PrusaConnectCam/WebServer.cpp index 9e189cb..a6994f8 100644 --- a/ESP32_PrusaConnectCam/WebServer.cpp +++ b/ESP32_PrusaConnectCam/WebServer.cpp @@ -428,6 +428,52 @@ void Server_InitWebServer_Actions() { request->send_P(200, "text/plain", "Change LED status"); }); + /* route for change LED status */ + server.on("/light", HTTP_GET, [](AsyncWebServerRequest* request) { + SystemLog.AddEvent(LogLevel_Verbose, F("WEB server: /light set LED status")); + if (Server_CheckBasicAuth(request) == false) + return; + + if (request->hasArg("on")) { + SystemLog.AddEvent(LogLevel_Verbose, F("Turning light ON")); + SystemCamera.SetFlashStatus(true); + SystemCamera.SetCameraFlashEnable(false); + request->send_P(200, "text/plain", "Light ON"); + + } else if (request->hasArg("off")) { + SystemLog.AddEvent(LogLevel_Verbose, F("Turning light OFF")); + SystemCamera.SetFlashStatus(false); + SystemCamera.SetCameraFlashEnable(false); + request->send_P(200, "text/plain", "Light OFF"); + + } else { + request->send_P(400, "text/plain", "Invalid request"); + } + }); + + /* route for change FLASH status */ + server.on("/flash", HTTP_GET, [](AsyncWebServerRequest* request) { + SystemLog.AddEvent(LogLevel_Verbose, F("WEB server: /flash set flash status")); + if (Server_CheckBasicAuth(request) == false) + return; + + if (request->hasArg("on")) { + SystemLog.AddEvent(LogLevel_Verbose, F("Turning flash ON")); + SystemCamera.SetCameraFlashEnable(true); + SystemCamera.SetFlashStatus(false); + request->send_P(200, "text/plain", "Flash ON"); + + } else if (request->hasArg("off")) { + SystemLog.AddEvent(LogLevel_Verbose, F("Turning flash OFF")); + SystemCamera.SetCameraFlashEnable(false); + SystemCamera.SetFlashStatus(false); + request->send_P(200, "text/plain", "Flash OFF"); + + } else { + request->send_P(400, "text/plain", "Invalid request"); + } + }); + /* reboot MCU */ server.on("/action_reboot", HTTP_GET, [](AsyncWebServerRequest* request) { SystemLog.AddEvent(LogLevel_Verbose, F("WEB server: /action_reboo reboot MCU!")); diff --git a/ESP32_PrusaConnectCam/camera.cpp b/ESP32_PrusaConnectCam/camera.cpp index 20b6afe..77be7dd 100644 --- a/ESP32_PrusaConnectCam/camera.cpp +++ b/ESP32_PrusaConnectCam/camera.cpp @@ -49,7 +49,7 @@ void Camera::Init() { log->AddEvent(LogLevel_Info, F("Init GPIO")); ledcAttach(FLASH_GPIO_NUM, FLASH_PWM_FREQ, FLASH_PWM_RESOLUTION); - ledcWrite(FLASH_GPIO_NUM, FLASH_OFF_STATUS); + SetFlashStatus(false); InitCameraModule(); ApplyCameraCfg(); @@ -117,7 +117,6 @@ void Camera::InitCameraModule() { /* Camera init */ err = esp_camera_init(&CameraConfig); - if (err != ESP_OK) { log->AddEvent(LogLevel_Warning, F("Camera init failed. Error: "), String(err, HEX)); log->AddEvent(LogLevel_Warning, F("Reset ESP32-cam!")); @@ -333,7 +332,7 @@ void Camera::CapturePhoto() { CameraCaptureSuccess = false; /* check flash, and enable FLASH LED */ if (true == CameraFlashEnable) { - ledcWrite(FLASH_GPIO_NUM, FLASH_ON_STATUS); + SetFlashStatus(true); delay(CameraFlashTime); } @@ -401,8 +400,7 @@ void Camera::CapturePhoto() { /* Disable flash */ if (true == CameraFlashEnable) { - //delay(CameraFlashTime); - ledcWrite(FLASH_GPIO_NUM, FLASH_OFF_STATUS); + SetFlashStatus(false); } xSemaphoreGive(frameBufferSemaphore); diff --git a/ESP32_PrusaConnectCam/connect_types.h b/ESP32_PrusaConnectCam/connect_types.h index 18c2638..828ec75 100644 --- a/ESP32_PrusaConnectCam/connect_types.h +++ b/ESP32_PrusaConnectCam/connect_types.h @@ -1,3 +1,14 @@ +/** + @file connect_types.h + + @brief Here are defined types for communication with prusa connect backend + + @author Miroslav Pivovarsky + Contact: miroslav.pivovarsky@gmail.com + + @bug: no know bug +*/ + #pragma once /** diff --git a/ESP32_PrusaConnectCam/mcu_cfg.h b/ESP32_PrusaConnectCam/mcu_cfg.h index 4a8825c..bc68134 100644 --- a/ESP32_PrusaConnectCam/mcu_cfg.h +++ b/ESP32_PrusaConnectCam/mcu_cfg.h @@ -18,7 +18,7 @@ //#define ESP32_WROVER_DEV /* ---------------- BASIC MCU CFG --------------*/ -#define SW_VERSION "1.0.3" ///< SW version +#define SW_VERSION "1.1.0-rc1" ///< SW version #define SW_BUILD __DATE__ " " __TIME__ ///< build number #define CONSOLE_VERBOSE_DEBUG false ///< enable/disable verbose debug log level for console #define DEVICE_HOSTNAME "Prusa-ESP32cam" ///< device hostname