1

I have an esp32 which is running a web page together with a java script and using socket I update some values on the web page. I am doing this very fast (like each 50ms). Now I am serving all the html/java script code stored in a string variable but I am wondering which is the best? To store this html in spiff or in code ? and why? Thank you very much

3
  • SRAM is typically an order of magnitude faster than NVRAM. – dandavis Jun 14 '19 at 16:17
  • why do you need to update a web page every 50 ms? – jsotola Jun 14 '19 at 20:15
  • I am displaying rpm of an engine which is read through obd port and I want see it as "real time" as possible – lucian_v Jun 18 '19 at 9:13
1

Best is to store static files in SPIFFS and serve them with Expires header set, to let the browser cache the html, css, js files and pictures files.

How to build the Expires header:

unsigned long expires = now() + SECS_PER_YEAR;
bp.printf(F("Expires: %s, "), dayShortStr(weekday(expires))); // two printfs because ShortStr functions share the buffer
bp.printf(F("%d %s %d 00:00:00 GMT"), day(expires), monthShortStr(month(expires)), year(expires));

And use AJAX method to show data on the page. Make the request with XMLHttpRequest object:

function getData(cmd) {
  var xhr = new XMLHttpRequest();
  xhr.onerror = function(e) {
    alert(xhr.status + " " + xhr.statusText);
  }
  xhr.onload = function(e) {
    showValues(xhr.responseText);
  };
  xhr.open("GET", "http://" + host + ":" + restPort + "/" + cmd, true);
  xhr.send();
}

showValues would be your function to fill the data into page

In ESP32 build the response as JSON for simple handling of the data in JavaScript.

Here is the WebServer in my project. It uses the basic Arduino Server class, not the esp WebServer. It has #ifdefs for SD or SPIFFS. The static files of my project including the js file are here.

0

It depends on your definition of "better".

Directly inlined in your code will probably be faster - it will be subject to the caching mechanisms of the execution pipeline, whereas SPIFFS will have to make specific SPI calls to read the data from the flash chip at the time it's required. However SPIFFS will be easier to maintain since you are just working with files.

I would probably choose SPIFFS for larger files like graphics or big chunks of Javascript. Things that the client can download once and then cache (as long as your web server supports that kind of functionality - not sure what facilities the default ESP32 web server code has). The rest, the more dynamic things that change often, have as inline string literals.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.