Ethernet Webserver How to use separate HTML file instead of inline

So i am planning on making a webserver, as i create a mental map of what im going to do im faced with a very laborious issue.

In the Ethernet Webserver example the HTML that is being returned to the one requesting it is made inline with the code.

    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connection: close");  // the connection will be closed after completion of the response
          client.println("Refresh: 5");  // refresh the page automatically every 5 sec
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
          // output the value of each analog input pin
          for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
            int sensorReading = analogRead(analogChannel);
            client.print("analog input ");
            client.print(analogChannel);
            client.print(" is ");
            client.print(sensorReading);
            client.println("<br />");
          }
          client.println("</html>");
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        } else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }

this is a very laborious problem, because if my html webpage has a few hundred lines of code, the code will get very very messy.

Is there some way that i can just include a .html file and it will automagically return that instead?

Storing it on the SD card? There are numerous examples on the Net for that.

As you failed to tell us what exact hardware you're using we cannot go into more detail.

Another way would be to develop the HTML file externally and convert it by a script into properly formatted source code.

Im using a Raspberry pico Rp2040 Board. Im planning on storing it in flash Actually Since the pico has a lot of flash space compared to other boards(2Mb).

I was actually thinking of doing this incase there was no other way of doing it. I would include some sort of .h file that is generated by the script.

its kind of a shame the library do not have inbuilt support for it.

its kind of shame that you don't know about
"PROGMEM raw string literal"

google it, learn it, and you can put your html file in a .h file

1 Like

I'm not an expert for the RP2040 board but if the corresponding core doesn't support a flash file system (p.e. the SPIFFS on the ESP32) the library cannot provide such a functionality. The Arduino-Pico core supports the LittleFS for such purposes but don't expect any example of a general purpose library as the Ethernet library to support stuff available on a single platform only.

BTW, how do you want to use the Ethernet library on that board? Did you attach a WizNet board to it?

PROGMEM are basically non existent on arm architectures :stuck_out_tongue: . The compilers are smart enough that const or static const will do the same thing.

I guess you should google it and learn more about it :slight_smile:

Yes , a W5500 board is connected. I already have done some test and have already made a very simple webserver ( just 10 lines of html).

This seems a bit more trouble than i want to deal with. There might also be some overhead processing when fetching the html file. I guess the option for me is to go with the script route. But first i have to look into how to read url parameters, there might be weird interactions if i use one or the other

You can try my EthernetWebServer library, which supports RP2040 + W5500 using either ArduinoCore-mbed or arduino-pico core.

Check this AdvancedWebServer example and you can either leave the html inside the .ino or split the html code to a separate .h file

LittleFS can also be used and you just load the html to a variable at the beginning, then use it as long as you'd like.

Check the similar examples written for ESP32 and ESP8266 and LittleFS

  1. FS_EthernetWebServer
  2. ESP32_FS_EthernetWebServer

Here is the LittleFS_Mbed_RP2040 library for mbed core and LittleFS for arduino-pico core

Which would be better use littleFS or just a separate .h file performance wise?

A separate .h file will certainly give you the best performance along with much simpler code.

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