You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ESP32 Wrover模块搭建HTTPS服务器遇文件系统挂载错误求助

ESP32 Wrover HTTPS服务器文件系统挂载错误修复

问题背景

编写ESP32 Wrover HTTPS服务器代码后,程序下载成功,但设备复位后出现以下文件系统挂载错误:

E (1559) SPIFFS: mount failed, -10025 
E (16717) esp_littlefs: ./components/esp_littlefs/src/littlefs/lfs.c:1347:error: Corrupted dir pair at {0x0, 0x1}
E (16718) esp_littlefs: mount failed,  (-84)
E (16721) esp_littlefs: Failed to initialize LittleFS

证书文件server.key和server.pem已放置在sketch的data文件夹中,多次修改代码仍未解决问题。

错误原因分析

  1. 文件系统冲突:代码中同时尝试挂载SPIFFS和LittleFS,ESP32默认Flash分区仅支持挂载一种文件系统,两种挂载操作互相干扰导致失败。
  2. LittleFS挂载参数错误:LittleFS.begin("spiffs")参数无效,正确的挂载调用应使用格式化标记而非分区名称。
  3. 文件路径错误:上传到文件系统的文件直接位于根目录,代码中使用/data/server.pem的路径不存在。
  4. 私钥读取对象错误:读取私钥时误用了证书文件对象certFile,导致读取内容错误。
  5. 服务器循环逻辑缺失:原loop()函数为空,HTTPS服务器无法定期处理客户端请求,会导致服务无响应。

修复步骤

  1. 统一文件系统:移除SPIFFS相关代码,仅使用LittleFS(性能更优、稳定性更强)。
  2. 修正LittleFS挂载方式:使用LittleFS.begin(FORMAT_LITTLEFS_IF_FAILED)自动格式化损坏的分区。
  3. 修正文件路径:将文件路径改为/server.pem和/server.key。
  4. 修正私钥读取逻辑:使用privateKeyFile读取私钥数据。
  5. 补充服务器循环逻辑:在loop()中添加secureServer.loop()调用,处理客户端请求。
  6. 重新上传文件:确保server.key和server.pem通过ESP32文件上传工具(如Arduino IDE的"Upload Filesystem Image")正确写入LittleFS。

完整修正代码

#include <WiFi.h>
#include <HTTPSServer.hpp> 
#include <SSLCert.hpp> 
#include <LittleFS.h> 

// 网络凭据
const char* ssid = "Buffalo-G-0238";
const char* password = "xvpi4w8km4bhr";

using namespace httpsserver;

#define FORMAT_LITTLEFS_IF_FAILED true

SSLCert cert;
HTTPSServer secureServer(&cert); // 全局声明服务器对象,确保loop中可访问

void setup() {
  Serial.begin(115200);
  
  // 连接WiFi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
  Serial.println("Connected to WiFi");
  Serial.print("IP Address: ");
  Serial.println(WiFi.localIP());

  // 挂载LittleFS,失败则自动格式化
  if (!LittleFS.begin(FORMAT_LITTLEFS_IF_FAILED)) {
    Serial.println("LittleFS mount failed, formatting...");
    ESP.restart();
  }

  // 打开证书文件
  File certFile = LittleFS.open("/server.pem", "r");
  if (!certFile) {
    Serial.println("Failed to open cert file");
    return;
  }

  // 打开私钥文件
  File privateKeyFile = LittleFS.open("/server.key", "r");
  if (!privateKeyFile) {
    Serial.println("Failed to open private key file");
    return;
  }

  // 加载证书数据
  const uint16_t certSize = certFile.size();
  unsigned char certData[certSize];
  certFile.read(certData, certSize);
  cert.setCert(certData, certSize);
  certFile.close(); // 及时关闭文件

  // 加载私钥数据(修正:使用privateKeyFile读取)
  const uint16_t prikeySize = privateKeyFile.size();
  unsigned char prikeyData[prikeySize];
  privateKeyFile.read(prikeyData, prikeySize);
  cert.setPK(prikeyData, prikeySize);
  privateKeyFile.close(); // 及时关闭文件

  // 注册根路径GET请求处理
  ResourceNode* nodeRoot = new ResourceNode("/", "GET", [](HTTPRequest* req, HTTPResponse* res) {
    if (req && res) {
      res->setHeader("Content-Type", "text/html");
      res->println("<!DOCTYPE html><html><body><h1>Hello, world!</h1></body></html>");
    }
  });
  secureServer.registerNode(nodeRoot);

  // 启动服务器
  secureServer.start();
  if (secureServer.isRunning()) {
    Serial.println("Server ready.");
  }
}

void loop() {
  // 定期处理服务器请求,必须调用
  secureServer.loop();
  delay(10);
}

内容的提问来源于stack exchange,提问作者Iyaps

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 11:55:38