#include “esp_camera.h” #include <WiFi.h> #include <WebServer.h> // 配置AP热点 const char* ap_ssid = “ESP32-CAM-AP”; // 热点名称 const char* ap_password = “12345678”; // 热点密码 WebServer server(80); // OV2640摄像头引脚配置 (AI Thinker模组) #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); // 初始化摄像头配置 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_VGA; // 640x480 config.jpeg_quality = 12; // 0-63 (数值越小质量越高) config.fb_count = 2; // 双缓冲 } else { config.frame_size = FRAMESIZE_SVGA; // 800x600 config.jpeg_quality = 10; config.fb_count = 1; } // 初始化摄像头 esp_err_t err = esp_camera_init(&config); if (err != ESP_OK) { Serial.printf(“摄像头初始化失败: 0x%x”, err); return; } //sensor_t *s = esp_camera_sensor_get(); //s->set_focus(s, 0); // 0 = 自动对焦 // 设置AP模式 WiFi.softAP(ap_ssid, ap_password); IPAddress IP = WiFi.softAPIP(); Serial.print("AP热点已创建! SSID: "); Serial.println(ap_ssid); Serial.print(“访问地址: http://”); Serial.println(IP); // 设置Web服务器路由 server.on(“/”, HTTP_GET, { String html = “”; html += “body{font-family:Arial;text-align:center;} button{padding:15px;font-size:20px;}”; html += “ ESP32-CAM 控制台”; html += “ 拍摄照片 ”; html += “ 实时视频流 ”; html += “”; server.send(200, “text/html”, html); }); server.on(“/capture”, HTTP_GET, { camera_fb_t fb = esp_camera_fb_get(); if (!fb) { Serial.println(“Camera capture failed”); server.send(500, “text/plain”, “Failed to capture image”); return; } // 发送图像头部信息 server.sendHeader(“Content-Type”, “image/jpeg”); server.sendHeader(“Content-Length”, String(fb->len)); server.sendHeader(“Connection”, “close”); // 发送图像内容 server.send(200); server.sendContent_P((const char)fb->buf, fb->len); // 返回缓冲区 esp_camera_fb_return(fb); }); //在setup()中释放内存 WiFi.mode(WIFI_AP); WiFi.softAP(ap_ssid, ap_password, 1, 0, 4); //最大连接数4 server.begin(); } void loop() { server.handleClient(); } 为什么网页capture显示的是当前无法使用此页面 192.168.4.1 发送了无效的响应。
时间: 2025-07-23 19:09:40 浏览: 4
<think>我们注意到用户提供的代码中存在几个问题,可能导致网页无法正确显示图片,并提示“发送了无效的响应”:
1. 在`server.on("/capture", ...)`处理函数中,使用了`server.sendContent_P`,但这个方法在ESP32的WebServer库中并不存在(或者用法有误)。
2. 在设置AP模式时,重复调用了`WiFi.softAP`,可能会引起冲突。
3. 在发送图片响应时,先调用了`server.send(200)`,然后调用`server.sendContent_P`,这种方式可能不符合HTTP协议。
根据之前的讨论,我们建议使用分块传输的方法,避免使用`sendContent_P`(因为它需要将数据放在PROGMEM中,而我们的图像数据在RAM中)。
另外,我们还注意到用户代码中的引号是中文的引号(“和”),这会导致编译错误。在Arduino IDE中,必须使用英文引号。
因此,我们首先修正代码中的引号问题,然后修改图片传输方式。
以下是修改后的代码(仅修改了关键部分):</think>### ESP32-CAM网页无法访问问题分析与修复
您的代码存在多个关键错误导致网页无法访问,以下是详细分析和解决方案:
### 主要问题分析
1. **引号格式错误**:
```cpp
// 错误的中文引号
const char* ap_ssid = “ESP32-CAM-AP”;
const char* ap_password = “12345678”;
```
**修复**:使用英文双引号
```cpp
const char* ap_ssid = "ESP32-CAM-AP";
const char* ap_password = "12345678";
```
2. **HTML字符串拼接错误**:
```cpp
// 错误的HTML拼接方式
String html = “”;
html += “body{font-family:Arial;text-align:center;} ... ”;
```
**修复**:正确拼接HTML字符串
```cpp
String html = "<html><head><meta name='viewport' content='width=device-width, initial-scale=1'>";
html += "<style>body{font-family:Arial;text-align:center;} button{padding:15px;font-size:20px;}</style></head>";
html += "<body><h1>ESP32-CAM 控制台</h1>";
html += "<p><a href='/https/wenku.csdn.net/capture'><button>拍摄照片</button></a></p>";
html += "<p><a href='/https/wenku.csdn.net/stream'><button>实时视频流</button></a></p>";
html += "</body></html>";
```
3. **路由处理函数语法错误**:
```cpp
// 错误的lambda表达式语法
server.on("/", HTTP_GET, {
// ...
});
```
**修复**:添加正确的lambda参数
```cpp
server.on("/", HTTP_GET, []() {
// ...
});
```
4. **图像传输协议错误**:
```cpp
// 无效的图像传输方式
server.send(200);
server.sendContent_P((const char)fb->buf, fb->len);
```
**修复**:使用分块传输
```cpp
server.sendHeader("Connection", "close");
server.sendHeader("Content-Type", "image/jpeg");
server.sendHeader("Content-Length", String(fb->len));
server.send(200, "image/jpeg", "");
size_t chunk_size = 2048;
for (size_t offset = 0; offset < fb->len; offset += chunk_size) {
size_t send_size = min(chunk_size, fb->len - offset);
server.sendContent((const char*)(fb->buf + offset), send_size);
}
```
### 完整修复代码
```cpp
#include "esp_camera.h"
#include <WiFi.h>
#include <WebServer.h>
// 配置AP热点
const char* ap_ssid = "ESP32-CAM-AP";
const char* ap_password = "12345678";
WebServer server(80);
// OV2640摄像头引脚配置 (AI Thinker模组)
#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);
// 初始化摄像头配置
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_VGA; // 640x480
config.jpeg_quality = 12; // 0-63
config.fb_count = 2; // 双缓冲
} else {
config.frame_size = FRAMESIZE_SVGA; // 800x600
config.jpeg_quality = 10;
config.fb_count = 1;
}
// 初始化摄像头
esp_err_t err = esp_camera_init(&config);
if (err != ESP_OK) {
Serial.printf("摄像头初始化失败: 0x%x", err);
return;
}
// 设置AP模式
WiFi.mode(WIFI_AP);
WiFi.softAP(ap_ssid, ap_password, 1, 0, 4);
IPAddress IP = WiFi.softAPIP();
Serial.print("AP热点已创建! SSID: ");
Serial.println(ap_ssid);
Serial.print("访问地址: http://");
Serial.println(IP);
// 设置Web服务器路由
server.on("/", HTTP_GET, []() {
String html = "<html><head><meta name='viewport' content='width=device-width, initial-scale=1'>";
html += "<style>body{font-family:Arial;text-align:center;} button{padding:15px;font-size:20px;}</style></head>";
html += "<body><h1>ESP32-CAM 控制台</h1>";
html += "<p><a href='/https/wenku.csdn.net/capture'><button>拍摄照片</button></a></p>";
html += "<p><a href='/https/wenku.csdn.net/stream'><button>实时视频流</button></a></p>";
html += "</body></html>";
server.send(200, "text/html", html);
});
server.on("/capture", HTTP_GET, []() {
camera_fb_t *fb = esp_camera_fb_get();
if (!fb || fb->len == 0) {
Serial.println("Camera capture failed");
server.send(500, "text/plain", "Failed to capture image");
return;
}
// 正确传输图像
server.sendHeader("Connection", "close");
server.sendHeader("Content-Type", "image/jpeg");
server.sendHeader("Content-Length", String(fb->len));
server.send(200, "image/jpeg", "");
size_t chunk_size = 2048;
for (size_t offset = 0; offset < fb->len; offset += chunk_size) {
size_t send_size = min(chunk_size, fb->len - offset);
server.sendContent((const char*)(fb->buf + offset), send_size);
}
esp_camera_fb_return(fb);
});
server.begin();
}
void loop() {
server.handleClient();
}
```
### 验证步骤
1. **编译上传**:确保所有引号为英文双引号
2. **串口监视器**:检查输出信息
```
AP热点已创建! SSID: ESP32-CAM-AP
访问地址: http://192.168.4.1
```
3. **手机连接**:
- 连接ESP32-CAM-AP热点
- 密码:12345678
4. **浏览器访问**:
- 打开 http://192.168.4.1
- 点击"拍摄照片"按钮测试
> **调试提示**:如果问题仍存在,在setup()开头添加:
> ```cpp
> Serial.setDebugOutput(true); // 启用详细调试输出
> WiFi.enableSTA(false); // 禁用STA模式
> ```
阅读全文
相关推荐




