2

Is there any macro for importing the content of a file (for example html code) located in the sketch folder as a string for the Arduino IDE during compilation time?

3 Answers 3

3

For ESP32 (and ESP8266), you can partition part of the Flash memory to be used as the file system and creating a data/ folder in your project for storing files. Take a look at arduino-esp32littlefs-plugin (used for uploading the data to the partition) on how to install the plugin in Arduino IDE, and the LittleFS library or SPIFFS on how to use it to store and retrieve data.

I have a project on my github showing how the SPIFFS is used for storing webpages (html, JavaScript, CSS, icon, etc.) and serve it via an Async ESP32 Server. Following the link on the github for more detail explanation on the blog.

2
  • This is a very nice little project: modern and straightforward approach, the code is short and simple, very clear explanations in your blog… It could make a good “reference example” of sorts for people aiming to build a Web server for a hardware project. Commented Dec 17, 2023 at 14:54
  • Thank you @EdgarBonet for your compliments.
    – hcheung
    Commented Dec 17, 2023 at 23:43
2

Try using the C++ Raw String Literal.

It's in the form of

const char *myLiteralText=R"abc123~~~(
   this is
   literal text
   which includes all spaces
   and newlines
)abc123~~~"

The abc123~~~ is a delimiter.

The test.h file must be modified by adding the first and last lines.

Actually, the last line can be in the test.ino file after the #include line, so only the first line has to be added to the test.h file.

Example at https://wokwi.com/projects/384484092370522113

test.ino

void setup() {

  Serial.begin(9600);

  const char *index_html=
    #include "test.h"
  ;
    
  Serial.print(index_html);

}

void loop() {}

test.h

R"html(

<!DOCTYPE html>
<html lang="en">
  <head>
      <meta charset="UTF-8">
      <title>VIPSDK MONITOR</title>
      <meta http-equiv="refresh" content="10">
  </head>
  <style type="text/css">
  </style>
</html>

)html"
0

In short, no. That I'm aware of the IDE, languages, and build system don't provide anything to do that automatically. It would be nice if C++ had something like Rust's include_str or include_bytes but so far as I know it still doesn't.

Your question is tagged with esp32. You may want to put strings on on your data partition if they're large.

There are other ways to do it, but the usual way to build arbitrary data into a C++ or C program is to run a utility that converts it into source code. Either into a string literal or an initialized array of some character type. The xxd utility that usually comes with VIM is one such utility. It's normally used to generate hex dumps of files, but can be told to produce source code of an initialized array. Utilities like that are often run in a pre-build stage of a more conventional build process for C++. You could probably hack something into platform.txt that would do it. Or into something like a git hook. But the IDE doesn't have that sort of flexibility.

Here's a webpage that does the same sort of thing. You may have to manually null terminate the array if you want to do this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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