正如标题所说。以下是我从博客中获取的 cam.ino 代码。
#include "esp_camera.h"
WiFiServer camServer(81);
bool connected = false;
WiFiClient live_client;
String index_html = "<meta charset=\"utf-8\"/>\n" \
"<style>\n" \
"#content {\n" \
"display: flex;\n" \
"flex-direction: column;\n" \
"justify-content: center;\n" \
"align-items: center;\n" \
"text-align: center;\n" \
"min-height: 100vh;}\n" \
"</style>\n" \
"<body bgcolor=\"#000000\"><div id=\"content\"><h2 style=\"color:#ffffff\">HTTP ESP32 Cam live stream </h2><img src=\"video\"></div></body>";
void configCamera(){
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_UXGA;
config.jpeg_quality = 10;
config.fb_count = 2;
esp_err_t err = esp_camera_init(&config);
if (err != ESP_OK) {
Serial.printf("Camera init failed with error 0x%x", err);
return;
}
}
//continue sending camera frame
void liveCam(WiFiClient &client){
//capture a frame
camera_fb_t * fb = esp_camera_fb_get();
if (!fb) {
Serial.println("Frame buffer could not be acquired");
return;
}
client.print("--frame\n");
client.print("Content-Type: image/jpeg\n\n");
client.flush();
client.write(fb->buf, fb->len);
client.flush();
client.print("\n");
//return the frame buffer back to be reused
esp_camera_fb_return(fb);
}
void http_resp(){
WiFiClient client = camServer.available();
/* check client is connected */
if (client.connected()) {
/* client send request? */
/* request end with '\r' -> this is HTTP protocol format */
String req = "";
while(client.available()){
req += (char)client.read();
}
Serial.println("request " + req);
/* First line of HTTP request is "GET / HTTP/1.1"
here "GET /" is a request to get the first page at root "/"
"HTTP/1.1" is HTTP version 1.1
*/
/* now we parse the request to see which page the client want */
int addr_start = req.indexOf("GET") + strlen("GET");
int addr_end = req.indexOf("HTTP", addr_start);
if (addr_start == -1 || addr_end == -1) {
Serial.println("Invalid request " + req);
return;
}
req = req.substring(addr_start, addr_end);
req.trim();
Serial.println("Request: " + req);
client.flush();
String s;
/* if request is "/" then client request the first page at root "/" -> we process this by return "Hello world"*/
if (req == "/")
{
s = "HTTP/1.1 200 OK\n";
s += "Content-Type: text/html\n\n";
s += index_html;
s += "\n";
client.print(s);
client.stop();
}
else if (req == "/video")
{
live_client = client;
live_client.print("HTTP/1.1 200 OK\n");
live_client.print("Content-Type: multipart/x-mixed-replace; boundary=frame\n\n");
live_client.flush();
connected = true;
}
else
{
/* if we can not find the page that client request then we return 404 File not found */
s = "HTTP/1.1 404 Not Found\n\n";
client.print(s);
client.stop();
}
}
}
void startCamera() {
camServer.begin();
configCamera();
}
void camLoop() {
http_resp();
if(connected == true){
liveCam(live_client);
}
}
这是我的 main.ino:
void setup() {
Serial.begin(115200);
Serial.setDebugOutput(true);
startWifi();
startCamera();
}
void loop() {
camLoop();
}
相机流运行良好。关闭浏览器选项卡后,我收到以下错误:
Guru Meditation Error: Core 1 panic'ed (LoadProhibited). Exception was unhandled.
Core 1 register dump:
PC : 0x400d751b PS : 0x00060330 A0 : 0x800d763c A1 : 0x3ffb21e0
A2 : 0x00000000 A3 : 0x00000000 A4 : 0x00000e31 A5 : 0x00000004
A6 : 0x3ffc524c A7 : 0x00000032 A8 : 0x800d7854 A9 : 0x3ffb21a0
A10 : 0x3ffb21c4 A11 : 0x00000000 A12 : 0x00000e31 A13 : 0x00000000
A14 : 0x3ffc5260 A15 : 0x00000001 SAR : 0x00000019 EXCCAUSE: 0x0000001c
EXCVADDR: 0x00000010 LBEG : 0x4008b014 LEND : 0x4008b030 LCOUNT : 0xffffffff
Backtrace: 0x400d7518:0x3ffb21e0 0x400d7639:0x3ffb2210 0x400d2eb0:0x3ffb2230 0x400d4967:0x3ffb2250 0x400d496f:0x3ffb2270 0x400e5261:0x3ffb2290
我猜这与循环变得空闲有关?但不确定如何修复它。将不胜感激任何帮助。预先感谢!
无法分析核心崩溃的原因,但最终您可以自行调试原因。我建议使用 here
中的 ESPExceptionDecoder