我想在这个脚本里放一个已经制作好的网站,这样我就不用再写整个网站了
ESP32器件模型
#include <WiFi.h>
#include <DNSServer.h>
#include <WebServer.h>
const byte DNS_PORT = 53;
IPAddress apIP(172, 217, 28, 1);
DNSServer dnsServer;
WebServer webServer(80);
String responseHTML = ****""
"<!DOCTYPE html><html lang='en'><head>"
"<meta name='viewport' content='width=device-width'>"
"<title>CaptivePortal</title></head><body>"
"<h1>Hello World!</h1><p>This is a captive portal example."
" All requests will be redirected here.</p></body></html>";***
void setup() {
WiFi.mode(WIFI_AP);
WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0));
WiFi.softAP("Vodafone");
// if DNSServer is started with "*" for domain name, it will reply with
// provided IP to all DNS request
dnsServer.start(DNS_PORT, "*", apIP);
// replay to all requests with same HTML
webServer.onNotFound([]() {
webServer.send(200, "text/html", responseHTML);
});
webServer.begin();
}
void loop() {
dnsServer.processNextRequest();
webServer.handleClient();
}在运行代码时,它将使用所编写的html创建站点。
但是我想放一个更完整的html,而不必写这整个站点的行。
也就是说,就好像我们将文件导入到脚本中一样?
发布于 2022-11-17 03:44:01
ESP允许您将Flash内存的一部分划分为用于数据存储的文件系统,它可以用于保存html、css、Javascript文件等内容,并在需要时检索它。
data目录,并将所有html文件、css和JavaScript放到目录中。data目录所需的大小)。index.html上提供主页/,如下所示: server.serveStatic("/", SPIFFS, "/") // first '/' - html route, second '/' - root directory of 'data' directory in LITTLEFS
.setDefaultFile("index.html") // set html home page
.setCacheControl("max-age=86400"); // set cache expire我有一个关于如何在我的ESP32上使用ESPAsynWebServer为ESPAsynWebServer文件系统提供内容的示例,您可以查看更详细的实现。我的例子是基于LittleFS的with,它已经折旧并被所取代,但是这个API在一般情况下是兼容的。
https://stackoverflow.com/questions/74465362
复制相似问题