• 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

Display a string on a web page

Q&A Forum › Category: ESP32 › Display a string on a web page
0 Vote Up Vote Down
Barry asked 1 month 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);
});

 

5 Answers
0 Vote Up Vote Down
Sara Santos Staff answered 1 month 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 1 month 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 1 month 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 1 month 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 1 month 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
 

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] Download Build Web Servers with ESP32 and ESP8266 (version 2.1) April 14, 2021
  • [eBook Updated] Download Build Web Servers with ESP32 and ESP8266 (2nd Edition) February 8, 2021

You must be logged in to view this content.

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

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