esp32-cam: Max image size to be base64 encoded and sended

FromJapan
Posts: 1
Joined: Fri Nov 19, 2021 3:17 pm

esp32-cam: Max image size to be base64 encoded and sended

Postby FromJapan » Fri Nov 19, 2021 3:41 pm

I,
I'm trying to capture images with esp32-cam, then transform the images into a base64 string, which will be sent to google drive.
The maximum size of images I can send to the drive is approximately 45kb. For larger images only a part is sended.
Seems to be a memory issue with esp32 but shouldn't it be possible to send larger images?
The code I'm using is:
  1.  
  2. #include <WiFi.h>
  3. #include <WiFiClientSecure.h>
  4. #include "soc/soc.h"
  5. #include "soc/rtc_cntl_reg.h"
  6. #include "esp_camera.h"
  7. #include "Base64.h"
  8.  
  9. const char* ssid     = "";  
  10. const char* password = "";
  11. const char* host = "script.google.com";
  12. String meuScript = "/macros/s/.../exec";
  13.  
  14. String nomedoArquivo = "filename=ESP32-CAM.jpg";
  15. String mimeType = "&mimetype=image/jpeg";
  16. String Imagem = "&data=";
  17.  
  18. int tempo_espera = 10000;  
  19.  
  20. //Definição dos pinos
  21. #define PWDN_GPIO_NUM     32
  22. #define RESET_GPIO_NUM    -1
  23. #define XCLK_GPIO_NUM      0
  24. #define SIOD_GPIO_NUM     26
  25. #define SIOC_GPIO_NUM     27
  26. #define Y9_GPIO_NUM       35
  27. #define Y8_GPIO_NUM       34
  28. #define Y7_GPIO_NUM       39
  29. #define Y6_GPIO_NUM       36
  30. #define Y5_GPIO_NUM       21
  31. #define Y4_GPIO_NUM       19
  32. #define Y3_GPIO_NUM       18
  33. #define Y2_GPIO_NUM        5
  34. #define VSYNC_GPIO_NUM    25
  35. #define HREF_GPIO_NUM     23
  36. #define PCLK_GPIO_NUM     22
  37. #define flash 4
  38.  
  39. void setup()
  40. {
  41.    
  42.   WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0);
  43.    
  44.   Serial.begin(115200);
  45.   delay(100);
  46.    
  47.   WiFi.mode(WIFI_STA);
  48.  
  49.   Serial.println("");
  50.   Serial.print("Conectando na rede: ");
  51.   Serial.println(ssid);
  52.   WiFi.begin(ssid, password);  
  53.  
  54.   while (WiFi.status() != WL_CONNECTED) {  
  55.     Serial.print(".");
  56.     delay(500);
  57.   }
  58.  
  59.   pinMode(flash,OUTPUT);
  60.  
  61.   Serial.println("Conectado!");
  62.   Serial.println();
  63.   Serial.print("endereço STAIP: ");
  64.   Serial.println(WiFi.localIP());
  65.   Serial.println();
  66.  
  67.   camera_config_t config;
  68.   config.ledc_channel = LEDC_CHANNEL_0;
  69.   config.ledc_timer = LEDC_TIMER_0;
  70.   config.pin_d0 = Y2_GPIO_NUM;
  71.   config.pin_d1 = Y3_GPIO_NUM;
  72.   config.pin_d2 = Y4_GPIO_NUM;
  73.   config.pin_d3 = Y5_GPIO_NUM;
  74.   config.pin_d4 = Y6_GPIO_NUM;
  75.   config.pin_d5 = Y7_GPIO_NUM;
  76.   config.pin_d6 = Y8_GPIO_NUM;
  77.   config.pin_d7 = Y9_GPIO_NUM;
  78.   config.pin_xclk = XCLK_GPIO_NUM;
  79.   config.pin_pclk = PCLK_GPIO_NUM;
  80.   config.pin_vsync = VSYNC_GPIO_NUM;
  81.   config.pin_href = HREF_GPIO_NUM;
  82.   config.pin_sscb_sda = SIOD_GPIO_NUM;
  83.   config.pin_sscb_scl = SIOC_GPIO_NUM;
  84.   config.pin_pwdn = PWDN_GPIO_NUM;
  85.   config.pin_reset = RESET_GPIO_NUM;
  86.   config.xclk_freq_hz = 20000000;
  87.   config.pixel_format = PIXFORMAT_JPEG;
  88.  
  89.   // if PSRAM IC present, init with UXGA resolution and higher JPEG quality
  90.   //                      for larger pre-allocated frame buffer.
  91.   if(psramFound()){
  92.     Serial.print("FOUND");
  93.     config.frame_size = FRAMESIZE_UXGA; // FRAMESIZE_ + QVGA|CIF|VGA|SVGA|XGA|SXGA|UXGA
  94.     config.jpeg_quality = 50;
  95.     config.fb_count = 2;
  96.   } else {
  97.     Serial.print("not found");
  98.     config.frame_size = FRAMESIZE_SVGA;
  99.     config.jpeg_quality = 12;
  100.     config.fb_count = 1;
  101.    }
  102.    
  103.   esp_err_t err = esp_camera_init(&config); //Inicialização da câmera
  104.    
  105.   if (err != ESP_OK) {
  106.      
  107.     Serial.printf("O início da câmera falhou com erro 0x%x", err);
  108.     delay(1000);
  109.     ESP.restart();//Reinicia o ESP
  110.      
  111.   }
  112.    
  113. }
  114.  
  115. void loop() {
  116.    
  117.   Captura_Imagem();
  118.   delay(4000);
  119.    
  120. }
  121.  
  122. void Captura_Imagem() {
  123.    
  124.   Serial.println("Conectando ao " + String(host));
  125.  
  126.   WiFiClientSecure client;
  127.   client.setInsecure();
  128.    
  129.   if (client.connect(host, 443)) {
  130.      
  131.     Serial.println("Conexão com sucesso!");
  132.      
  133.     camera_fb_t * fb = NULL;
  134.      
  135.     digitalWrite(flash,HIGH);
  136.     delay(100);
  137.      
  138.     fb = esp_camera_fb_get();
  139.      
  140.     digitalWrite(flash,LOW);
  141.     delay(100);
  142.      
  143.     if(!fb) {
  144.        
  145.       Serial.println("Falha ao capturar imagem!");
  146.       delay(1000);
  147.       ESP.restart();
  148.       return;
  149.      
  150.     }
  151.  
  152.    
  153.     char *input = (char *)fb->buf;
  154.     char output[base64_enc_len(3)];
  155.     String imageFile = "";
  156.  
  157.     for (int i=0;i<fb->len;i++) {
  158.       base64_encode(output, (input++), 3);
  159.       if (i%3==0) imageFile += urlencode(String(output));
  160.     }
  161.    
  162.     String Data = nomedoArquivo+mimeType+Imagem;
  163.      
  164.     esp_camera_fb_return(fb);
  165.      
  166.     Serial.println("Enviando imagem capturada ao Google Drive.");
  167.    
  168.     client.println("POST " + meuScript + " HTTP/1.1");
  169.     client.println("Host: " + String(host));
  170.     client.println("Content-Length: " + String(Data.length()+imageFile.length()));
  171.     client.println("Content-Type: application/x-www-form-urlencoded");
  172.     client.println();
  173.      
  174.     client.print(Data);
  175.  
  176.     int Index;
  177.     Serial.print("START:\n");
  178.     for (Index = 0; Index < imageFile.length(); Index = Index+1000) {
  179.       client.print(imageFile.substring(Index, Index+1000));
  180.  
  181.     }
  182.    
  183.    
  184.     Serial.print("END:\n");
  185.      
  186.     Serial.println("Aguardando resposta.");
  187.      
  188.     long int tempo_inicio = millis();
  189.      
  190.     while (!client.available()) {
  191.       Serial.print(".");
  192.       delay(100);
  193.       if ((tempo_inicio+tempo_espera) < millis()) {
  194.         Serial.println();
  195.         Serial.println("Sem Resposta.");
  196.         break;
  197.       }
  198.     }
  199.      
  200.     Serial.println();  
  201.      
  202.     while (client.available()) {
  203.       Serial.print(char(client.read()));
  204.     }  
  205.   } else {        
  206.     Serial.println("Conexão ao " + String(host) + " falhada.");
  207.   }
  208.   client.stop();
  209. }
  210.  
  211.  
  212. String urlencode(String str)
  213. {
  214.     String encodedString="";
  215.     char c;
  216.     char code0;
  217.     char code1;
  218.     char code2;
  219.      
  220.     for (int i =0; i < str.length(); i++){
  221.       c=str.charAt(i);
  222.       if (c == ' '){
  223.         encodedString+= '+';
  224.       } else if (isalnum(c)){
  225.         encodedString+=c;
  226.       } else{
  227.         code1=(c & 0xf)+'0';
  228.         if ((c & 0xf) >9){
  229.             code1=(c & 0xf) - 10 + 'A';
  230.         }
  231.         c=(c>>4)&0xf;
  232.         code0=c+'0';
  233.         if (c > 9){
  234.             code0=c - 10 + 'A';
  235.         }
  236.         code2='\0';
  237.         encodedString+='%';
  238.         encodedString+=code0;
  239.         encodedString+=code1;
  240.       }
  241.       yield();
  242.     }
  243.     return encodedString;
  244. }
  245.  
  246.  

Bomexx
Posts: 1
Joined: Tue Feb 01, 2022 6:28 pm

Re: esp32-cam: Max image size to be base64 encoded and sended

Postby Bomexx » Tue Feb 01, 2022 6:32 pm

Have you found a solution yet?

aparnareddy
Posts: 2
Joined: Thu Dec 01, 2022 7:27 am

Re: esp32-cam: Max image size to be base64 encoded and sended

Postby aparnareddy » Thu Dec 01, 2022 7:32 am

Hi does this issue got resolved
if yes , can you please share the code.

thank you
aparna

Who is online

Users browsing this forum: and3rson and 126 guests