ESP32, I have some strings stored in SPIFFS files. I’ve read the file and now have a string (say abc1) (plain text) that I want to display as content in a table.
…
<tr> <td>1</td>
<td> I want the value in abc1 to appear here </td>
I’ve just read https://rntlab.com/question/inject-an-arduino-variable-into-html-web-server-page/
and now am wondering about the value of 200 in send_P. I see it used a few lines prior, but have no context of the value 200. Is this a string max length or something else?
Can I just substitute abc1 for lastPhoto.c_str() ?
Why is this a function call rather than a variable?
From referenced post:
server.on("/demo", HTTP_GET, [](AsyncWebServerRequest *request){ request->send_P(200, "text/plain", lastPhoto.c_str()); });
server.on("/abcstring", HTTP_GET, [](AsyncWebServerRequest *request){ request->send_P(200, "text/plain", abc1); });
Hi.
What’s the name of your variable?
Imagine your variable is a String called abc1.
String abc1 = "some random text";
The send_P() function accepts variables of type char. That’s why we need to use the c_str() method.
So, you would send the string as follows:
server.on("/abcstring", HTTP_GET, [](AsyncWebServerRequest *request){ request->send_P(200, "text/plain", abc1.c_str()); });
Let me know if this helps.
Regards,
Sara
Just for reference 200 is the HTTP error code. 200 means everything went great and what follows is the result of your request. There are many other codes that you ca google.
Steve – Thank you.
Sara
I’m an old guy that doesn’t know C++ and was hoping there was a C answer to this.Been doing embedded work for ages.
Until the last few years when memory and speed became essentially free, using minimal lib and simple functions was important. C++ was discouraged in some businesses just because of bloat.
Is there no C equivalent of abc1.c_str() ?
I have done:
char abc1[20];
Are these statements equivalent?
abc1.c_str()
&abc1
char label[7][20];
…
void onlabelRequest(AsyncWebServerRequest *request) {
char* abc;
abc = &label[1][0];
IPAddress remote_ip = request->client()->remoteIP();
request->send_P(200, “text/plain”, abc.c_str() );
}
Generates this error msg:
request for member ‘c_str’ in ‘abc’, which is of non-class type ‘char*’
This is a bit of a work around, but I already have my SPIFFS read and parsing working to create strings in label[7][20]. Would prefer not to change that.
Hi Barry.
I think my explanation was not correct.
These articles better explain the meaning of c_str():
- https://www.arduino.cc/reference/en/language/variables/data-types/string/functions/c_str/
- https://www.geeksforgeeks.org/basic_string-c_str-function-in-c-stl/
This discussion might also be useful:
I hope this helps.
Regards,
Sara
This works in the .html page:
<object type=”text/html” data=”/filename.html”></object>
The file contains: <h3>Desired text</h3>NULL
I’m not 100% sure, but I think the .html extension is better than .txt as it is interpreted as html content. Using the <h3> is good for the font I wanted.
This is MUCH MUCH less complicated than other things I found in web searches. I hope others will benefit from this resolution.
When the user enters the text on a separate page, I’ll write it to the file with SPIFFS and include the <h3> and the .html extension.