I'm working through a tutorial about using esp8266 connected to an Arduino Uno to serve a webpage with Ajax that retrieves a json file (also served by the Arduino). The tutorial (won't link to it here) looks like it's a work of fiction because the author builds the webpage using Strings like this:
String webpage = "<!DOCTYPE html><html><head><meta name=\"viewport\" content=\"width=device-width, minimumscale=1.0, maximum-scale=1.0, initial-scale=1\" />";
webpage += "<style>body { background-color: #cccccc; text-align: center; max-width: 400px; margin: 10px auto; } #datavalues { max-width: 400px; display: block; margin-top: 30px; }</style>";
webpage += "</head><body>";
webpage += "<div id=\"datavalues\">";
webpage += "<h1>Light: </h1><div id=\"light\">";
webpage += lightval;
webpage += "</div>";
webpage += "<h1>Count: </h1><div id=\"count\">";
webpage += count;
webpage += "</div>";
webpage += "</div>";
webpage += "<script>function loadDoc() { var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { var obj = JSON.parse(this.responseText); document.getElementById(\"light\").innerHTML = obj.data[0].datavalue; document.getElementById(\"count\").innerHTML = obj.data[1].datavalue; } }; xhttp.open(\"GET\", \"data.json\", true); xhttp.send(); } var timedEvent = setInterval(function(){ loadDoc(); }, 2000);</script>";
webpage += "</body></html>";
and when you test it it looks like the webpage is either too long for a String or the uno runs out of memory. I've been trying with c type strings (reading that they are more efficent) like so:
char webpage[1024] = "<!DOCTYPE html><html><head><meta name=\"viewport\" content=\"width=device-width, minimumscale=1.0, maximum-scale=1.0, initial-scale=1\" />";
strcat(webpage, "<style>body { background-color: #cccccc; text-align: center; max-width: 400px; margin: 10px auto; } #datavalues { max-width: 400px; display: block; margin-top: 30px; }</style>");
but it doesn't seem to make much difference. Is there a way to serve a webpage this size from the arduino? / what is the most efficient way to build it and serve it?