我在使用ESP32时遇到了AsyncWebServer的问题。访问代码的url是ip/inline?a=5数字a=5控制AsyncWebServer将向客户端发送多少字符。当我通过浏览器访问它时,如果字符数在1到500个范围内,它可以正常工作,浏览器通过在窗口中显示数据来工作。然而,当我到达600及以上时,董事会或重置或停止响应一段时间,浏览器窗口永远不会恢复。最初我认为这是内存的一个问题,因为字符串不能有2000+字符,所以它已经很低了。
有人能告诉我怎么解决吗?另外,我想显示一个非常长的HTML字符串给浏览器,可能有10K字符。我能做这个吗?多么?它与其他应用程序代码相结合。我抄袭并简化了它。编译器在收到实际应用程序后的消息显示,我仍然有很多可用的内存。
我的密码在这里
#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include <ESPmDNS.h>
const char* ssid = "****";
const char* password = "***";
;
WebServer server(80);
const int led = 13;
void handleRoot() {
digitalWrite(led, 1);
server.send(200, "text/plain", "hello from esp32!");
digitalWrite(led, 0);
}
void handleNotFound() {
digitalWrite(led, 1);
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET) ? "GET" : "POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i = 0; i < server.args(); i++) {
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
digitalWrite(led, 0);
}
void setup(void) {
pinMode(led, OUTPUT);
digitalWrite(led, 0);
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
if (MDNS.begin("esp32")) {
Serial.println("MDNS responder started");
}
server.on("/", handleRoot);
server.on("/inline", []()
{
String content="\0";
String random_data=F("<br />Apart from counting words and characters, our online editor can help you to improve 1234<br />");
for (int i=1;i<=100*9;i++)
{
//serial.print(i);
content=content+i;
content=content+random_data;
}
server.send(200, "text/html", content);
});
server.onNotFound(handleNotFound);
server.begin();
Serial.println("HTTP server started");
}
void loop(void) {
server.handleClient();
delay(2);//allow the cpu to switch to other tasks
}发布于 2022-10-19 09:42:42
代码的格式不正确。
首先是一个正确的模型,并给出了一个示例:
server.on("/inline", HTTP_GET, [](AsyncWebServerRequest *request) {
// Check if we have a param.
// If not, return with 500 internal error.
if( !request->hasParam("a") ){
request->send(500, "text/plain", "Please fill param!");
}
AsyncWebParameter* p = request->getParam("a");
int charAmountRequested = p->value().toInt();
// Create a char array with the requested size.
char content[charAmountRequested];
for (size_t i = 0; i < charAmountRequested; i++){
content[i] = 'g';
}
request->send(200, "text/html", content);
});如果您绝对想用字符串来完成它,下面是另一个示例:
server.on("/inline", HTTP_GET, [](AsyncWebServerRequest *request) {
// Check if we have a param.
// If not, return with 500 internal error.
if( !request->hasParam("a") ){
request->send(500, "text/plain", "Please fill param!");
}
String content;
AsyncWebParameter* p = request->getParam("a");
int charAmountRequested = p->value().toInt();
for (size_t i = 0; i < charAmountRequested; i++){
content += "g";
}
request->send(200, "text/html", content);
});注:这些是未经测试的代码,但它应该给你一个想法。
https://stackoverflow.com/questions/73934246
复制相似问题