Page 1 of 1

ESP32-CAM live streaming and LED-control

Posted: Sun Mar 12, 2023 3:11 pm
by PyPat2021
Hello there,

I could really need some help with one of my projects. I am somewhat experienced with Python but I don't really have experience with C(++).
I am trying to have my ESP32-Cam live streaming via soft access point whilst switching flash LED on and off (later I would like to control this LED via HTTP-request). First of all: The live streaming part with soft access works fine for me. But I don't know at which line of the code I should write the LED control. I would like to switch flash-LED (GPIO4) on and off every second.

My live streaming code via soft AP is:
  1. #include "esp_camera.h"
  2. #include <WiFi.h>
  3. #include "esp_timer.h"
  4. #include "img_converters.h"
  5. #include "Arduino.h"
  6. #include "fb_gfx.h"
  7. #include "soc/soc.h" //disable brownout problems
  8. #include "soc/rtc_cntl_reg.h"  //disable brownout problems
  9. #include "esp_http_server.h"
  10.  
  11. // Replace with your network credentials
  12. const char* ssid     = "ESP32-Access-Point"; //Name des Arduino-WiFi's
  13. const char* password = "123456789"; //Passwort für das Arduino-WiFi
  14. int max_connection = 1; //maximale Anzahl gleichzeitiger Clients im Arduino-Netzwerk
  15.  
  16. #define PART_BOUNDARY "123456789000000000000987654321"
  17.  
  18. #define PWDN_GPIO_NUM     32
  19. #define RESET_GPIO_NUM    -1
  20. #define XCLK_GPIO_NUM      0
  21. #define SIOD_GPIO_NUM     26
  22. #define SIOC_GPIO_NUM     27
  23.  
  24. #define Y9_GPIO_NUM       35
  25. #define Y8_GPIO_NUM       34
  26. #define Y7_GPIO_NUM       39
  27. #define Y6_GPIO_NUM       36
  28. #define Y5_GPIO_NUM       21
  29. #define Y4_GPIO_NUM       19
  30. #define Y3_GPIO_NUM       18
  31. #define Y2_GPIO_NUM        5
  32. #define VSYNC_GPIO_NUM    25
  33. #define HREF_GPIO_NUM     23
  34. #define PCLK_GPIO_NUM     22
  35.  
  36.  
  37. static const char* _STREAM_CONTENT_TYPE = "multipart/x-mixed-replace;boundary=" PART_BOUNDARY;
  38. static const char* _STREAM_BOUNDARY = "\r\n--" PART_BOUNDARY "\r\n";
  39. static const char* _STREAM_PART = "Content-Type: image/jpeg\r\nContent-Length: %u\r\n\r\n";
  40.  
  41. httpd_handle_t stream_httpd = NULL;
  42.  
  43. static esp_err_t stream_handler(httpd_req_t *req){
  44.   camera_fb_t * fb = NULL;
  45.   esp_err_t res = ESP_OK;
  46.   size_t _jpg_buf_len = 0;
  47.   uint8_t * _jpg_buf = NULL;
  48.   char * part_buf[64];
  49.  
  50.   res = httpd_resp_set_type(req, _STREAM_CONTENT_TYPE);
  51.   if(res != ESP_OK){
  52.     return res;
  53.   }
  54.  
  55.   while(true){
  56.     fb = esp_camera_fb_get();
  57.     if (!fb) {
  58.       Serial.println("Camera capture failed");
  59.       res = ESP_FAIL;
  60.     } else {
  61.       if(fb->width > 400){
  62.         if(fb->format != PIXFORMAT_JPEG){
  63.           bool jpeg_converted = frame2jpg(fb, 80, &_jpg_buf, &_jpg_buf_len);
  64.           esp_camera_fb_return(fb);
  65.           fb = NULL;
  66.           if(!jpeg_converted){
  67.             Serial.println("JPEG compression failed");
  68.             res = ESP_FAIL;
  69.           }
  70.         } else {
  71.           _jpg_buf_len = fb->len;
  72.           _jpg_buf = fb->buf;
  73.         }
  74.       }
  75.     }
  76.     if(res == ESP_OK){
  77.       size_t hlen = snprintf((char *)part_buf, 64, _STREAM_PART, _jpg_buf_len);
  78.       res = httpd_resp_send_chunk(req, (const char *)part_buf, hlen);
  79.     }
  80.     if(res == ESP_OK){
  81.       res = httpd_resp_send_chunk(req, (const char *)_jpg_buf, _jpg_buf_len);
  82.     }
  83.     if(res == ESP_OK){
  84.       res = httpd_resp_send_chunk(req, _STREAM_BOUNDARY, strlen(_STREAM_BOUNDARY));
  85.     }
  86.     if(fb){
  87.       esp_camera_fb_return(fb);
  88.       fb = NULL;
  89.       _jpg_buf = NULL;
  90.     } else if(_jpg_buf){
  91.       free(_jpg_buf);
  92.       _jpg_buf = NULL;
  93.     }
  94.     if(res != ESP_OK){
  95.       break;
  96.     }
  97.     //Serial.printf("MJPG: %uB\n",(uint32_t)(_jpg_buf_len));
  98.   }
  99.   return res;
  100. }
  101.  
  102. void startCameraServer(){
  103.   httpd_config_t config = HTTPD_DEFAULT_CONFIG();
  104.   config.server_port = 80;
  105.  
  106.   httpd_uri_t index_uri = {
  107.     .uri       = "/",
  108.     .method    = HTTP_GET,
  109.     .handler   = stream_handler,
  110.     .user_ctx  = NULL
  111.   };
  112.  
  113.   //Serial.printf("Starting web server on port: '%d'\n", config.server_port);
  114.   if (httpd_start(&stream_httpd, &config) == ESP_OK) {
  115.     httpd_register_uri_handler(stream_httpd, &index_uri);
  116.   }
  117. }
  118.  
  119. void setup() {
  120.  
  121.   WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); //disable brownout detector
  122.  
  123.   Serial.begin(115200);
  124.   Serial.setDebugOutput(false);
  125.  
  126.   camera_config_t config;
  127.   config.ledc_channel = LEDC_CHANNEL_0;
  128.   config.ledc_timer = LEDC_TIMER_0;
  129.   config.pin_d0 = Y2_GPIO_NUM;
  130.   config.pin_d1 = Y3_GPIO_NUM;
  131.   config.pin_d2 = Y4_GPIO_NUM;
  132.   config.pin_d3 = Y5_GPIO_NUM;
  133.   config.pin_d4 = Y6_GPIO_NUM;
  134.   config.pin_d5 = Y7_GPIO_NUM;
  135.   config.pin_d6 = Y8_GPIO_NUM;
  136.   config.pin_d7 = Y9_GPIO_NUM;
  137.   config.pin_xclk = XCLK_GPIO_NUM;
  138.   config.pin_pclk = PCLK_GPIO_NUM;
  139.   config.pin_vsync = VSYNC_GPIO_NUM;
  140.   config.pin_href = HREF_GPIO_NUM;
  141.   config.pin_sscb_sda = SIOD_GPIO_NUM;
  142.   config.pin_sscb_scl = SIOC_GPIO_NUM;
  143.   config.pin_pwdn = PWDN_GPIO_NUM;
  144.   config.pin_reset = RESET_GPIO_NUM;
  145.   config.xclk_freq_hz = 20000000;
  146.   config.pixel_format = PIXFORMAT_JPEG;
  147.  
  148.   if(psramFound()){
  149.     config.frame_size = FRAMESIZE_UXGA;
  150.     config.jpeg_quality = 10;
  151.     config.fb_count = 2;
  152.   } else {
  153.     config.frame_size = FRAMESIZE_SVGA;
  154.     config.jpeg_quality = 12;
  155.     config.fb_count = 1;
  156.   }
  157.  
  158.   // Camera init
  159.   esp_err_t err = esp_camera_init(&config);
  160.   if (err != ESP_OK) {
  161.     Serial.printf("Camera init failed with error 0x%x", err);
  162.     return;
  163.   }
  164.   // Wi-Fi connection
  165.   WiFi.softAP(ssid, password, max_connection);
  166.  
  167.   IPAddress IP(192, 168, 4, 2); //Eine IP-Adresse für den Arduino festlegen
  168.     IPAddress NMask(255, 255, 255, 0);
  169.   WiFi.softAPConfig(IP, IP, NMask); //Configure ESP32 to apply the defined IP address from above
  170.   IPAddress local_IP = WiFi.softAPIP();
  171.  
  172.   Serial.print("Camera Stream Ready! Go to: http://");
  173.   Serial.print(local_IP);
  174.  
  175.   // Start streaming web server
  176.   startCameraServer();
  177. }
  178.  
  179. void loop() {
  180. delay(1);
  181. }
Can anybody please help me where to put further code without crashing the working video-stream part?
Thank you so much in advance!

Greetings from Germany.

Re: ESP32-CAM live streaming and LED-control

Posted: Mon Mar 13, 2023 1:55 pm
by noweare
I think most of the example code for the esp-camera includes the flash led with a #define to include the code or not.

Here is the link to the example code. You will have to go through the code and copy paste into your code anything
that has to do with the flash led stuff.

https://github.com/espressif/arduino-es ... _httpd.cpp

Here is a snippit:
// Enable LED FLASH setting
#define CONFIG_LED_ILLUMINATOR_ENABLED 1

// LED FLASH setup
#if CONFIG_LED_ILLUMINATOR_ENABLED

#define LED_LEDC_CHANNEL 2 //Using different ledc channel/timer than camera
#define CONFIG_LED_MAX_INTENSITY 255

int led_duty = 0;
bool isStreaming = false;

#endif