简体   繁体   中英

Can someone give me the code for esp32 cam in order to upload the captured image to a local server using http post method?

I want to capture some moment using esp32 cam (by AI Thinker) and this captured photo should upload to a local server using HTTP post method. I am completely new to HTTP request. I want the arduino code. Here is my code, it never connects to server. I tried by removing the client.connect function but it still not works.

    #include<WiFi.h>
    #include<WiFiClientSecure.h>
    #include "esp_camera.h"
    #include "esp_timer.h"
    #include "img_converters.h"
    #include "Arduino.h"
    #include "fb_gfx.h"
    #include "fd_forward.h"
    #include "fr_forward.h"
    #include "FS.h"               
    #include "SD_MMC.h"           
    #include "soc/soc.h"           
    #include "soc/rtc_cntl_reg.h"  
    #include "driver/rtc_io.h"

    #define PWDN_GPIO_NUM     32
    #define RESET_GPIO_NUM    -1
    #define XCLK_GPIO_NUM      0
    #define SIOD_GPIO_NUM     26
    #define SIOC_GPIO_NUM     27
    #define Y9_GPIO_NUM       35
    #define Y8_GPIO_NUM       34
    #define Y7_GPIO_NUM       39
    #define Y6_GPIO_NUM       36
    #define Y5_GPIO_NUM       21
    #define Y4_GPIO_NUM       19
    #define Y3_GPIO_NUM       18
    #define Y2_GPIO_NUM        5
    #define VSYNC_GPIO_NUM    25
    #define HREF_GPIO_NUM     23
    #define PCLK_GPIO_NUM     22

    const char* ssid = "unknown";
    const char* password = "unknown";

    const char* host = "192.168.1.249";
    const int Port = 5000;
    const char* boundry = "dgbfhfh";

    WiFiClientSecure client;

    void setup() 
    {
         WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0);

         Serial.begin(115200);

         pinMode(4,OUTPUT);

         Serial.printf("Connecting to the Wifi [%s]...\r\n", ssid);
         WiFi.begin(ssid,password);
         while (WiFi.status() != WL_CONNECTED) 
         {
                delay(500);
                Serial.print(".");
         }
         Serial.println("WiFi connected");

         camera_config_t config;
         config.ledc_channel = LEDC_CHANNEL_0;
         config.ledc_timer = LEDC_TIMER_0;
         config.pin_d0 = Y2_GPIO_NUM;
         config.pin_d1 = Y3_GPIO_NUM;
         config.pin_d2 = Y4_GPIO_NUM;
         config.pin_d3 = Y5_GPIO_NUM;
         config.pin_d4 = Y6_GPIO_NUM;
         config.pin_d5 = Y7_GPIO_NUM;
         config.pin_d6 = Y8_GPIO_NUM;
         config.pin_d7 = Y9_GPIO_NUM;
         config.pin_xclk = XCLK_GPIO_NUM;
         config.pin_pclk = PCLK_GPIO_NUM;
         config.pin_vsync = VSYNC_GPIO_NUM;
         config.pin_href = HREF_GPIO_NUM;
         config.pin_sscb_sda = SIOD_GPIO_NUM;
         config.pin_sscb_scl = SIOC_GPIO_NUM;
         config.pin_pwdn = PWDN_GPIO_NUM;
         config.pin_reset = RESET_GPIO_NUM;
         config.xclk_freq_hz = 20000000;
         config.pixel_format = PIXFORMAT_JPEG; 

        if(psramFound())
        {
           config.frame_size = FRAMESIZE_QVGA; // FRAMESIZE_                                 +QVGA|CIF|VGA|SVGA|XGA|SXGA|UXGA
          config.jpeg_quality = 10;
          config.fb_count = 2;
        } 
        else 
        {
           config.frame_size = FRAMESIZE_QVGA;
           config.jpeg_quality = 12;
           config.fb_count = 1;
        }

      // Initialise Camera
      esp_err_t err = esp_camera_init(&config);
      if (err != ESP_OK) 
      {
           Serial.printf("Camera init failed with error 0x%x", err);
           return;
      }

      sendPhotoToServer(); 
    }

    void sendPhotoToServer()
    {
         Serial.printf("Connecting to %s:%d... ", host,Port);

         if (!client.connect(host,Port)) 
         {      
           Serial.println("Failure in connection with the server");      
           return;
         }

         sendDataToServer();
     }

     void sendDataToServer()
     {
        String start_request = "";
        String end_request = "";

        start_request = start_request + "--" + boundry + "\r\n";
        start_request = start_request + "Content-Disposition: form- data; name=\"image\"\r\n";
        start_request = start_request + "Content-Type: image/jpeg\r\n";
        start_request = start_request + "\r\n";

        end_request = end_request + "\r\n";
        end_request = end_request + "--" + boundry + "--" + "\r\n";

        /************************************************/  
        digitalWrite(4,HIGH);
        camera_fb_t * fb = NULL;
        // Take Picture with Camera
        fb = esp_camera_fb_get();  
        if(!fb) 
         {
           Serial.println("Camera capture failed");
           return;
         }
         digitalWrite(4,LOW);

         delay(1000);
        /************************************************/     

         int contentLength = (int)fb->len + start_request.length() + end_request.length();    

         String headers = String("POST /image-upload HTTP/1.1\r\n");
         headers = headers + "Host: " + host + "\r\n";
         headers = headers + "User-Agent: ESP8266" + "\r\n";
         headers = headers + "Accept: */*\r\n";
         headers = headers + "Content-Type: multipart/form-data; boundary=" + boundry + "\r\n";
         headers = headers + "Content-Length: " + contentLength + "\r\n";
         headers = headers + "\r\n";
         headers = headers + "\r\n";

         Serial.println();
         Serial.println("Sending data to Server...");        

         Serial.print(headers);        
         client.print(headers);
         client.flush();

         Serial.print(start_request);
         client.print(start_request);
         client.flush();

       /*****************************************************/
       int iteration = fb->len / 1024;
       for(int i=0; i<iteration; i++)
       {
         client.write(fb->buf,1024);
         fb->buf += 1024;
         client.flush();
       }
       size_t remain = fb->len % 1024;
       client.write(fb->buf,remain);
       client.flush();
       /****************************************************/  

       Serial.print(end_request);
       client.print(end_request);
       client.flush();

       esp_camera_fb_return(fb);
      }

     void loop() 
      {

      }

