Re: ESP8266 AJAX [SOLVED]
Posted: Mon Sep 28, 2015 2:38 pm
Hi,
very appreciated code but
not so advanced in javascript as your level,
I dont know how to had another value to the buildXML and how to display it.
A little help would be very useful.
very appreciated code but
not so advanced in javascript as your level,
I dont know how to had another value to the buildXML and how to display it.
A little help would be very useful.
Code: Select all
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
ESP8266WebServer server(80);
const char* ssid="SSID";
const char* password="password";
String webSite,javaScript,XML;
int sensorPin = A0; // select the input pin for ldr
int sensorValue = 0; // variable to store the value coming from the sensor
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'></A><BR>\n";
webSite+="LDR = <A id='LDR'></A>\n";
webSite+="</BODY>\n";
webSite+="</HTML>\n";
}
void buildJavascript(){
javaScript="<SCRIPT>\n";
javaScript+="var xmlHttp=createXmlHttpObject();\n";
javaScript+="function createXmlHttpObject(){\n";
javaScript+=" if(window.XMLHttpRequest){\n";
javaScript+=" xmlHttp=new XMLHttpRequest();\n";
javaScript+=" }else{\n";
javaScript+=" xmlHttp=new ActiveXObject('Microsoft.XMLHTTP');\n";
javaScript+=" }\n";
javaScript+=" return xmlHttp;\n";
javaScript+="}\n";
javaScript+="function process(){\n";
javaScript+=" if(xmlHttp.readyState==0 || xmlHttp.readyState==4){\n";
javaScript+=" xmlHttp.open('PUT','xml',true);\n";
javaScript+=" xmlHttp.onreadystatechange=handleServerResponse;\n"; // no brackets?????
javaScript+=" xmlHttp.send(null);\n";
javaScript+=" }\n";
javaScript+=" setTimeout('process()',1000);\n";
javaScript+="}\n";
javaScript+="function handleServerResponse(){\n";
javaScript+=" if(xmlHttp.readyState==4 && xmlHttp.status==200){\n";
javaScript+=" xmlResponse=xmlHttp.responseXML;\n";
javaScript+=" xmldoc = xmlResponse.getElementsByTagName('response');\n";
javaScript+=" message = xmldoc[0].firstChild.nodeValue;\n";
javaScript+=" document.getElementById('runtime').innerHTML=message;\n";
javaScript+=" xmldoc = xmlResponse.getElementsByTagName('responseLDR');\n";
javaScript+=" message = xmldoc[0].firstChild.nodeValue;\n";
javaScript+=" document.getElementById('LDR').innerHTML=message;\n";
javaScript+=" }\n";
javaScript+="}\n";
javaScript+="</SCRIPT>\n";
}
void buildXML(){
XML="<?xml version='1.0'?>";
XML+="<response>";
XML+=millis2time();
XML+="</response>";
XML+="<responseLDR>";
XML+=sensorValue;
XML+="</responseLDR>";
}
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 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() {
sensorValue = analogRead(sensorPin);
delay(250);
server.handleClient();
}