help with camera webserver code

harish
Posts: 2
Joined: Wed Jan 11, 2023 11:07 am

help with camera webserver code

Postby harish » Mon Jan 16, 2023 10:15 am

I am new to this, I was trying to understand live stream code but I am not able to understand some lines of code can someone tell me what they mean?
  1.  
  2. #include "esp_camera.h"
  3. #include <WiFi.h>
  4. #include "esp_timer.h"
  5. #include "img_converters.h"
  6. #include "Arduino.h"
  7. #include "SPI.h"
  8. #include "fb_gfx.h"
  9. #include "soc/soc.h"          //--> disable brownout problems
  10. #include "soc/rtc_cntl_reg.h" //--> disable brownout problems
  11. #include "esp_http_server.h"
  12.  
  13. #define CAMERA_MODEL_AI_THINKER
  14.  
  15. #define PWDN_GPIO_NUM 32
  16. #define RESET_GPIO_NUM -1
  17. #define XCLK_GPIO_NUM 0
  18. #define SIOD_GPIO_NUM 26
  19. #define SIOC_GPIO_NUM 27
  20.  
  21. #define Y9_GPIO_NUM 35
  22. #define Y8_GPIO_NUM 34
  23. #define Y7_GPIO_NUM 39
  24. #define Y6_GPIO_NUM 36
  25. #define Y5_GPIO_NUM 21
  26. #define Y4_GPIO_NUM 19
  27. #define Y3_GPIO_NUM 18
  28. #define Y2_GPIO_NUM 5
  29. #define VSYNC_GPIO_NUM 25
  30. #define HREF_GPIO_NUM 23
  31. #define PCLK_GPIO_NUM 22
  32.  
  33. const char *ssid = "*********";
  34. const char *password = "********";
  35.  
  36. #define PART_BOUNDARY "123456789000000000000987654321"
  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.  
  42.  httpd_handle_t index_httpd = NULL;
  43.  httpd_handle_t stream_httpd = NULL;
  44.  
  45. static const char PROGMEM INDEX_HTML[] = R"rawliteral(
  46. <html>
  47. <head>
  48.    <title>ESP32-CAM</title>
  49.    <meta name="viewport" content="width=device-width, initial-scale=1">
  50.    <style>
  51.        body {
  52.            font-family: Arial;
  53.            text-align: center;
  54.            margin: 0px auto;
  55.            padding-top: 30px;
  56.        }
  57.        /* ----------------------------------- Stream and Capture Viewer
  58.        img {
  59.            width: auto;
  60.            max-width: 100%;
  61.            height: auto;
  62.        }
  63.         ----------------------------------- */
  64.    </style>
  65. </head>
  66. <body>
  67.    <h3>ESP32-CAM</h3>
  68.    <img src="" id="photo">
  69.    <script>
  70.        /* ----------------------------------- Calls the video stream link and displays it */
  71.        window.onload = document.getElementById("photo").src = window.location.href.slice(0, -1) + ":81/stream";
  72.    </script>
  73. </body>
  74. </html>
  75. )rawliteral";
  76.  
  77. static esp_err_t index_handler(httpd_req_t *req)
  78. {
  79.     httpd_resp_set_type(req, "text/html");
  80.     return httpd_resp_send(req, (const char *)INDEX_HTML, strlen(INDEX_HTML));
  81. }
  82.  
  83. static esp_err_t stream_handler(httpd_req_t *req)
  84. {
  85.  
  86.     camera_fb_t *fb = NULL;
  87.     esp_err_t res = ESP_OK;
  88.     size_t _jpg_buf_len = 0;
  89.     uint8_t *_jpg_buf = NULL;
  90.     char *part_buf[64];
  91.  
  92.     res = httpd_resp_set_type(req, _STREAM_CONTENT_TYPE);
  93.     if (res != ESP_OK)
  94.     {
  95.         return res;
  96.     }
  97.  
  98.     while (true)
  99.     {
  100.         fb = esp_camera_fb_get();
  101.         if (!fb)
  102.         {
  103.             Serial.println("Camera capture failed");
  104.             res = ESP_FAIL;
  105.         }
  106.         else
  107.         {
  108.             if (fb->width > 400)
  109.             {
  110.                 if (fb->format != PIXFORMAT_JPEG)
  111.                 {
  112.                     bool jpeg_converted = frame2jpg(fb, 80, &_jpg_buf, &_jpg_buf_len);
  113.                     esp_camera_fb_return(fb);
  114.                     fb = NULL;
  115.                     if (!jpeg_converted)
  116.                     {
  117.                         Serial.println("JPEG compression failed");
  118.                         res = ESP_FAIL;
  119.                     }
  120.                 }
  121.                 else
  122.                 {
  123.                     _jpg_buf_len = fb->len;
  124.                     _jpg_buf = fb->buf;
  125.                 }
  126.             }
  127.         }
  128.         if (res == ESP_OK)
  129.         {
  130.             size_t hlen = snprintf((char *)part_buf, 64, _STREAM_PART, _jpg_buf_len);
  131.             res = httpd_resp_send_chunk(req, (const char *)part_buf, hlen);
  132.         }
  133.         if (res == ESP_OK)
  134.         {
  135.             res = httpd_resp_send_chunk(req, (const char *)_jpg_buf, _jpg_buf_len);
  136.         }
  137.         if (res == ESP_OK)
  138.         {
  139.             res = httpd_resp_send_chunk(req, _STREAM_BOUNDARY, strlen(_STREAM_BOUNDARY));
  140.         }
  141.         if (fb)
  142.         {
  143.             esp_camera_fb_return(fb);
  144.             fb = NULL;
  145.             _jpg_buf = NULL;
  146.         }
  147.         else if (_jpg_buf)
  148.         {
  149.             free(_jpg_buf);
  150.             _jpg_buf = NULL;
  151.         }
  152.         if (res != ESP_OK)
  153.         {
  154.             break;
  155.         }
  156.     }
  157.     return res;
  158. }
  159.  
  160. void startCameraServer()
  161. {
  162.     httpd_config_t config = HTTPD_DEFAULT_CONFIG();
  163.     config.server_port = 80;
  164.  
  165.     httpd_uri_t index_uri = {
  166.         .uri = "/",
  167.         .method = HTTP_GET,
  168.         .handler = index_handler,
  169.         .user_ctx = NULL};
  170.  
  171.     httpd_uri_t stream_uri = {
  172.         .uri = "/stream",
  173.         .method = HTTP_GET,
  174.         .handler = stream_handler,
  175.         .user_ctx = NULL};
  176.  
  177.     if (httpd_start(&index_httpd, &config) == ESP_OK)
  178.     {
  179.         httpd_register_uri_handler(index_httpd, &index_uri);
  180.     }
  181.  
  182.     config.server_port += 1;
  183.     config.ctrl_port += 1;
  184.     if (httpd_start(&stream_httpd, &config) == ESP_OK)
  185.     {
  186.         httpd_register_uri_handler(stream_httpd, &stream_uri);
  187.     }
  188.  
  189.     Serial.println();
  190.     Serial.println("Camera Server started successfully");
  191.     Serial.print("Camera Stream Ready! Go to: http://");
  192.     Serial.print(WiFi.localIP());
  193.     Serial.println();
  194. }
  195.  
  196. void setup()
  197. {
  198.     WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); //--> disable brownout detector
  199.  
  200.     Serial.begin(115200);
  201.     Serial.println();
  202.     delay(1000);
  203.     Serial.setDebugOutput(false);
  204.  
  205.     camera_config_t config;
  206.     config.ledc_channel = LEDC_CHANNEL_0;
  207.     config.ledc_timer = LEDC_TIMER_0;
  208.     config.pin_d0 = Y2_GPIO_NUM;
  209.     config.pin_d1 = Y3_GPIO_NUM;
  210.     config.pin_d2 = Y4_GPIO_NUM;
  211.     config.pin_d3 = Y5_GPIO_NUM;
  212.     config.pin_d4 = Y6_GPIO_NUM;
  213.     config.pin_d5 = Y7_GPIO_NUM;
  214.     config.pin_d6 = Y8_GPIO_NUM;
  215.     config.pin_d7 = Y9_GPIO_NUM;
  216.     config.pin_xclk = XCLK_GPIO_NUM;
  217.     config.pin_pclk = PCLK_GPIO_NUM;
  218.     config.pin_vsync = VSYNC_GPIO_NUM;
  219.     config.pin_href = HREF_GPIO_NUM;
  220.     config.pin_sscb_sda = SIOD_GPIO_NUM;
  221.     config.pin_sscb_scl = SIOC_GPIO_NUM;
  222.     config.pin_pwdn = PWDN_GPIO_NUM;
  223.     config.pin_reset = RESET_GPIO_NUM;
  224.     config.xclk_freq_hz = 20000000;
  225.     config.pixel_format = PIXFORMAT_JPEG;
  226.  
  227.     config.frame_size = FRAMESIZE_SVGA;
  228.     config.jpeg_quality = 12;
  229.     config.fb_count = 1;
  230.  
  231.     esp_err_t err = esp_camera_init(&config);
  232.     if (err != ESP_OK)
  233.     {
  234.         Serial.printf("Camera init failed with error 0x%x", err);
  235.         return;
  236.     }
  237.     delay(1000);
  238.     pinMode(33, OUTPUT); //--> RED LED on Board
  239.  
  240.     Serial.println();
  241.     WiFi.begin(ssid, password);
  242.     while (WiFi.status() != WL_CONNECTED)
  243.     {
  244.         digitalWrite(33, HIGH);
  245.         delay(250);
  246.         digitalWrite(33, LOW);
  247.         delay(250);
  248.         Serial.print(".");
  249.     }
  250.  
  251.     digitalWrite(4, LOW);
  252.     delay(1000);
  253.     Serial.println("");
  254.     Serial.println("WiFi connected");
  255.  
  256.     startCameraServer(); //--> Start camera web server
  257. }
  258.  
  259. void loop()
  260. {
  261.     delay(1);
  262. }
i need help with static esp_err_t stream_handler() function and from line 36-39
can someone explain this to me

Who is online

Users browsing this forum: No registered users and 77 guests