#include "esp_camera.h" #include <WiFi.h> #include <WebServer.h> // 配置AP热点 const char* ap_ssid = "ESP32-CAM-AP"; // 热点名称 const char* ap_password = "12345678"; // 热点密码 WebServer server(80); // OV2640摄像头引脚配置 (AI Thinker模组) #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); // 初始化摄像头配置 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_VGA; // 640x480 config.jpeg_quality = 12; // 0-63 (数值越小质量越高) config.fb_count = 2; // 双缓冲 } else { config.frame_size = FRAMESIZE_SVGA; // 800x600 config.jpeg_quality = 10; config.fb_count = 1; } // 初始化摄像头 esp_err_t err = esp_camera_init(&config); if (err != ESP_OK) { Serial.printf("摄像头初始化失败: 0x%x", err); return; } //sensor_t *s = esp_camera_sensor_get(); //s->set_focus(s, 0); // 0 = 自动对焦 // 设置AP模式 WiFi.softAP(ap_ssid, ap_password); IPAddress IP = WiFi.softAPIP(); Serial.print("AP热点已创建! SSID: "); Serial.println(ap_ssid); Serial.print("访问地址: http://"); Serial.println(IP); // 设置Web服务器路由 server.on("/", HTTP_GET, [](){ String html = "<html><head><meta name='viewport' content='width=device-width, initial-scale=1'>"; html += "<style>body{font-family:Arial;text-align:center;} button{padding:15px;font-size:20px;}</style></head>"; html += "<body>ESP32-CAM 控制台"; html += "<button>拍摄照片</button>"; html += "<button>实时视频流</button>"; html += "</body></html>"; server.send(200, "text/html", html); }); server.on("/capture", HTTP_GET, [](){ camera_fb_t *fb = esp_camera_fb_get(); if (!fb) { Serial.println("Camera capture failed"); server.send(500, "text/plain", "Failed to capture image"); return; } // 发送图像头部信息 server.sendHeader("Content-Type", "image/jpeg"); server.sendHeader("Content-Length", String(fb->len)); server.sendHeader("Connection", "close"); // 发送图像内容 server.send(200); server.sendContent_P((const char*)fb->buf, fb->len); // 返回缓冲区 esp_camera_fb_return(fb); }); //在setup()中释放内存 WiFi.mode(WIFI_AP); WiFi.softAP(ap_ssid, ap_password, 1, 0, 4); //最大连接数4 server.begin(); } void loop() { server.handleClient(); } 为什么网页capture显示的是传输无效数据,无法查看图片

