Using the new Arduino IDE for ESP8266 and found bugs, report them here

Moderator: igrr

User avatar
By gecko
#60072 I'm creating a website on a Wemos D1 mini with a few fairly large pages (~10k). However, I cannot make page transitions with AJAX calls. I know the call is getting in because I've embedded a 2 sec light blink into the called function. But the line "server.send_P(200, "text/html", page2_html) is being ignored. The following is a stripped down version of my Arduino code using HelloServer as framework:

Code: Select all#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.
User avatar
By martinayotte
#60088 You are doing an AJAX call to get the page2, it is the request object that will get the page2 html code response, not your main page browser.
You don't need to use any AJAX code to switch pages, simply define your button as the following :
Code: Select all<button type="button" onClick="window.location.replace('/page2_html');" value="PRESS ME!" />
User avatar
By gecko
#60127
martinayotte wrote:You don't need to use any AJAX code to switch pages


What a marvelous idea! Though I'm still using AJAX calls for other aspects of my web page, your suggestion for page switching works fine. I guess it's time for me to learn more HTML5. :) Thanks.

(Still puzzled by that problem with the page2_html constant that would only work if modified by blocks of 4 chars. I thought surely it must be a bug. Not a big deal for me though.)