ESP32-CAM 发送图片到服务器:使用 multipart/form-data 上传
以下是一个示例代码,用于将 ESP32-CAM 拍摄的图像作为 multipart/form-data 发送到服务器:
#include <WiFi.h>
#include <HTTPClient.h>
#include <WiFiClient.h>
#include "esp_camera.h"
// 替换为您的网络凭据
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";
// 服务器详细信息
const char* serverName = "http://your_server_address/upload.php";
const char* serverPort = "80";
// 摄像头引脚
#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);
// 连接到 Wi-Fi 网络,使用 SSID 和密码
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_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;
// 初始化摄像头
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() {
// 拍摄照片
camera_fb_t* fb = esp_camera_fb_get();
if (!fb) {
Serial.println("Failed to capture photo");
return;
}
// 创建 HTTP 客户端
WiFiClient client;
HTTPClient http;
http.begin(client, serverName, serverPort);
// 设置 content-type 头部
http.addHeader("Content-Type", "multipart/form-data");
// 创建 multipart/form-data 负载
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");
// 发送图片数据
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;
}
// 关闭 multipart/form-data 负载
http.write("\r\n--");
http.print("123456789000");
http.write("--\r\n");
http.endRequest();
// 发送 HTTP 请求并获取响应
int httpResponseCode = http.POST();
String response = http.getString();
// 打印响应
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
Serial.print("HTTP Response body: ");
Serial.println(response);
// 释放摄像头缓冲区
esp_camera_fb_return(fb);
// 等待 5 秒,然后拍摄下一张照片
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/jN9g 著作权归作者所有。请勿转载和采集!