Chat freely about anything...

User avatar
By Sirquil
#84166 Following: "SOLVED WEBSERVER CAN'T SEND LARGE WEBPAGE TO CLIENT;" does not compile with Async Web Server.

Doing as NickLD posted:
This is my Favorite method of serving any webpage content from an ESP that's more complex than a few lines:

1. Create a new file in the Arduino IDE named index.h
2. Put this inside it:
CODE: SELECT ALL
const char MAIN_page[] PROGMEM = R"=====(
/// Your entire webpage here. (Can be multi-line)
)=====";

3. Make sure you save it, and import it in your main sketch file:
CODE: SELECT ALL
#import "index.h"

4. Now to serve the web-page, simply do this:
CODE: SELECT ALL
httpServer.send(200, "text/html", MAIN_page);


Plus adding Pablo2048 small correction:
Yes, exactly, what NickLD wrote with small correction:
replace httpServer.send(200, "text/html", MAIN_page); with httpServer.send_P(200, "text/html", MAIN_page, sizeof(MAIN_page));
there are _P methods for direct PROGMEM output (see ESP8266WebServer.h)


My "index.h" file:

Code: Select all//index.h

     const char MAIN_page[] PROGMEM = R"=====(
        
      getWeatherData();

      if(!isnan(dew))
      {

         gpslat = gps.location.lat();
         gpslng = gps.location.lng();
         gpsalt = gps.altitude.feet();

         // First send the success response code.
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: html");
          client.println("Connnection: close");
          client.println("Server: Robotdyn WiFi D1 R2");
         // Send an empty line to signal start of body.
          client.println("");
         // Now send the response data.
         // output dynamic webpage
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
          client.println("<head><title>Weather Observations</title>");
          client.println("<body>");
         // add a meta refresh tag, so the browser pulls again every 15 seconds:
         // client.println("<meta http-equiv=\"refresh\" content=\"15\">");
          client.println("<h2>Rain Gauge Two<br>");
          client.println("Indianapolis, Indiana</h2></head><br>");

         if(lastUpdate != NULL)
         {
             client.println("Last Update:  ");
             client.println(lastUpdate);
             client.println("   <br>");
         }

          client.println("Latitude : " );
          client.print(gpslat,5);
          client.print(" <br>");
          client.println("Longitude : " );
          client.print(gpslng,5);
          client.print(" <br>");
          client.println("Elevation");
          client.print(gpsalt,0);
          client.print(" Feet.<br>");
          client.println("Temperature:  ");
          client.print(temp, 1);
          client.print(" C.<br>");
          client.println("Humidity:  ");
          client.print(hum, 1);
          client.print(" %<br>");
         // client.println("Dew point:  ");
         // client.print(DewPoint, 1);
         // client.print(" F. <br>");
         // client.println("Heat Index:  ");
         // client.print(HeatIndex, 1);
         // client.print(" F. <br>");
          client.println("Barometric Pressure:  ");
          client.print(pressure, 1);   //Inches of Mercury
          client.print(" hpa.<br>");
          client.println("Rain Day :  ");
          client.print(rainDay, 2);   //Inches of Mercury
          client.print(" Day/mm <br>");
          client.println("Rain Hour:  ");
          client.println(rainHour, 2);   //Convert hPA to millbars
          client.println(" Hour/mm <br>");
          client.println("Rain5min :  ");
          client.print(rainFall, 2);
          client.print(" Five min/mm <br>");
         // client.println("Elevation:  824 Feet<br>");
          client.println("<h2>Weather Observations</h2>");
          client.println("<h3>" + dtStamp + "    </h3><br>");
          client.println("<br>");
          client.println("<a href=http://" + publicIP + ":" + LISTEN_PORT + "/SdBrowse >File Browser</a><br>");
          client.println("<br>");
          client.println("<a href=http://" + publicIP + ":" +  LISTEN_PORT + "/Graphs >Graphed Weather Observations</a><br>");
          client.println("<br>");
          client.println("<a href=http://" + publicIP + ":" +  LISTEN_PORT + "/README.TXT download>Server:  README</a><br>");
          client.println("<br>");
          client.println("<a href=https://forum.arduino.cc/index.php?topic=606947.0>Project Discussion</a><br>");
          client.println("<br>");
         //Show IP Adress on client screen
          client.print("Client IP: ");
          client.print( remoteIP().toString().c_str());
          client.println("</body>");
          client.println("</html>");
         
      }
      
   )=====";


I have #import "index.h" in main part of the code.

Asyncwebserver object:
Code: Select allAsyncWebServer serverAsync(8080);


Attempt to send:
Code: Select all      serverAsync.on("/Weather", HTTP_GET, [](AsyncWebServerRequest * request) {
     request->send_P(200, "text/html", MAIN_page, sizeof(MAIN_page));
     });


Compiling produces error:
invalid conversion from 'const char*' to 'const uint8_t* {aka const unsigned char*}' [-fpermissive]


How can multiple line HTML be sent using Async Web Server?

William
User avatar
By Sirquil
#84173 Thank you Pablo2048.

Code: Select all     serverAsync.on("/Weather", HTTP_GET, [](AsyncWebServerRequest * request) {
     request->send_P(200, PSTR("text/html"), MAIN_page, sizeof(MAIN_page));
     });


Produces compiler error:

invalid conversion from 'const char*' to 'const uint8_t* {aka const unsigned char*}' [-fpermissive]


Changing:

Code: Select allconst char MAIN_page[] PROGMEM = R"=====(
/// Your entire webpage here. (Can be multi-line)
)=====";


To:

Code: Select allconst uint8_t MAIN_page[] PROGMEM = R"=====(
/// Your entire webpage here. (Can be multi-line)
)=====";
Compiles;however MAIN_page, loads none of the page.

Do I need to recode "index.h"?

William