#include "esp_camera.h" #include <WiFi.h> #include <WebServer.h> const char* ap_ssid = "ESP32-CAM-AP"; const char* ap_password = "12345678"; WebServer server(80); // 引脚定义(根据实际接线修改) #define PWDN_GPIO_NUM 32 #define RESET_GPIO_NUM -1 // 不使用复位引脚 #define XCLK_GPIO_NUM 0 #define SIOD_GPIO_NUM 26 // I2C SDA #define SIOC_GPIO_NUM 27 // I2C SCL #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); // 配置摄像头参数 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; // 20MHz(OV2640支持10-24MHz) config.pixel_format = PIXFORMAT_JPEG; // 图像格式:JPEG/RGB565/YUV422等 // 分辨率配置(根据需求选择) config.frame_size = FRAMESIZE_VGA; // VGA(640x480)、QVGA(320x240)、CIF(352x288)等 config.jpeg_quality = 12; // JPEG质量(0-63,数值越小质量越高) config.fb_count = 1; // 帧缓冲数量(1-2) // 初始化摄像头 esp_err_t err = esp_camera_init(&config); if (err != ESP_OK) { Serial.printf("摄像头初始化失败:%s\n", esp_err_to_name(err)); return; } // 检查传感器ID(确认连接正常) sensor_t *s = esp_camera_sensor_get(); if (s->id.PID != OV2640_PID) { // OV2640的PID通常为0x2640 Serial.println("未检测到OV2640传感器!"); return; } Serial.println("摄像头初始化成功!"); // 摄像头启动延迟 delay(500); // 单次WiFi初始化 WiFi.mode(WIFI_AP); if(!WiFi.softAP(ap_ssid, ap_password, 1, 0, 4)) { Serial.println("AP模式启动失败!"); } IPAddress IP = WiFi.softAPIP(); Serial.print("AP IP: "); Serial.println(IP); Serial.printf("连接数: %d\n", WiFi.softAPgetStationNum()); // Web路由 server.on("/", HTTP_GET, [](){ String html = "<html><head><meta name='viewport' content='width=device-width, initial-scale=1'>"; html += "<style>body{font-family:Arial;text-align:center;} button{padding:15px;font-size:20px;}</style></head>"; html += "<body>ESP32-CAM"; html += "<button>take photos</button>"; html += "<button>real-time video</button>"; html += "内存状态: " + String(ESP.getFreeHeap()) + " bytes"; html += "</body></html>"; server.send(200, "text/html", html); }); server.on("/capture", HTTP_GET, [](){ Serial.println("捕获请求"); // 捕获图像(保持原有代码不变) camera_fb_t *fb = NULL; for(int i=0; i<3; i++) { fb = esp_camera_fb_get(); if(fb != NULL && fb->len > 0) break; delay(100); } if (!fb || fb->len == 0) { Serial.println("捕获失败"); server.send(500, "text/plain", "Camera capture failed"); if(fb) esp_camera_fb_return(fb); return; } Serial.printf("捕获成功: %d bytes\n", fb->len); // 优化:合并响应头发送,避免重复的 Content-Length String responseHeaders = "Content-Type: image/jpeg\r\n"; responseHeaders += "Content-Length: " + String(fb->len) + "\r\n"; responseHeaders += "Cache-Control: no-cache, no-store, must-revalidate\r\n"; responseHeaders += "Pragma: no-cache\r\n"; responseHeaders += "Expires: 0\r\n"; // 使用 sendContent() 发送完整响应(包括头和内容) server.sendContent("HTTP/1.1 200 OK\r\n" + responseHeaders + "\r\n"); // 分块发送图像数据(保持原有代码不变) const size_t CHUNK_SIZE = 1024; uint8_t *ptr = fb->buf; size_t remaining = fb->len; while (remaining > 0) { size_t send_size = (remaining < CHUNK_SIZE) ? remaining : CHUNK_SIZE; server.sendContent((const char*)ptr, send_size); ptr += send_size; remaining -= send_size; } esp_camera_fb_return(fb); server.client().stop(); Serial.println("图像已发送"); }); server.begin(); } void loop() { server.handleClient(); // 定期内存检查 static uint32_t last_mem_check = 0; if(millis() - last_mem_check > 5000) { Serial.printf("空闲内存: %d bytes\n", ESP.getFreeHeap()); last_mem_check = millis(); } delay(2000); } 给以上代码增加输出1分钟 3.3V 10kHz 的pwm方波后,再进行拍照 ,其他一定不要变 参考:#include "esp_camera.h" #include <WiFi.h> #include <WebServer.h> #include <driver/ledc.h> // 修正1:更换PWM输出引脚(避免GPIO2) #define OUTPUT_PIN 12 // 改为GPIO12(确保未占用) #define FREQ 10000 #define DUTY_CYCLE 128 // 50%占空比(8位分辨率) #define CHANNEL LEDC_CHANNEL_0 #define TIMER LEDC_TIMER_0 #define RESOLUTION LEDC_TIMER_8_BIT const char* ap_ssid = "ESP32-CAM-AP"; const char* ap_password = "12345678"; // 新增全局变量 const uint32_t PWM_DURATION = 1 * 60 * 1000; // 10分钟(毫秒) bool pwmActive = false; uint32_t pwmStartTime = 0; WebServer server(80); // OV2640引脚配置 #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); // PWM配置(修正引脚) ledc_timer_config_t timer_conf = { .speed_mode = LEDC_LOW_SPEED_MODE, .duty_resolution = RESOLUTION, .timer_num = TIMER, .freq_hz = FREQ, .clk_cfg = LEDC_AUTO_CLK }; ledc_timer_config(&timer_conf); ledc_channel_config_t channel_conf = { .gpio_num = OUTPUT_PIN, .speed_mode = LEDC_LOW_SPEED_MODE, .channel = CHANNEL, .intr_type = LEDC_INTR_DISABLE, .timer_sel = TIMER, .duty = 0, .hpoint = 0 }; ledc_channel_config(&channel_conf); // 在初始化PWM后,启动PWM输出 ledc_set_duty(LEDC_LOW_SPEED_MODE, CHANNEL, 128); // 50%占空比(因为8位分辨率,128/256=0.5) ledc_update_duty(LEDC_LOW_SPEED_MODE, CHANNEL); pwmStartTime = millis(); pwmActive = true; Serial.setDebugOutput(true); // 启用详细调试 // 初始化前设置PWDN引脚 pinMode(PWDN_GPIO_NUM, OUTPUT); digitalWrite(PWDN_GPIO_NUM, LOW); // 唤醒摄像头 // 摄像头配置 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_VGA; config.jpeg_quality = 12; config.fb_count = 2; } else { config.frame_size = FRAMESIZE_SVGA; config.jpeg_quality = 10; config.fb_count = 1; } // 初始化摄像头 esp_err_t err = esp_camera_init(&config); if (err != ESP_OK) { Serial.printf("摄像头初始化失败: 0x%x\n", err); // 详细错误处理 if(err == ESP_ERR_CAMERA_FAILED_TO_SET_FRAME_SIZE) { Serial.println("错误:无法设置分辨率"); } return; } // 摄像头启动延迟 delay(500); // 单次WiFi初始化 WiFi.mode(WIFI_AP); if(!WiFi.softAP(ap_ssid, ap_password, 1, 0, 4)) { Serial.println("AP模式启动失败!"); } IPAddress IP = WiFi.softAPIP(); Serial.print("AP IP: "); Serial.println(IP); Serial.printf("连接数: %d\n", WiFi.softAPgetStationNum()); // Web路由 server.on("/", HTTP_GET, [](){ String html = "<html><head><meta name='viewport' content='width=device-width, initial-scale=1'>"; html += "<style>body{font-family:Arial;text-align:center;} button{padding:15px;font-size:20px;}</style></head>"; html += "<body>ESP32-CAM"; html += "<button>take photos</button>"; html += "<button>real-time video</button>"; html += "内存状态: " + String(ESP.getFreeHeap()) + " bytes"; html += "</body></html>"; server.send(200, "text/html", html); }); server.on("/capture", HTTP_GET, [](){ // 检查PWM是否还在输出 if (pwmActive) { server.send(503, "text/plain", "Camera not ready: PWM is still active (wait 10 minutes after power-on)"); return; } Serial.println("捕获请求"); // 捕获图像(保持原有代码不变) camera_fb_t *fb = NULL; for(int i=0; i<3; i++) { fb = esp_camera_fb_get(); if(fb != NULL && fb->len > 0) break; delay(100); } if (!fb || fb->len == 0) { Serial.println("捕获失败"); server.send(500, "text/plain", "Camera capture failed"); if(fb) esp_camera_fb_return(fb); return; } Serial.printf("捕获成功: %d bytes\n", fb->len); // 优化:合并响应头发送,避免重复的 Content-Length String responseHeaders = "Content-Type: image/jpeg\r\n"; responseHeaders += "Content-Length: " + String(fb->len) + "\r\n"; responseHeaders += "Cache-Control: no-cache, no-store, must-revalidate\r\n"; responseHeaders += "Pragma: no-cache\r\n"; responseHeaders += "Expires: 0\r\n"; // 使用 sendContent() 发送完整响应(包括头和内容) server.sendContent("HTTP/1.1 200 OK\r\n" + responseHeaders + "\r\n"); // 分块发送图像数据(保持原有代码不变) const size_t CHUNK_SIZE = 1024; uint8_t *ptr = fb->buf; size_t remaining = fb->len; while (remaining > 0) { size_t send_size = (remaining < CHUNK_SIZE) ? remaining : CHUNK_SIZE; server.sendContent((const char*)ptr, send_size); ptr += send_size; remaining -= send_size; } esp_camera_fb_return(fb); server.client().stop(); Serial.println("图像已发送"); }); server.begin(); } void loop() { // 检查PWM是否已经输出满10分钟 if (pwmActive && (millis() - pwmStartTime >= 60000)) { // 10分钟=600000毫秒 // 停止PWM:设置占空比为0 ledc_set_duty(LEDC_LOW_SPEED_MODE, CHANNEL, 0); ledc_update_duty(LEDC_LOW_SPEED_MODE, CHANNEL); pwmActive = false; // 标记停止 Serial.println("PWM output stopped after 10 minutes."); } server.handleClient(); // 定期内存检查 static uint32_t last_mem_check = 0; if(millis() - last_mem_check > 5000) { Serial.printf("空闲内存: %d bytes\n", ESP.getFreeHeap()); last_mem_check = millis(); } }

