esp32 通过post 发送图片
ESP32可以通过HTTP POST方法发送图片。以下是一个示例代码,可以将ESP32上的图片发送到远程服务器:
#include <WiFi.h>
#include <HTTPClient.h>
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";
const char* serverName = "http://your_server_name.com/upload_image.php"; // 服务器接收图片的URL
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
// 读取图片数据
File file = SPIFFS.open("/image.jpg", "r");
if (!file) {
Serial.println("Failed to open file for reading");
return;
}
uint8_t* imageBuffer = (uint8_t*)malloc(file.size());
file.read(imageBuffer, file.size());
file.close();
// 创建HTTPClient对象
HTTPClient http;
// 设置POST请求头
http.begin(serverName);
http.addHeader("Content-Type", "image/jpeg");
// 发送POST请求
int httpResponseCode = http.POST(imageBuffer, file.size());
// 处理响应
if (httpResponseCode > 0) {
String response = http.getString();
Serial.println(httpResponseCode);
Serial.println(response);
} else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
// 释放内存
free(imageBuffer);
http.end();
}
void loop() {
// do nothing
}
在此示例中,我们使用SPIFFS文件系统从ESP32上读取名为“image.jpg”的图片。然后,我们使用HTTPClient库创建一个HTTP客户端对象,并设置POST请求头为“image/jpeg”。最后,我们使用http.POST()方法发送图片数据,并处理响应。请注意,此示例仅适用于JPEG格式的图片。如果您要发送其他格式的图片,请相应地更改Content-Type头
原文地址: https://www.cveoy.top/t/topic/cjwB 著作权归作者所有。请勿转载和采集!