Hello there are quite a lot of examples about how to do it online. Check out this project:

https://github.com/martinberlin/FS32/blob/master/src/FasarekFS2.ino#L433

Basically just like you do in the web, you need to create the right headers in the ESP32:

String boundary = "_cam_";
String end_request = "\n--"+boundary+"--\n";
String start_request = start_request + 
"\n--"+boundary+"\n" + 
"Content-Disposition: form-data; name=\"upload\"; filename=\"CAM.JPG\"\n" + 
"Content-Transfer-Encoding: binary\n\n";
WiFiClient client;
client.println("POST "+String(upload_path)+" HTTP/1.1");
client.println("Host: "+String(upload_host));
client.println("Content-Type: multipart/form-data; boundary="+boundary);
client.print("Content-Length: "); client.println(full_length);
client.println();
client.print(start_request);

Then you should read the image in a Buffer and send it to the client. Ending with a:

client.println(end_request);

I know is quite a raw example. But it's the way I got it to work in different projects.

Here is your modified code. It is working !!! :D

    #include<WiFiClientSecure.h>
    #include "esp_camera.h"
    #include "esp_timer.h"
    #include "img_converters.h"
    #include "Arduino.h"
    #include "fb_gfx.h"
    #include "fd_forward.h"
    #include "fr_forward.h"
    #include "FS.h"               
    #include "SD_MMC.h"           
    #include "soc/soc.h"           
    #include "soc/rtc_cntl_reg.h"  
    #include "driver/rtc_io.h"

    #define PWDN_GPIO_NUM     32
    #define RESET_GPIO_NUM    -1
    #define XCLK_GPIO_NUM      0
    #define SIOD_GPIO_NUM     26
    #define SIOC_GPIO_NUM     27
    #define Y9_GPIO_NUM       35
    #define Y8_GPIO_NUM       34
    #define Y7_GPIO_NUM       39
    #define Y6_GPIO_NUM       36
    #define Y5_GPIO_NUM       21
    #define Y4_GPIO_NUM       19
    #define Y3_GPIO_NUM       18
    #define Y2_GPIO_NUM        5
    #define VSYNC_GPIO_NUM    25
    #define HREF_GPIO_NUM     23
    #define PCLK_GPIO_NUM     22

    const char* ssid = "***";
    const char* password = "****";

    const char* host = "192.168.3.19";
    const int Port = 4567;
    const char* boundry = "dgbfhfh";

    WiFiClientSecure client;

    void setup() 
    {
         WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0);

         Serial.begin(115200);

         pinMode(4,OUTPUT);

         Serial.printf("Connecting to the Wifi [%s]...\r\n", ssid);
         WiFi.begin(ssid,password);
         while (WiFi.status() != WL_CONNECTED) 
         {
                delay(500);
                Serial.print(".");
         }
         Serial.println("WiFi connected");

         camera_config_t config;
         config.ledc_channel = LEDC_CHANNEL_0;
         config.ledc_timer = LEDC_TIMER_0;
         config.pin_d0 = Y2_GPIO_NUM;
         config.pin_d1 = Y3_GPIO_NUM;
         config.pin_d2 = Y4_GPIO_NUM;
         config.pin_d3 = Y5_GPIO_NUM;
         config.pin_d4 = Y6_GPIO_NUM;
         config.pin_d5 = Y7_GPIO_NUM;
         config.pin_d6 = Y8_GPIO_NUM;
         config.pin_d7 = Y9_GPIO_NUM;
         config.pin_xclk = XCLK_GPIO_NUM;
         config.pin_pclk = PCLK_GPIO_NUM;
         config.pin_vsync = VSYNC_GPIO_NUM;
         config.pin_href = HREF_GPIO_NUM;
         config.pin_sscb_sda = SIOD_GPIO_NUM;
         config.pin_sscb_scl = SIOC_GPIO_NUM;
         config.pin_pwdn = PWDN_GPIO_NUM;
         config.pin_reset = RESET_GPIO_NUM;
         config.xclk_freq_hz = 20000000;
         config.pixel_format = PIXFORMAT_JPEG; 

        if(psramFound())
        {
           config.frame_size = FRAMESIZE_SXGA; // FRAMESIZE_                                 +QVGA|CIF|VGA|SVGA|XGA|SXGA|UXGA
          config.jpeg_quality = 10;
          config.fb_count = 2;
        } 
        else 
        {
           config.frame_size = FRAMESIZE_VGA;
           config.jpeg_quality = 12;
           config.fb_count = 1;
        }

      // Initialise Camera
      esp_err_t err = esp_camera_init(&config);
      if (err != ESP_OK) 
      {
           Serial.printf("Camera init failed with error 0x%x", err);
           return;
      }

      sendPhotoToServer(); 
    }

    void sendPhotoToServer()
    {
         Serial.printf("Connecting to %s:%d... ", host,Port);

         if (!client.connect(host,Port)) 
         {      
           Serial.println("Failure in connection with the server");      
           return;
         }

         sendDataToServer();
     }

     void sendDataToServer()
     {
        String start_request = "";
        String end_request = "";

        start_request = start_request + "--" + boundry + "\r\n";

        start_request = start_request + "Content-Disposition: form-data; name=\"file\"; filename=\"CAM.jpg\"\r\n";
        start_request = start_request + "Content-Type: image/jpg\r\n";
        start_request = start_request + "\r\n";

        end_request = end_request + "\r\n";


        end_request = end_request + "--" + boundry + "--" + "\r\n";

        /************************************************/  
        digitalWrite(4,HIGH);
        camera_fb_t * fb = NULL;
        // Take Picture with Camera
        fb = esp_camera_fb_get();  
        if(!fb) 
         {
           Serial.println("Camera capture failed");
           return;
         }
         digitalWrite(4,LOW);

         delay(1000);
        /************************************************/     

         int contentLength = (int)fb->len + start_request.length() + end_request.length();    

         String headers = String("POST https://192.168.3.19:4567/api/upload HTTP/1.1\r\n");
         headers = headers + "Host: " + host + "\r\n";
         headers = headers + "User-Agent: ESP32" + "\r\n";
         headers = headers + "Accept: */*\r\n";
         headers = headers + "Content-Type: multipart/form-data; boundary=" + boundry + "\r\n";
         headers = headers + "Content-Length: " + contentLength + "\r\n";
         headers = headers + "\r\n";
         headers = headers + "\r\n";

         Serial.println();
         Serial.println("Sending data to Server...");        

         Serial.print(headers);        
         client.print(headers);
         client.flush();

         Serial.print(start_request);
         client.print(start_request);
         client.flush();

       /*****************************************************/
       int iteration = fb->len / 1024;
       for(int i=0; i<iteration; i++)
       {
         client.write(fb->buf,1024);
         fb->buf += 1024;
         client.flush();
       }
       size_t remain = fb->len % 1024;
       client.write(fb->buf,remain);
       client.flush();
       /****************************************************/  

       Serial.print(end_request);
       client.print(end_request);
       client.flush();

       esp_camera_fb_return(fb);
      }

     void loop() 
      {

      }

And backend method from Spark Java. Dont forget to make your spark server HTTPS/SSL !!! http://sparkjava.com/documentation#examples-and-faq



public class Main {

    public static void main(String[] args) {

       secure("deploy/keystore.jks", "password", null, null);

        File uploadDir = new File("upload");
        uploadDir.mkdir(); // create the upload directory if it doesn't exist

        post("/api/upload", (req, res) -> {
            req.attribute("org.eclipse.jetty.multipartConfig", new MultipartConfigElement(uploadDir.getPath()));
            Part filePart = req.raw().getPart("file");
            try (InputStream inputStream = filePart.getInputStream()) {
                OutputStream outputStream = new FileOutputStream(uploadDir + "/" + filePart.getSubmittedFileName());
                IOUtils.copy(inputStream, outputStream);
                outputStream.close();
            }
            return "File uploaded and saved.";
        });
    }
}


The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM