ESP32: How to use a file instead of String for html code

Hello, I am using an esp32 as a webserver. I have stored the html code in a string in main.cpp and that has worked but now i want to store the html code in a separate file and store in the spiffs file system.

I created a directory called "data" and stored my index.html file there. In my main.cpp file i create the spiffs file system and also have a partition in flash called "spiffs" I am not having any luck getting this to work.

The error I am getting is: "src/main.cpp: In lambda function:
src/main.cpp:66:41: error: request for member 'html' in 'index', which is of non-class type 'char*(const char*, int)'
server.send(200, "text/html", index.html);

Looking at the server.send function prototypes they all seem to be looking for a string type or char pointer and not a file so i don't know if I can even do this.

Here is the code

#include <Arduino.h>
#include <WiFi.h>
#include <WebServer.h> 
#include <FS.h>
#include <SPIFFS.h>

// SSID and password of Wifi connection:
const char *ssid = "SSID";
const char *password = "PASSWORD";

// String webpage = "<!DOCTYPE html>..."    html stored in String

// Initialization of webserver and websocket
WebServer server(80); // the server uses port 80 (standard port for websites

void setup()
{
  Serial.begin(115200); // init serial port for debugging

  WiFi.begin("SSID", "PASSWORD"); // start WiFi interface

  while (WiFi.status() != WL_CONNECTED)
  { // wait until WiFi is connected
    delay(1000);
    Serial.print(".");
  }
  Serial.print("Connected to network with IP address: ");
  Serial.println(WiFi.localIP()); // show IP address that the ESP32 has received from router

 
  if (!SPIFFS.begin(true))
  {
    Serial.println("An Error has occurred while mounting SPIFFS");
    return;
  }

  /*
   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(int code, const char* content_type, const char* content);
  */

  server.on("/", []() { // define here wat the webserver needs to do
     server.send(200, "text/html", index.html); //    -> it needs to send out the HTML string "webpage" to the client, // server.send(200, "text/html", webpage); //    -> this works
  });
  server.begin(); // start server
}

void loop()
{
  server.handleClient(); // Needed for the webserver to handle all clients
}

it wants a char buffer..
you have to read your file into a buffer and send the buffer..

SPIFFS Test

Ok, thanks. That seems easy enough. Will try tomorrow.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.