ESP32-­WROOM-­32E + ArduCam OV2640 Video stream problem

Mires98
Posts: 1
Joined: Fri Mar 25, 2022 12:54 pm

ESP32-­WROOM-­32E + ArduCam OV2640 Video stream problem

Postby Mires98 » Fri Mar 25, 2022 1:56 pm

Hello,

I am working on a robot using video stream.
My components:
- ESP32-DevKitC-32E V4 (ESP32-­WROOM-­32E)
- Camera module ArduCam OV2640, 2 MPx, 20 pins
I have a problem with video image. On the picture are black blinking pixels.
Screanshot:
Image
Connection diagram:
Image
Code:
  1. #include "esp_camera.h"
  2. #include <Arduino.h>
  3. #include <WiFi.h>
  4. #include <AsyncTCP.h>
  5. #include <ESPAsyncWebServer.h>
  6. #include <iostream>
  7. #include <sstream>
  8.  
  9.  
  10. //Camera related constants
  11. #define PWDN_GPIO_NUM     -1
  12. #define RESET_GPIO_NUM    17
  13. #define XCLK_GPIO_NUM     27
  14. #define SIOD_GPIO_NUM     21
  15. #define SIOC_GPIO_NUM     22
  16.  
  17. #define Y9_GPIO_NUM       19
  18. #define Y8_GPIO_NUM       36
  19. #define Y7_GPIO_NUM       18
  20. #define Y6_GPIO_NUM       39
  21. #define Y5_GPIO_NUM        5
  22. #define Y4_GPIO_NUM       34
  23. #define Y3_GPIO_NUM       35
  24. #define Y2_GPIO_NUM       32
  25. #define VSYNC_GPIO_NUM    25
  26. #define HREF_GPIO_NUM     26
  27. #define PCLK_GPIO_NUM     23
  28.  
  29.  
  30. const char* ssid     = "MyWiFiCar";
  31. const char* password = "12345678";
  32.  
  33. AsyncWebServer server(80);
  34. AsyncWebSocket wsCamera("/Camera");
  35. uint32_t cameraClientId = 0;
  36.  
  37. const char* htmlHomePage PROGMEM = R"HTMLHOMEPAGE(
  38. <!DOCTYPE html>
  39. <html>
  40.  <head>
  41.  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
  42.    <style>
  43.    </style>
  44.  
  45.  </head>
  46.  <body align="center">
  47.    
  48.    <table id="mainTable" style="width:400px;margin:auto;table-layout:fixed" CELLSPACING=10>
  49.      <tr>
  50.        <img id="cameraImage" src="" style="width:400px;height:250px"></td>
  51.      </tr>
  52.    </table>
  53.  
  54.    <script>
  55.      var webSocketCameraUrl = "ws:\/\/" + window.location.hostname + "/Camera";    
  56.      var websocketCamera;
  57.      
  58.      function initCameraWebSocket()
  59.      {
  60.        websocketCamera = new WebSocket(webSocketCameraUrl);
  61.        websocketCamera.binaryType = 'blob';
  62.        websocketCamera.onopen    = function(event){};
  63.        websocketCamera.onclose   = function(event){setTimeout(initCameraWebSocket, 2000);};
  64.        websocketCamera.onmessage = function(event)
  65.        {
  66.          var imageId = document.getElementById("cameraImage");
  67.          imageId.src = URL.createObjectURL(event.data);
  68.        };
  69.      }
  70.      
  71.      function initWebSocket()
  72.      {
  73.        initCameraWebSocket ();
  74.      }
  75.    
  76.      window.onload = initWebSocket;
  77.      document.getElementById("mainTable").addEventListener("touchend", function(event){
  78.        event.preventDefault()
  79.      });      
  80.    </script>
  81.  </body>    
  82. </html>
  83. )HTMLHOMEPAGE";
  84.  
  85. void handleRoot(AsyncWebServerRequest *request)
  86. {
  87.   request->send_P(200, "text/html", htmlHomePage);
  88. }
  89.  
  90. void handleNotFound(AsyncWebServerRequest *request)
  91. {
  92.     request->send(404, "text/plain", "File Not Found");
  93. }
  94.  
  95. void onCameraWebSocketEvent(AsyncWebSocket *server,
  96.                       AsyncWebSocketClient *client,
  97.                       AwsEventType type,
  98.                       void *arg,
  99.                       uint8_t *data,
  100.                       size_t len)
  101. {                      
  102.   switch (type)
  103.   {
  104.     case WS_EVT_CONNECT:
  105.       Serial.printf("WebSocket client #%u connected from %s\n", client->id(), client->remoteIP().toString().c_str());
  106.       cameraClientId = client->id();
  107.       break;
  108.     case WS_EVT_DISCONNECT:
  109.       Serial.printf("WebSocket client #%u disconnected\n", client->id());
  110.       cameraClientId = 0;
  111.       break;
  112.     case WS_EVT_DATA:
  113.       break;
  114.     case WS_EVT_PONG:
  115.     case WS_EVT_ERROR:
  116.       break;
  117.     default:
  118.       break;  
  119.   }
  120. }
  121.  
  122. void setupCamera()
  123. {
  124.   camera_config_t config;
  125.   config.ledc_channel = LEDC_CHANNEL_0;
  126.   config.ledc_timer = LEDC_TIMER_0;
  127.   config.pin_d0 = Y2_GPIO_NUM;
  128.   config.pin_d1 = Y3_GPIO_NUM;
  129.   config.pin_d2 = Y4_GPIO_NUM;
  130.   config.pin_d3 = Y5_GPIO_NUM;
  131.   config.pin_d4 = Y6_GPIO_NUM;
  132.   config.pin_d5 = Y7_GPIO_NUM;
  133.   config.pin_d6 = Y8_GPIO_NUM;
  134.   config.pin_d7 = Y9_GPIO_NUM;
  135.   config.pin_xclk = XCLK_GPIO_NUM;
  136.   config.pin_pclk = PCLK_GPIO_NUM;
  137.   config.pin_vsync = VSYNC_GPIO_NUM;
  138.   config.pin_href = HREF_GPIO_NUM;
  139.   config.pin_sscb_sda = SIOD_GPIO_NUM;
  140.   config.pin_sscb_scl = SIOC_GPIO_NUM;
  141.   config.pin_pwdn = PWDN_GPIO_NUM;
  142.   config.pin_reset = RESET_GPIO_NUM;
  143.   config.xclk_freq_hz = 20000000;
  144.   config.pixel_format = PIXFORMAT_JPEG;
  145.  
  146.   config.frame_size = FRAMESIZE_VGA;
  147.   config.jpeg_quality = 12;
  148.   config.fb_count = 1;
  149.   config.fb_location = CAMERA_FB_IN_DRAM;
  150.  
  151.  
  152.   // camera init
  153.   esp_err_t err = esp_camera_init(&config);
  154.   if (err != ESP_OK)
  155.   {
  156.     Serial.printf("Camera init failed with error 0x%x", err);
  157.     return;
  158.   }  
  159.  
  160.   if (psramFound())
  161.   {
  162.     heap_caps_malloc_extmem_enable(20000);  
  163.     Serial.printf("PSRAM initialized. malloc to take memory from Psram above this size");    
  164.   }  
  165. }
  166.  
  167. void sendCameraPicture()
  168. {
  169.   if (cameraClientId == 0)
  170.   {
  171.     return;
  172.   }
  173.   unsigned long  startTime1 = millis();
  174.   //capture a frame
  175.   camera_fb_t * fb = esp_camera_fb_get();
  176.   if (!fb)
  177.   {
  178.       Serial.println("Frame buffer could not be acquired");
  179.       return;
  180.   }
  181.  
  182.   unsigned long  startTime2 = millis();
  183.   wsCamera.binary(cameraClientId, fb->buf, fb->len);
  184.   esp_camera_fb_return(fb);
  185.    
  186.   //Wait for message to be delivered
  187.   while (true)
  188.   {
  189.     AsyncWebSocketClient * clientPointer = wsCamera.client(cameraClientId);
  190.     if (!clientPointer || !(clientPointer->queueIsFull()))
  191.     {
  192.       break;
  193.     }
  194.     delay(1);
  195.   }
  196.  
  197.   unsigned long  startTime3 = millis();  
  198.   //Serial.printf("Time taken Total: %d|%d|%d\n",startTime3 - startTime1, startTime2 - startTime1, startTime3-startTime2 );
  199. }
  200.  
  201. void setup(void)
  202. {
  203.   Serial.begin(115200);
  204.  
  205.   WiFi.softAP(ssid, password);
  206.   IPAddress IP = WiFi.softAPIP();
  207.   Serial.print("AP IP address: ");
  208.   Serial.println(IP);
  209.  
  210.   server.on("/", HTTP_GET, handleRoot);
  211.   server.onNotFound(handleNotFound);
  212.      
  213.   wsCamera.onEvent(onCameraWebSocketEvent);
  214.   server.addHandler(&wsCamera);
  215.  
  216.   server.begin();
  217.   Serial.println("HTTP server started");
  218.  
  219.   setupCamera();
  220. }
  221.  
  222. void loop()
  223. {
  224.   wsCamera.cleanupClients();
  225.   sendCameraPicture();
  226.   //Serial.printf("SPIRam Total heap %d, SPIRam Free Heap %d\n", ESP.getPsramSize(), ESP.getFreePsram());
  227. }
Where is probelm? Any suggestions for a solution?
Thanks for answer

advinio
Posts: 1
Joined: Sat Apr 09, 2022 1:22 am

Re: ESP32-­WROOM-­32E + ArduCam OV2640 Video stream problem

Postby advinio » Sat Apr 09, 2022 2:13 am

Where did you find the Camera Constants for this board?

Who is online

Users browsing this forum: No registered users and 82 guests