I'm trying to create a AsyncWebServer
after enabling the SoftAP
:
#include <Arduino.h>
#include <SPIFFS.h>
#include <WiFi.h>
#include <WiFiAP.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
WebApp webapp;
void setup()
{
Serial.begin(115200);
SPIFFS.begin();
File f = SPIFFS.open("/index.html");
String s = f.readString();
Serial.println(s);
f.close();
WiFi.softAP("ssid", NULL);
WiFi.softAPsetHostname("ssid");
Serial.println(WiFi.softAPIP());
webapp.Begin();
}
void loop()
{
}
class WebApp
{
public:
WebApp();
void Begin();
private:
AsyncWebServer _server;
};
#include "webapp.h"
WebApp::WebApp() : _server(80)
{
}
void WebApp::Begin()
{
_server.on("/", HTTP_GET, [this](AsyncWebServerRequest *request)
{
Serial.println("GET request");
_server.serveStatic("/", SPIFFS, "/").setDefaultFile("/index.html");
});
_server.onNotFound([](AsyncWebServerRequest *request){
request->send(404);
});
_server.begin();
Serial.println("[WEB] HTTP server started");
}
The SoftAP
brings up the network and I can connect to it (softAPgetStationNum()
tells me that 1 client is connected). It answers to pings.
The index.html
is stored into the flash (the contents is printed out correctly).
But when I try to open the root page (192.168.4.1) it prints out "GET request" but the browser request timeouts and nothing is received.
Am I missing anything in the code?
2 Answers 2
I don't have enough rep to make a comment on your post, but is it possible that when you give the parameter for the function setDefaultFile()
that the forward slash on /index.html
is causing the path of the file to be //index.html
instead of /index.html
.
Because serveStatic is probably appending the filename of setDefaultFile()
to the root directory of /
(the third parameter of serveStatic()
), you're probably getting an extra character you don't want in your filepath.
TL;DR you don't need the /
in index.html
for setDefaultFile()
.
See https://github.com/me-no-dev/ESPAsyncWebServer#serving-files-in-directory for the example.
-
I'll try this as soon as possible. But in that case should not just fall into a 404?Mark– Mark2022年06月08日 05:28:27 +00:00Commented Jun 8, 2022 at 5:28
-
Tried. It changes nothing. I can reach the page using the complete address (i.e.
192.168.4.1/index.html
) but often the wdt triggers and the ESP32 reboots. The pages are very light, less than 6 kBytesMark– Mark2022年06月08日 07:05:27 +00:00Commented Jun 8, 2022 at 7:05 -
Reloading the page usually leads to 404Mark– Mark2022年06月08日 07:12:01 +00:00Commented Jun 8, 2022 at 7:12
-
I confirm the issue is still there.Mark– Mark2022年06月16日 11:37:32 +00:00Commented Jun 16, 2022 at 11:37
The usage of serveStatic()
is wrong. It's not intended to be used inside a callback. Just place the call in the setup of the webserver, i.e.:
void WebApp::Begin()
{
_server.serveStatic("/", SPIFFS, "/").setDefaultFile("/index.html");
_server.onNotFound([](AsyncWebServerRequest *request)
{
request->send(404);
});
_server.begin();
Serial.println("[WEB] HTTP server started");
}