Esp32-cam 通过Post multipartform-data 发送图片图片参数名 file
以下是一个示例代码,用于将ESP32-CAM拍摄的图像作为multipart/form-data发送到服务器:
#include <WiFi.h>
#include <HTTPClient.h>
#include <WiFiClient.h>
#include "esp_camera.h"
// Replace with your network credentials
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";
// Server details
const char* serverName = "http://your_server_address/upload.php";
const char* serverPort = "80";
// Camera pins
#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
void setup() {
Serial.begin(115200);
delay(10);
// Connect to Wi-Fi network with SSID and password
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
// Camera configuration
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;
config.frame_size = FRAMESIZE_QVGA;
config.jpeg_quality = 12;
config.fb_count = 1;
// Initialize the camera
esp_err_t err = esp_camera_init(&config);
if (err != ESP_OK) {
Serial.printf("Camera init failed with error 0x%x", err);
return;
}
}
void loop() {
// Capture a photo
camera_fb_t* fb = esp_camera_fb_get();
if (!fb) {
Serial.println("Failed to capture photo");
return;
}
// Create a HTTP client
WiFiClient client;
HTTPClient http;
http.begin(client, serverName, serverPort);
// Set the content-type header
http.addHeader("Content-Type", "multipart/form-data");
// Create the multipart/form-data payload
WiFiClient stream;
http.collectHeaders(stream);
http.beginRequest();
http.write("--");
http.print("123456789000");
http.write("\r\n");
http.print("Content-Disposition: form-data; name=\"file\"; filename=\"image.jpg\"\r\n");
http.print("Content-Type: image/jpeg\r\n");
http.write("\r\n");
// Send the image data
uint8_t* buf = fb->buf;
size_t len = fb->len;
size_t index = 0;
while (index < len) {
size_t chunkSize = len - index;
if (chunkSize > 1024) {
chunkSize = 1024;
}
http.write(buf + index, chunkSize);
index += chunkSize;
}
// Close the multipart/form-data payload
http.write("\r\n--");
http.print("123456789000");
http.write("--\r\n");
http.endRequest();
// Send the HTTP request and get the response
int httpResponseCode = http.POST();
String response = http.getString();
// Print the response
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
Serial.print("HTTP Response body: ");
Serial.println(response);
// Release the camera buffer
esp_camera_fb_return(fb);
// Wait for 5 seconds before capturing the next photo
delay(5000);
}
该代码连接到Wi-Fi网络,初始化ESP32-CAM摄像头,并在循环中捕获一张照片,将其作为multipart/form-data发送到服务器。要使用此代码,请将以下参数替换为您自己的值:
ssid和password:您的Wi-Fi网络凭据。serverName:您的服务器地址。serverPort:您的服务器端口号。
请注意,此代码使用了HTTPClient库,因此您需要在Arduino IDE中安装该库。您可以通过依次选择“工具”>“管理库”>搜索“HTTPClient”来安装它
原文地址: https://www.cveoy.top/t/topic/cjrN 著作权归作者所有。请勿转载和采集!