‘HTML’ 没有捕获到 ESP32

huangapple go评论63阅读模式
英文:

'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(&quot;/&quot;, HTTP_GET, [](AsyncWebServerRequest *request) {
    request-&gt;send_P(200, &quot;text/plain&quot;, String(HTML));
  });

this is giving me an error saying

error: &#39;HTML&#39; is not captured

i have an HTML string

String HTML PROGMEM = &quot;&lt;!DOCTYPE html&gt;&quot;;
  HTML += &quot;&lt;html&gt;&quot;;
  HTML += &quot;&lt;head&gt;&quot;;
  HTML += &quot;&lt;title&gt; Helixis &lt;/title&gt;&quot;;
  HTML += &quot;&lt;script&gt;&quot;;
  HTML += &quot;let gateway = `ws:${window.location.hostname}/ws`&quot;;
  HTML += &quot;function initWS(){&quot;;
  HTML += &quot;console.log(&#39;Connection to WebSocket ... &#39;)&quot;;
  HTML += &quot;let ws = new WebSocket(gateway)&quot;;
  HTML += &quot;ws.onopen = onOpen&quot;;
  HTML += &quot;ws.onclose = onClose&quot;;
  HTML += &quot;ws.onmessage = onMessage&quot;;
  HTML += &quot;}&quot;;
  HTML += &quot;fucntion onOpen(){&quot;;
  HTML += &quot;console.log(&#39;Succesfully connected to the WebSocket&#39;)&quot;;
  HTML += &quot;}&quot;;
  HTML += &quot;function onClose(){&quot;;
  HTML += &quot;console.log(&#39;WebSocket connection closed&#39;)&quot;;
  HTML += &quot;}&quot;;
  HTML += &quot;function onMessage(e){&quot;;
  HTML += &quot;console.log(e==1 ? `On` : `Off`)&quot;;
  HTML += &quot;}&quot;;
  HTML += &quot;function onLoad(){&quot;;
  HTML += &quot;initWS()&quot;;
  HTML += &quot;}&quot;;
  HTML += &quot;window.addEventListner(&#39;load&#39;,onLoad)&quot;;
  HTML += &quot;&lt;/script&gt;&quot;;
  HTML += &quot;&lt;/head&gt;&quot;;
  HTML += &quot;&lt;/html&gt;&quot;;

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-&gt;send_P(200, &quot;text/plain&quot;, 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-&gt;send_P(200, &quot;text/plain&quot;, String(HTML));
  }

EDIT concerning the new error you got :
no matching function for call to &#39;AsyncWebServerRequest::send_P(int, const char [11], String)&#39;

There are 2 send_P methods within ESPAsyncWebServer.h :

void send_P(int code, const String&amp; contentType, const uint8_t * content, size_t len, AwsTemplateProcessor callback=nullptr);
void send_P(int code, const String&amp; 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-&gt;send_P(200, &quot;text/plain&quot;, HTML.c_str()));
  }

huangapple
  • 本文由 发表于 2023年5月14日 18:59:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76247113.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定