#include "esp_camera.h" #include <WiFi.h> #include <WebServer.h> #include <driver/ledc.h> #define OUTPUT_PIN 12 // 改为GPIO12(确保未占用) #define FREQ 10000 #define DURATION 3000 #define CHANNEL LEDC_CHANNEL_0 #define TIMER LEDC_TIMER_0 #define RESOLUTION LEDC_TIMER_8_BIT const char* ap_ssid = "ESP32-CAM-AP"; const char* ap_password = "12345678"; WebServer server(80); // OV2640引脚配置 #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); // PWM配置(修正引脚) ledc_timer_config_t timer_conf = { .speed_mode = LEDC_LOW_SPEED_MODE, .duty_resolution = RESOLUTION, .timer_num = TIMER, .freq_hz = FREQ, .clk_cfg = LEDC_AUTO_CLK }; ledc_timer_config(&timer_conf); ledc_channel_config_t channel_conf = { .gpio_num = OUTPUT_PIN, .speed_mode = LEDC_LOW_SPEED_MODE, .channel = CHANNEL, .intr_type = LEDC_INTR_DISABLE, .timer_sel = TIMER, .duty = 0, .hpoint = 0 }; ledc_channel_config(&channel_conf); Serial.setDebugOutput(true); // 启用详细调试 // 初始化前设置PWDN引脚 pinMode(PWDN_GPIO_NUM, OUTPUT); digitalWrite(PWDN_GPIO_NUM, LOW); // 唤醒摄像头 // 摄像头配置 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_VGA; config.jpeg_quality = 12; config.fb_count = 2; } else { config.frame_size = FRAMESIZE_SVGA; config.jpeg_quality = 10; config.fb_count = 1; } // 初始化摄像头 esp_err_t err = esp_camera_init(&config); if (err != ESP_OK) { Serial.printf("摄像头初始化失败: 0x%x\n", err); // 详细错误处理 if(err == ESP_ERR_CAMERA_FAILED_TO_SET_FRAME_SIZE) { Serial.println("错误:无法设置分辨率"); } return; } // 摄像头启动延迟 delay(500); // 单次WiFi初始化 WiFi.mode(WIFI_AP); if(!WiFi.softAP(ap_ssid, ap_password, 1, 0, 4)) { Serial.println("AP模式启动失败!"); } IPAddress IP = WiFi.softAPIP(); Serial.print("AP IP: "); Serial.println(IP); Serial.printf("连接数: %d\n", WiFi.softAPgetStationNum()); // Web路由 server.on("/", HTTP_GET, [](){ String html = "<html><head><meta name='viewport' content='width=device-width, initial-scale=1'>"; html += "<style>body{font-family:Arial;text-align:center;} button{padding:15px;font-size:20px;}</style></head>"; html += "<body>ESP32-CAM"; html += "<button>take photos</button>"; html += "<button>real-time video</button>"; html += "内存状态: " + String(ESP.getFreeHeap()) + " bytes"; html += "</body></html>"; server.send(200, "text/html", html); }); server.on("/capture", HTTP_GET, [](){ Serial.println("捕获请求"); // 捕获图像(保持原有代码不变) camera_fb_t *fb = NULL; for(int i=0; i<3; i++) { fb = esp_camera_fb_get(); if(fb != NULL && fb->len > 0) break; delay(100); } if (!fb || fb->len == 0) { Serial.println("捕获失败"); server.send(500, "text/plain", "Camera capture failed"); if(fb) esp_camera_fb_return(fb); return; } Serial.printf("捕获成功: %d bytes\n", fb->len); // 优化:合并响应头发送,避免重复的 Content-Length String responseHeaders = "Content-Type: image/jpeg\r\n"; responseHeaders += "Content-Length: " + String(fb->len) + "\r\n"; responseHeaders += "Cache-Control: no-cache, no-store, must-revalidate\r\n"; responseHeaders += "Pragma: no-cache\r\n"; responseHeaders += "Expires: 0\r\n"; // 使用 sendContent() 发送完整响应(包括头和内容) server.sendContent("HTTP/1.1 200 OK\r\n" + responseHeaders + "\r\n"); // 分块发送图像数据(保持原有代码不变) const size_t CHUNK_SIZE = 1024; uint8_t *ptr = fb->buf; size_t remaining = fb->len; while (remaining > 0) { size_t send_size = (remaining < CHUNK_SIZE) ? remaining : CHUNK_SIZE; server.sendContent((const char*)ptr, send_size); ptr += send_size; remaining -= send_size; } esp_camera_fb_return(fb); server.client().stop(); Serial.println("图像已发送"); }); server.begin(); } void loop() { // PWM输出 ledc_set_duty(LEDC_LOW_SPEED_MODE, CHANNEL, 128); ledc_update_duty(LEDC_LOW_SPEED_MODE, CHANNEL); delay(DURATION); server.handleClient(); // 定期内存检查 static uint32_t last_mem_check = 0; if(millis() - last_mem_check > 5000) { Serial.printf("空闲内存: %d bytes\n", ESP.getFreeHeap()); last_mem_check = millis(); } } 以上代码实现了什么功能

#include "esp_camera.h" #include <WiFi.h> #include <WebServer.h> #include <driver/ledc.h> // 修正1:更换PWM输出引脚(避免GPIO2) #define OUTPUT_PIN 12 // 改为GPIO12(确保未占用) #define FREQ 10000 #define DUTY_CYCLE 128 // 50%占空比(8位分辨率) #define CHANNEL LEDC_CHANNEL_0 #define TIMER LEDC_TIMER_0 #define RESOLUTION LEDC_TIMER_8_BIT const char* ap_ssid = "ESP32-CAM-AP"; const char* ap_password = "12345678"; // 新增全局变量 const uint32_t PWM_DURATION = 1 * 60 * 1000; // 10分钟(毫秒) bool pwmActive = false; uint32_t pwmStartTime = 0; WebServer server(80); // OV2640引脚配置 #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); // PWM配置(修正引脚) ledc_timer_config_t timer_conf = { .speed_mode = LEDC_LOW_SPEED_MODE, .duty_resolution = RESOLUTION, .timer_num = TIMER, .freq_hz = FREQ, .clk_cfg = LEDC_AUTO_CLK }; ledc_timer_config(&timer_conf); ledc_channel_config_t channel_conf = { .gpio_num = OUTPUT_PIN, .speed_mode = LEDC_LOW_SPEED_MODE, .channel = CHANNEL, .intr_type = LEDC_INTR_DISABLE, .timer_sel = TIMER, .duty = 0, .hpoint = 0 }; ledc_channel_config(&channel_conf); // 在初始化PWM后,启动PWM输出 ledc_set_duty(LEDC_LOW_SPEED_MODE, CHANNEL, 128); // 50%占空比(因为8位分辨率,128/256=0.5) ledc_update_duty(LEDC_LOW_SPEED_MODE, CHANNEL); pwmStartTime = millis(); pwmActive = true; Serial.setDebugOutput(true); // 启用详细调试 // 初始化前设置PWDN引脚 pinMode(PWDN_GPIO_NUM, OUTPUT); digitalWrite(PWDN_GPIO_NUM, LOW); // 唤醒摄像头 // 摄像头配置 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_VGA; config.jpeg_quality = 12; config.fb_count = 2; } else { config.frame_size = FRAMESIZE_SVGA; config.jpeg_quality = 10; config.fb_count = 1; } // 初始化摄像头 esp_err_t err = esp_camera_init(&config); if (err != ESP_OK) { Serial.printf("摄像头初始化失败: 0x%x\n", err); // 详细错误处理 if(err == ESP_ERR_CAMERA_FAILED_TO_SET_FRAME_SIZE) { Serial.println("错误:无法设置分辨率"); } return; } // 摄像头启动延迟 delay(500); // 单次WiFi初始化 WiFi.mode(WIFI_AP); if(!WiFi.softAP(ap_ssid, ap_password, 1, 0, 4)) { Serial.println("AP模式启动失败!"); } IPAddress IP = WiFi.softAPIP(); Serial.print("AP IP: "); Serial.println(IP); Serial.printf("连接数: %d\n", WiFi.softAPgetStationNum()); // Web路由 server.on("/", HTTP_GET, [](){ String html = "<html><head><meta name='viewport' content='width=device-width, initial-scale=1'>"; html += "<style>body{font-family:Arial;text-align:center;} button{padding:15px;font-size:20px;}</style></head>"; html += "<body>ESP32-CAM"; html += "<button>take photos</button>"; html += "<button>real-time video</button>"; html += "内存状态: " + String(ESP.getFreeHeap()) + " bytes"; html += "</body></html>"; server.send(200, "text/html", html); }); server.on("/capture", HTTP_GET, [](){ // 检查PWM是否还在输出 if (pwmActive) { server.send(503, "text/plain", "Camera not ready: PWM is still active (wait 10 minutes after power-on)"); return; } Serial.println("捕获请求"); // 捕获图像(保持原有代码不变) camera_fb_t *fb = NULL; for(int i=0; i<3; i++) { fb = esp_camera_fb_get(); if(fb != NULL && fb->len > 0) break; delay(100); } if (!fb || fb->len == 0) { Serial.println("捕获失败"); server.send(500, "text/plain", "Camera capture failed"); if(fb) esp_camera_fb_return(fb); return; } Serial.printf("捕获成功: %d bytes\n", fb->len); // 优化:合并响应头发送,避免重复的 Content-Length String responseHeaders = "Content-Type: image/jpeg\r\n"; responseHeaders += "Content-Length: " + String(fb->len) + "\r\n"; responseHeaders += "Cache-Control: no-cache, no-store, must-revalidate\r\n"; responseHeaders += "Pragma: no-cache\r\n"; responseHeaders += "Expires: 0\r\n"; // 使用 sendContent() 发送完整响应(包括头和内容) server.sendContent("HTTP/1.1 200 OK\r\n" + responseHeaders + "\r\n"); // 分块发送图像数据(保持原有代码不变) const size_t CHUNK_SIZE = 1024; uint8_t *ptr = fb->buf; size_t remaining = fb->len; while (remaining > 0) { size_t send_size = (remaining < CHUNK_SIZE) ? remaining : CHUNK_SIZE; server.sendContent((const char*)ptr, send_size); ptr += send_size; remaining -= send_size; } esp_camera_fb_return(fb); server.client().stop(); Serial.println("图像已发送"); }); server.begin(); } void loop() { // 检查PWM是否已经输出满10分钟 if (pwmActive && (millis() - pwmStartTime >= 60000)) { // 10分钟=600000毫秒 // 停止PWM:设置占空比为0 ledc_set_duty(LEDC_LOW_SPEED_MODE, CHANNEL, 0); ledc_update_duty(LEDC_LOW_SPEED_MODE, CHANNEL); pwmActive = false; // 标记停止 Serial.println("PWM output stopped after 10 minutes."); } server.handleClient(); // 定期内存检查 static uint32_t last_mem_check = 0; if(millis() - last_mem_check > 5000) { Serial.printf("空闲内存: %d bytes\n", ESP.getFreeHeap()); last_mem_check = millis(); } } 以上代码当esp32-cam 连接了ov2640 m12后输出捕获失败,修改代码使ov2640 m12可以拍摄照片











