ESSP32 Webserver - use of strings ?

My ESP32 project was going along nicely. Collecting data from a sensor and filing it to an SD card until, that was, I decided to add a webserver.

The examples I found used Strings but I decided to use strings so wrote this function

void publishRoot()
  char webBuffer[500];
  String stringBuffer;
  snprintf(webBuffer, 499,
      table, th, td {\
      border: 1px solid black;\
      border-collapse: collapse;\
  <meta http-equiv='refresh' content='5'/>\
  <title>ESP32 Web Server</title>\
    <h1>From ESP32</h1>\
           data.count, data.temperature, data.humidity);
  stringBuffer = webBuffer;
  webServer.send(200, "text/html", stringBuffer);
  Serial.print("size of webBuffer string : ");

Most of the time it works but there is the occasional crash, panic and reboot which I put down to memory corruption but I could not find anything significant so I tried this

void publishRoot()
  String webBuffer;
  String count = data.count;
  String temperature = data.temperature;
  String humidity = data.humidity;
  webBuffer = "<html>";
  webBuffer += "<head>";
  webBuffer += "<style>";
  webBuffer += "table, th, td {";
  webBuffer += "border: 1px solid black;";
  webBuffer += "border-collapse: collapse;";
  webBuffer += "</style>";
  webBuffer += "<meta http-equiv='refresh' content='5'/>";
  webBuffer += "<title>ESP32 Web Server</title>";
  webBuffer += "</head>";
  webBuffer += "<body>";
  webBuffer += "<h1>From ESP32</h1>";
  webBuffer += "<table>";
  webBuffer += "<tr><td>Count</td><td>" + count + "</td></tr>";
  webBuffer += "<tr><td>Temperature</td><td>" + temperature + "</td></tr>";
  webBuffer += "<tr><td>Humidity</td><td>" + humidity + "</td></tr>";
  webBuffer += "</table>";
  webBuffer += "</body>";
  webBuffer += "</html>";
  webServer.send(200, "text/html", webBuffer);
  //  Serial.println(webBuffer);

No crashes. All sweetness and light but I would still rather use strings rather than Strings

The specific webserver library used is as follows

Multiple libraries were found for “WebServer.h”
Used: C:\Users\Bob\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4\libraries\WebServer

and the prototype for the send() function(s) is

  // send response to the client
  // code - HTTP response code, can be 200 or 404
  // content_type - HTTP content type, like "text/plain" or "image/png"
  // content - actual content body
  void send(int code, const char* content_type = NULL, const String& content = String(""));
  void send(int code, char* content_type, const String& content);
  void send(int code, const String& content_type, const String& content);
  void send_P(int code, PGM_P content_type, PGM_P content);
  void send_P(int code, PGM_P content_type, PGM_P content, size_t contentLength);

so, Strings it is and I don’t fancy wrestling with the library to create another version of the send() function using strings and maintaining it afterwards

This is not the end of the World as the ESP32 has plenty of memory, but I would still rather use strings. Does anyone know of an alternative webserver library for the ESP32 that allows the use of strings rather than Strings ?

Not sure if local Strings & char arrays defined in a functions are allocated on the stack or heap memory but your first version is allocating 500 bytes somewhere to build the HTML page that ultimately has to be copied into a String for the webServer.send
I don't think the ESP cores have problems with Strings like the Arduino ATmega cores (used to?) have.
I have ESP's using Strings that run for months without issue and ESP.getFreeHeap(); shows the memory is not fragmenting/leaking.

data.count is a string?
I ask because of %s in the format string

the neater and best approach is to have html/css/JS into a progmem like this

const char home_page[] PROGMEM = R"=====(

<title> ESP32 PAGE </title>






// your JS goes here



    )=====";    end of array, no escape characters needed in the array

and then you invoke something like this to render the page

server.send_P(200, "text/html", home_page, sizeof(home_page) );

you should be saying good bye to stack error issues,

and to pass your variables, using Ajax ( for dynamic page refresh ) is the best idea

data.count is a string?
I ask because of %s in the format string

Yes, it is string (lowercase), but you are right to ask

All 3 data items, data.count, data.temperature and data.humidity are strings. They are parsed from the payload of an MQTT message using strtok()

As you can probably guess from the functions that I posted I have printed the contents of the HTML page to make sure that it contains the expected data, which it does

the neater and best approach is to have html/css/JS into a progmem like this

Thanks for the suggestion. At the moment I am trying to keep the HTML side of things as simple as possible but I will bear it in mind

the neater and best approach is to have html/css/JS into a progmem like this

The neatest and best approach is to put the HTML, CSS and JS into files in the SPIFFS filesystem, and use AJAX or similar to fetch the data.

Ray L.