Chat here about code rewrites, mods, etc... with respect to the github project https://github.com/esp8266/Arduino

Moderator: igrr

User avatar
By gfvalvo
#56921 Hi all, Newbie here.

I’ve been looking at lots of online examples using ESP8266WebServer. Even got a simple server running very quickly. Now I want to do something a little more advanced.

I want to serve the html from a file in the SPIFFS (I believe using the streamFile() method) rather than building it up in a large String object like a lot of the simpler examples do. I’ve studied an example of this technique already. But, in my case, one of the text elements in the web page needs to be computed dynamically before being sent (say a Temperature or a Voltage reading).

So, I’m looking for ideas of how to supply this dynamic value while still pulling the bulk of the page from SPIFFS. I imagine one way might be to send the SPIFFS file line-by-line. I’d break to insert the dynamic value at the proper point and the continue reading and sending the file (or a different file). Maybe there’s an html solution, like a tag that causes the browser to issue another request - this time just for the dynamic value to be inserted at the proper point?

Sorry for the long-winded question as well as my obvious ignorance of both ESP8266 and html coding. But, any help would be appreciated.

Thanks.

Greg
User avatar
By dannybackx
#56936 Greg,

You can write code to serve HTML pages, so you can so almost anything you want. You can cut your "static" file in two and provide the dynamic content between those pieces. In the second part of the example here (see /description.xml), a function is called to provide a reply. You can write a similar function to do just that.

Code: Select all    Serial.printf("Starting HTTP...\n");
    HTTP.on("/index.html", HTTP_GET, [](){
      HTTP.send(200, "text/plain", "Hello World!");
    });
    HTTP.on("/description.xml", HTTP_GET, [](){
      SSDP.schema(HTTP.client());
    });
    HTTP.begin();


Diving deeper into that : what this example actually does is somewhat hardcoded :
Code: Select all// Called by HTTP server when our description XML is queried
void UPnPClass::schema(WiFiClient client) {
  client.printf(_http_header);

  IPAddress ip = WiFi.localIP();
  client.printf(_upnp_device_template_1,
    ip.toString().c_str(), device->getPort(),
    device->getDeviceURN(),
    device->getFriendlyName(),
    device->getPresentationURL(),
    device->getSerialNumber(),
    device->getModelName(),
    device->getModelNumber(),
    device->getModelURL(),
    device->getManufacturer(),
    device->getManufacturerURL(),
    device->getUuid()
  );
  if (services) {
    for (int i=0; i<nservices; i++) {
      // Need to free
      char *tmp = services[i]->getServiceXML();
      client.print(tmp);
      free(tmp);
    }
  }
  client.print(_upnp_device_template_2);
}

If you replace the first and list lines of this function by code to stream a file from SPIFFS, then the code in the middle can add any dynamic content you like.

Danny