Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By Top-Dog
#39018 Hey

Is there a way to serve dynamic webpages using (or not using) the Arduino library functions?

So far I can load static content from Flash using the file system library (FS.h):
Code: Select all   
String path = "/index.html";
File file = SPIFFS.open(path, "r");
size_t sent = server.streamFile(file, contentType);
file.close();


Or serve dynamic content by creating a String variable and formatting it:
Code: Select allString html = "<p>Uptime: %02d:%02d:%02d</p>;
char buff[40];
snprintf (buff, 40, html, varHours, varMinutes, varSeconds);
server.send (200, "text/html", buff);


But is there a way of combining the two methods and having so called "dynamic" webpages, where variables can be substituted/formatted into static HTML templates stored in flash? The only way that I can think of is to somehow manually read the HTML file stored in flash into a char array or String variable and do all the string formatting/substitution before sending it off to the server as "text/html" file.
User avatar
By WereCatf
#39020
Top-Dog wrote:But is there a way of combining the two methods and having so called "dynamic" webpages, where variables can be substituted/formatted into static HTML templates stored in flash? The only way that I can think of is to somehow manually read the HTML file stored in flash into a char array or String variable and do all the string formatting/substitution before sending it off to the server as "text/html" file.


No, there isn't. At least not without installing some additional libraries. You either have to write your own interpreter, or if you know before-hand the specific number of these dynamic variables and their types you could use snprintf -- read file to memory, allocate a buffer to hold the output, feed the file and the variables to snprintf and then send the output to your client. I use this method myself for printing uptime and other statistics on a website dynamically, but as I said, you can't change the number or type of the parameters when using snprintf.

You could probably find some interpreter-library already, I'm sure someone has done such, but it's going to be a lot slower than snprintf.
User avatar
By Top-Dog
#39022
use snprintf -- read file to memory, allocate a buffer to hold the output, feed the file and the variables to snprintf and then send the output to your client.


I think at this stage snprintf suits my requirements, hence why I was already contemplating this method, but is there a smart way to implement it, or do I just go around creating fixed length buffers that I know work for each of my HTML pages?

The esphttpd project seems to have a pretty nice URL/function/argument handling system, but it looks like it would require modifying the Arduino ESP8266WebServer library.
User avatar
By WereCatf
#39024
Top-Dog wrote:I think at this stage snprintf suits my requirements, hence why I was already contemplating this method, but is there a smart way to implement it, or do I just go around creating fixed length buffers that I know work for each of my HTML pages?


Well, yes.

The esphttpd project seems to have a pretty nice URL/function/argument handling system, but it looks like it would require modifying the Arduino ESP8266WebServer library.


I haven't looked at esphttpd, I'm not even sure what you mean, but if you're talking about passing arguments to an URL and having the function do something with those arguments then that's perfectly doable with just the ESP8266WebServer.