Post topics, source code that relate to the Arduino Platform

User avatar
By 404hult
#72539 Hello and thank you for good information!

I try to create multiple pages with dynamic data but I do not know how to distinguish or find out from which page the xml request comes from. Now my dynamic data comes always from the "millis2time". I have tried with new handlers like:
"server.on("/",handleWebsite);
server.on("/xml",handleXML);

server.on("/test1",handleWebsite_test);
server.on("/test1/xml",handleXML_test);
"
But this seems not to be correct. How can I handle/find out this in the "Void handleXML"-function?

Best Regards
Stefan H
User avatar
By katz
#72584 @Stefan

You will probably need another buildJavascript() function as well. In there you can make the call to your XML-page (/test1/xml) inside the javascript.

Good luck, Peter.
User avatar
By 404hult
#72585 Thank you Katz for your reply.

I have copied the ”handling” functions to a new set (for learning purpose). I will read, check and try to learn more and see if I can find out how it works.

/S
User avatar
By codeJake
#73161
katz wrote:Hi all,

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.

Code: Select all#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();
}



Fantastic. This code helped me more then you could know. I've been struggling to get the uptime of my nodemcu to update n the webpage like this for nearly a month. I've tried many things and this is the only working example I could find. It makes a nice addition to the current time displayed by the browser in local time so that knowing when the last data update was done and when to expect the next update to refresh all the page controls. I did make changes to show system uptime in the following format though(1 day 01:27:48 ).
Thank you for sharing. Sharing is what makes these devices possible for all to use.