英文:
'HTML' is not captured ESP32
问题
I have a server running on ESP32 using ESPAsyncServer And here
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send_P(200, "text/plain", String(HTML));
});
this is giving me an error saying
error: 'HTML' is not captured
I have an HTML string
String HTML PROGMEM = "<!DOCTYPE html>";
HTML += "<html>";
HTML += "<head>";
HTML += "<title> Helixis </title>";
HTML += "<script>";
HTML += "let gateway = `ws:${window.location.hostname}/ws`";
HTML += "function initWS(){";
HTML += "console.log('Connection to WebSocket ... ')";
HTML += "let ws = new WebSocket(gateway)";
HTML += "ws.onopen = onOpen";
HTML += "ws.onclose = onClose";
HTML += "ws.onmessage = onMessage";
HTML += "}";
HTML += "function onOpen(){";
HTML += "console.log('Successfully connected to the WebSocket')";
HTML += "}";
HTML += "function onClose(){";
HTML += "console.log('WebSocket connection closed')";
HTML += "}";
HTML += "function onMessage(e){";
HTML += "console.log(e==1 ? 'On' : 'Off')";
HTML += "}";
HTML += "function onLoad(){";
HTML += "initWS()";
HTML += "}";
HTML += "window.addEventListener('load',onLoad)";
HTML += "</script>";
HTML += "</head>";
HTML += "</html>";
and this gives an error;
any suggestion on what is happening?
英文:
I have a server running on ESP32 using ESPAsyncServer And here
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send_P(200, "text/plain", String(HTML));
});
this is giving me an error saying
error: 'HTML' is not captured
i have an HTML string
String HTML PROGMEM = "<!DOCTYPE html>";
HTML += "<html>";
HTML += "<head>";
HTML += "<title> Helixis </title>";
HTML += "<script>";
HTML += "let gateway = `ws:${window.location.hostname}/ws`";
HTML += "function initWS(){";
HTML += "console.log('Connection to WebSocket ... ')";
HTML += "let ws = new WebSocket(gateway)";
HTML += "ws.onopen = onOpen";
HTML += "ws.onclose = onClose";
HTML += "ws.onmessage = onMessage";
HTML += "}";
HTML += "fucntion onOpen(){";
HTML += "console.log('Succesfully connected to the WebSocket')";
HTML += "}";
HTML += "function onClose(){";
HTML += "console.log('WebSocket connection closed')";
HTML += "}";
HTML += "function onMessage(e){";
HTML += "console.log(e==1 ? `On` : `Off`)";
HTML += "}";
HTML += "function onLoad(){";
HTML += "initWS()";
HTML += "}";
HTML += "window.addEventListner('load',onLoad)";
HTML += "</script>";
HTML += "</head>";
HTML += "</html>";
and this gives an error ;
any suggestion on what is happening?
答案1
得分: 4
This code:
[](AsyncWebServerRequest *request) {
request->send_P(200, "text/plain", String(HTML));
}
是一个"lambda表达式"。它直到调用request->send_P()
时才会被评估。
这意味着在那一点上HTML还没有被定义。为了使其在lambda中可用,必须将其"捕获",这意味着你必须明确告诉lambda表达式在[]
内使其可用。
因此,你需要重写这段代码为:
[HTML](AsyncWebServerRequest *request) {
request->send_P(200, "text/plain", String(HTML));
}
EDIT 关于你收到的新错误:
no matching function for call to 'AsyncWebServerRequest::send_P(int, const char [11], String)'
在ESPAsyncWebServer.h中有两个send_P
方法:
void send_P(int code, const String& contentType, const uint8_t * content, size_t len, AwsTemplateProcessor callback=nullptr);
void send_P(int code, const String& contentType, PGM_P content, AwsTemplateProcessor callback=nullptr);
顺便说一下,P用于发送存储在PROGMEM中的字符串,这是你的情况。
在这里,你需要使用第二个方法,使用PGM_P
来存储String
。
所需的只是将String
转换为PGM_P
。
lambda最终变成了:
[HTML](AsyncWebServerRequest *request) {
request->send_P(200, "text/plain", HTML.c_str());
}
英文:
This code :
[](AsyncWebServerRequest *request) {
request->send_P(200, "text/plain", String(HTML));
}
is a "lambda expression". It won't be evaluated until request->send_P() is called.
It means that HTML won't be defined at that point. In order to make it available for the lamba, it has to be "captured", which means that you must explicitly tell the lambda expression to make it available within []
.
As a result, you need to rewrite this code to :
[HTML](AsyncWebServerRequest *request) {
request->send_P(200, "text/plain", String(HTML));
}
EDIT concerning the new error you got :
no matching function for call to 'AsyncWebServerRequest::send_P(int, const char [11], String)'
There are 2 send_P
methods within ESPAsyncWebServer.h :
void send_P(int code, const String& contentType, const uint8_t * content, size_t len, AwsTemplateProcessor callback=nullptr);
void send_P(int code, const String& contentType, PGM_P content, AwsTemplateProcessor callback=nullptr);
Side note, P is for sending strings stored in PROGMEM, which is your case.
Here, you'll need the 2nd one using PGM_P
to store the String
.
All that's required is then to convert a String
into a PGM_P
.
PGM_P
is defined into the avr_pgmspace
namespace as const char *
. Consequently, you then need to convert your HTML String
into const char *
The lambda finally becomes :
[HTML](AsyncWebServerRequest *request) {
request->send_P(200, "text/plain", HTML.c_str()));
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论