• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

SOLVED – Display a string on a web page

Q&A Forum › Category: ESP32 › SOLVED – Display a string on a web page
0 Vote Up Vote Down
Barry asked 4 years ago

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);
});

 

6 Answers
0 Vote Up Vote Down
Sara Santos Staff answered 4 years ago

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

0 Vote Up Vote Down
Steve Mercer answered 4 years ago

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.

0 Vote Up Vote Down
Barry answered 4 years ago

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

0 Vote Up Vote Down
Barry answered 4 years ago

 
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.
 
 

0 Vote Up Vote Down
Sara Santos Staff answered 4 years ago

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:

  • https://forum.arduino.cc/index.php?topic=515618.0

I hope this helps.
Regards,
Sara
 

0 Vote Up Vote Down
Barry answered 4 years ago

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.

 

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [eBook Updated] Learn Raspberry Pi Pico/Pico W with MicroPython eBook – Version 1.2 May 26, 2025
  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.