I am trying to get a dynamic website from my ESP8266 server. It should update the runtime of the ESP every second. I have prepared some code that doesn't work ... yet. It hangs in the javascript part where it tests for 'readyState==4' and 'status==200'. This test fails and it triggers an alert.
The ESP does serve xml-data. The ulr 'http://192.168.xxx.yyy/xml' returns a proper xml-formatted page.
How do I get the ESP8266 to deliver the proper readyState and status?
Thanks, Peter.
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
ESP8266WebServer server(80);
const char* ssid="SSID";
const char* password="PASSWORD";
String webSite,javaScript,XML;
void buildWebsite(){
buildJavascript();
webSite="<!DOCTYPE HTML>\n";
webSite+=javaScript;
webSite+="<BODY onload='process()'>\n";
webSite+="<BR>This is the ESP website ...<BR>\n";
webSite+="Runtime = <A id='runtime'>00:00:00</A>\n";
webSite+="</BODY></HTML>\n";
}
void buildJavascript(){
javaScript="<SCRIPT>\n";
javaScript+="var xmlHttp=null;\n";
javaScript+="if(window.XMLHttpRequest){\n";
javaScript+=" xmlHttp=new XMLHttpRequest();\n";
javaScript+="}else{\n";
javaScript+=" xmlHttp=new ActiveXObject('Microsoft.XMLHTTP');\n";
javaScript+="}\n";
javaScript+="function process(){\n";
javaScript+=" if(xmlHttp.readyState==0 || xmlHttp.readyState==4){\n";
javaScript+=" xmlHttp.open('GET','/xml',true);\n";
javaScript+=" xmlHttp.onreadystatechange=handleServerResponse();\n";
javaScript+=" xmlHttp.send(null);\n";
javaScript+=" }else{\n";
javaScript+=" setTimeout('process()',1000);\n";
javaScript+=" }\n";
javaScript+="}\n";
javaScript+="function handleServerResponse(){\n";
javaScript+=" if(xmlHttp.readyState==4 && xmlHttp.status==200){\n";
javaScript+=" xmlResponse=xmlHttp.responseXML;\n";
javaScript+=" xmlDocumentElement=xmlResponse.DocumentElement;\n";
javaScript+=" message=xmlDocumentElement.firstChild.data;\n";
javaScript+=" document.getElementById('runtime').innerHTML=message;\n";
javaScript+=" setTimeout('process()',1000);\n";
javaScript+=" }else{\n";
javaScript+=" alert('There is a problem ...');\n";
javaScript+=" }\n";
javaScript+="}\n";
javaScript+="</SCRIPT>\n";
}
String millis2time(){
String Time="";
unsigned long ss;
byte mm,hh;
ss=millis()/1000;
hh=ss/3600;
mm=(ss-hh*3600)/60;
ss=(ss-hh*3600)-mm*60;
if(hh<10)Time+="0";
Time+=(String)hh+":";
if(mm<10)Time+="0";
Time+=(String)mm+":";
if(ss<10)Time+="0";
Time+=(String)ss;
return Time;
}
void buildXML(){
XML="<?xml version='1.0'?>";
XML+="<response>";
XML+=millis2time();
XML+="</response>";
}
void handleWebsite(){
buildWebsite();
server.send(200,"text/html",webSite);
}
void handleXML(){
buildXML();
server.send(200,"text/xml",XML);
}
void setup() {
Serial.begin(115200);
WiFi.begin(ssid,password);
while(WiFi.status()!=WL_CONNECTED)delay(500);
WiFi.mode(WIFI_STA);
Serial.println("\n\nBOOTING ESP8266 ...");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("Station IP address: ");
Serial.println(WiFi.localIP());
server.on("/",handleWebsite);
server.on("/xml",handleXML);
server.begin();
}
void loop() {
server.handleClient();
}