-->
Page 1 of 1

How to use PROGMEM to store and serve web pages

PostPosted: Fri Aug 21, 2015 4:39 pm
by Silux
There is a way to store a web page in an arduino program and recall it without making the ESP8266 to crash?
Code: Select allconst char PAGE_NetworkConfiguration[] PROGMEM = R"=====(
 <!DOCTYPE html>
<html>
<head>
 
</head>
<body>
  hello!
</body>

</html>
)=====";


served with
Code: Select allelse if (req.indexOf("/servepage") != -1){
      for(int i = 0; i < 10;i++){
        client.println(PAGE_NetworkConfiguration[i]);   
      }
    }


when i call "/servepage" the ESP resets with
Code: Select all ets Jan  8 2013,rst cause:4, boot mode:(1,6)

wdt reset

Which if i remember correctly is due to the watchdog timer.

Re: How to use PROGMEM to store and serve web pages

PostPosted: Fri Aug 21, 2015 5:19 pm
by martinayotte
If you were using ESP8266WebServer example, this functionnality has been added 3-4 weeks ago, with the new send_P() and sendContent_P() functions.

viewtopic.php?f=29&t=4060&p=24478&hilit=PROGMEM+send_P#p24478
and the commit is :
https://github.com/esp8266/Arduino/comm ... 03280c5c76

If you are using plain WifiClient like you seems to, this functionnality is not present yet.
You will then have to create your own inspired by the commit above, using memccpy_P in loop for sending chunks of the PROGMEM until it reach the end.

EDIT : Oh ! I didn't see that earlier : you are sending "client.println(PAGE_NetworkConfiguration[i]);", why the "[i]" is there ? it not array of web page, so you are providing wrong string pointer by proving a character value as a pointer to string... :o
But that doesn't make my previous comment irrelevant, I will work to have new write_P() added in WifiClient ...

EDIT2 : I've done the work for new WifiClient::write_P(), it is now commit, waiting for approved PR ...

Re: How to use PROGMEM to store and serve web pages

PostPosted: Sun Aug 23, 2015 1:36 am
by roboticboyer
You can try with this sketch
Code: Select all#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>

//Include the HTML, STYLE and Script "Pages"
#include "page_01.h"

// Create an instance of the server specify the port to listen on as an argument
ESP8266WebServer server(80);


//-----------------------------------------------------------------------
void handleWebsite(){
  //upload page_00.h
  server.send(200,"text/html",PAGE_NetworkConfiguration);
}

//-----------------------------------------------------------------------
void handleNotFound() {
    String message = "File Not Found\n\n";
    message += "URI: ";
    message += server.uri();
    message += "\nMethod: ";
    message += ( server.method() == HTTP_GET ) ? "GET" : "POST";
    message += "\nArguments: ";
    message += server.args();
    message += "\n";
    for ( uint8_t i = 0; i < server.args(); i++ ) {
        message += " " + server.argName ( i ) + ": " + server.arg ( i ) + "\n";
    }
    server.send ( 404, "text/plain", message );
}
//-----------------------------------------------------------------------
void setup(){
 
  // Start the server
   server.on("/",handleWebsite);
   server.onNotFound ( handleNotFound );

}
//-----------------------------------------------------------------------
void loop(){
   server.handleClient();
}


Put your html page inside the page01.h file
Code: Select allconst char PAGE_NetworkConfiguration[] PROGMEM = R"=====(
 <!DOCTYPE html>
<html>
<head>
 
</head>
<body>
  hello!
</body>

</html>
)=====";


Have fun ;) ;) ;)
Gio