#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
const char* ssid = "myWIFI";
const char* password = "myPASSWORD";
ESP8266WebServer server(80);
const char index_html[] PROGMEM = R"~~(<!DOCTYPE html>
<html><head><meta charset="utf-8">
</head><body><button type="button" id="page2">"PRESS ME!"</button>
<script type="text/javascript">
document.getElementById("page2").addEventListener("click", function(){request = new XMLHttpRequest();
request.open("GET", "page2_html", true); request.send(null);});
</script></body></html>)~~";
const char page2_html[] PROGMEM = R"~~(<!DOCTYPE html>
<html><head><meta charset="utf-8">
</head><body>"Hello from ESP8266!"</body></html>)~~";
void handleRoot() {
digitalWrite(LED_BUILTIN, LOW);
server.send_P(200, "text/html", index_html);
digitalWrite(LED_BUILTIN, HIGH);
}
void page2() {
server.send_P(200, "text/html", page2_html);
digitalWrite(LED_BUILTIN, LOW);
delay(2000);
digitalWrite(LED_BUILTIN, HIGH);
}
void setup(void){
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.println("");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
if (MDNS.begin("esp8266")) {
Serial.println("MDNS responder started");
}
server.on("/", handleRoot);
server.on("/page2_html", page2);
server.begin();
Serial.println("HTTP server started");
digitalWrite(LED_BUILTIN, HIGH);
}
void loop(void){
server.handleClient();
}
index_html loads properly but pressing the button does not take me to page2_html. The light turns on for 2 secs as instructed. If I manually add in "/page2_html" on the browser address line then page2_html is displayed properly. Is there anything I'm missing?
Another screwy thing about this code is that the PROGMEM constant, page2_html[], can only be modified in chunks of 4 chars. If I add or delete any number of chars that is a non-multiple of 4, then the Wemos throws an Exception (3) as soon as I access the root page from the browser. The root page itself, PROGMEM constant index_html[], I can modify without any problems at all. What's going on?
BTW, I'm using Arduino IDE 1.6.12 and ESP8266 v2.3.0. Any help with this would be much appreciated.