Compartir tecnología

ESP32 controla la cámara: 1. Controla el módulo OV2640 (por verificar)

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

1. Instale la biblioteca ArduCam y la biblioteca ESPAsyncWebServer

2. Código de referencia

  1. #include <Wire.h>
  2. #include <ArduCAM.h>
  3. #include <SPI.h>
  4. #include <WiFi.h>
  5. #include <ESPAsyncWebServer.h>
  6. #define CAM_CS 32 // modify according to your own wiring
  7. #define OV2640_CHIPID_HIGH 0x0A
  8. #define OV2640_CHIPID_LOW 0x0B
  9. ArduCAM myCAM(OV2640, CAM_CS);
  10. // Set your access point's SSID and password
  11. const char* ssid = "..."; //wifi名
  12. const char* password = "....."; // 密码
  13. AsyncWebServer server(80); // Create an instance of the server
  14. // This function sends the live video frames to the client
  15. void serverStream(AsyncWebServerRequest *request){
  16. AsyncWebServerResponse *response = request->beginResponse("multipart/x-mixed-replace;boundary=frame", HTTP_ANY, [](uint8_t *buffer, size_t maxLen, size_t alreadySent) -> size_t {
  17. if(myCAM.get_bit(ARDUCHIP_TRIG , CAP_DONE_MASK)){
  18. size_t len = myCAM.read_fifo_length();
  19. uint8_t * buf = (uint8_t *)malloc(len);
  20. myCAM.CS_LOW();
  21. myCAM.set_fifo_burst();
  22. myCAM.spi_trans(0xFF);
  23. myCAM.transferBytes(myCAM.camera_model,BUF,buf,len);
  24. myCAM.CS_HIGH();
  25. size_t available_len = len - (alreadySent ? 32 : 0);
  26. size_t will_send =(available_len > maxLen) ? maxLen : available_len;
  27. if (!alreadySent){ // First chunk
  28. memcpy_P(buffer, buf, will_send);
  29. } else { // Consequent chunks
  30. buffer = buf + 32;
  31. }
  32. free(buf);
  33. if (!alreadySent) {
  34. return will_send;
  35. } else if (alreadySent + will_send < len) {
  36. return will_send;
  37. } else {
  38. myCAM.clear_fifo_flag();
  39. return 0; // Last chunk sent, signaling end of response by returning 0 bytes left
  40. }
  41. }
  42. return 0; // If there's nothing in the buffer, return 0 bytes left
  43. });
  44. response->addHeader("Connection", "close");
  45. request->send(response);
  46. }
  47. void setup() {
  48. Wire.begin(21, 22); // SDA, SCL
  49. WiFi.begin(ssid, password);
  50. while (WiFi.status() != WL_CONNECTED) {
  51. delay(1000);
  52. }
  53. myCAM.set_format(JPEG);
  54. myCAM.InitCAM();
  55. myCAM.write_reg(ARDUCHIP_TIM, VSYNC_LEVEL_MASK); // VSYNC is active HIGH
  56. myCAM.clear_fifo_flag();
  57. myCAM.write_reg(ARDUCHIP_FRAMES, 0x00);
  58. server.on("/stream", HTTP_GET, serverStream); // Route for live video stream
  59. server.begin();
  60. }
  61. void loop() {
  62. // You can put other code here
  63. }

3. Cableado de hardware

OV2640 VCC (fuente de alimentación) --&gt; ESP32 3.3V

OV2640 GND (tierra) --&gt; ESP32 GND

OV2640 SDA (línea de datos) --&gt; ESP32 IO21 (línea de datos I2C SDA)

OV2640 SCL (línea de reloj) --&gt; ESP32 IO22 (línea de reloj I2C SCL)

OV2640 VSNC (sincronización de cuadros) --&gt; ESP32 IO25

OV2640 HREF (sincronización de filas) --&gt; ESP32 IO23

OV2640 PCLK (reloj de píxeles) --&gt; ESP32 IO19

OV2640 XCLK (reloj del sistema) --&gt; ESP32 IO18

OV2640 D0~D7 (línea de datos) --&gt; ESP32 IO5~IO13 (línea de datos paralela de 8 bits)

4. Reflexión

En este momento, algunos amigos atentos pueden haber descubierto que una placa de desarrollo ESP-WROOM-32 cuesta 25 yuanes, un módulo OV2640 cuesta 23 yuanes, un total de 48 yuanes y una placa de desarrollo ESP-WROOM-32 con su propia cámara; 23 yuanes.ESP32CAM La placa de desarrollo sólo cuesta 41 yuanes. En otras palabras, la solución del "módulo ESP-WROOM-32+" no sólo es difícil de ajustar sino que también cuesta 7 yuanes, así que ¿por qué no comprar simplemente una placa de desarrollo ESP32CAM? No me preguntes porque yo también me acabo de enterar...