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

ESP32 DevkitC V4 WebSocket服务器无法访问问题求助

问题排查:ESP32 Web服务器无响应问题

我正在开发一款由ESP32托管、通过WebSocket通信的自定义Web应用控制的简易移动机器人。原本正常运行的代码,中断开发后无法工作——大概率是误改代码或对部分逻辑理解有误。串口日志显示程序已连接WiFi、成功挂载SPIFFS、服务器已在指定IP启动,但访问该IP时无任何响应。目前仅需实现页面托管访问功能,以下是我的.ino代码(HTML和.js文件可补充),数据文件夹已上传至闪存。

#include <Arduino.h>
#include <WiFi.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include "SPIFFS.h"
#include <Arduino_JSON.h>

// Network credentials
const char *ssid = "mySSID";
const char *password = "myCredentials";

// Create AsyncWebServer object on port 80
AsyncWebServer server(80);

// Create a WebSocket object
AsyncWebSocket ws("/ws");

// Placeholder for messages from the clients
String message = "";

void initFS()
{
  if (!SPIFFS.begin())
  {
    Serial.println("An error has occurred while mounting SPIFFS");
  }
  else
  {
    Serial.println("SPIFFS mounted successfully");
  }
}

// Initialise WiFi
void initWiFi()
{
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  Serial.print("Connecting to WiFi ..");
  while (WiFi.status() != WL_CONNECTED)
  {
    Serial.print('.');
    delay(1000);
  }
  Serial.println(WiFi.localIP());
}

void handleWebSocketMessage(void *arg, uint8_t *data, size_t len)
{
  AwsFrameInfo *info = (AwsFrameInfo *)arg;
  if (info->final && info->index == 0 && info->len == len && info->opcode == WS_TEXT)
  {
    data[len] = 0;
    message = (char *)data;
  }
}

void onEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type, void *arg, uint8_t *data, size_t len)
{
  switch (type)
  {
  case WS_EVT_CONNECT:
    Serial.printf("WebSocket client #%u connected from %s\n", client->id(), client->remoteIP().toString().c_str());
    break;
  case WS_EVT_DISCONNECT:
    Serial.printf("WebSocket client #%u disconnected\n", client->id());
    break;
  case WS_EVT_DATA:
    handleWebSocketMessage(arg, data, len);
    break;
  case WS_EVT_PONG:
  case WS_EVT_ERROR:
    break;
  }
}

void initWebSocket()
{
  Serial.println("I'm Inside!");
  ws.onEvent(onEvent);
  server.addHandler(&ws);
}

void setup()
{
  Serial.begin(115200);

  Serial.println("Initialising SPIFFS");
  initFS();

  Serial.println("Initialising WiFi");
  initWiFi();

  Serial.println("Initialising WebSocket");
  initWebSocket();

  // Web Server Root URL
  server.on("/", HTTP_GET, [](AsyncWebServerRequest *request)
            { request->send(SPIFFS, "/index.html", "text/html"); });

  server.serveStatic("/", SPIFFS, "/");

  // Start server
  server.begin();
  
  Serial.println("Starting Server");
}

void loop()
{
  ws.cleanupClients();
}

更新:我编写了另一极简测试代码,日志显示已连接WiFi,但访问服务器IP仍超时,代码如下:

#include <WiFi.h>
#include <WebServer.h>

//SSID and Password to the local WiFi
const char* ssid = "ssid";
const char* password = "password";

WebServer server(80);

String webpage = "<!DOCTYPE html><html lang='en'><head> <meta charset='UTF-8'> <meta http-equiv='X-UA-Compatible' content='IE=edge'> <meta name='viewport' content='width=device-width, initial-scale=1.0'> <title>Document</title></head><body> <h1> Test! </h1></body></html>";

//Function to initialise the connection with local WiFi
void InitWiFi()
{  
  WiFi.begin(ssid, password);
  Serial.println("Trying to connect to the WiFi with SSID: " + String(ssid) + "..");

  while(WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print(".");
}
  Serial.println("Connected to the WiFi with the IP address: ");
  Serial.println(WiFi.localIP());
}

void setup() {
  Serial.begin(115200);
  InitWiFi();

  //Initialising WebServer
  server.on("/", []() {
  server.send(200, "text\html", webpage);
  });

  delay(1000);
  server.begin();
  Serial.println("Server started");
 
}

void loop() {
  server.handleClient();
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 03:25:28