Bonjour a tous.
Je suis rentré en mode réalisation de mon projet domotique, et je commence les tests avec les différents composant en volant.
J'ai besoin de récupérer les états des broches digitale, et des températures issues de DS18B20, en continu.
N'ayant pas les compétence, ni le temps de me former sur le sujet, j'ai éplucher des dizaines de site/blog traitant de webserver arduino "Ajax".
J'ai fini par trouver celui-ci: Arduino--Ajax-Web--Server/Ajax_WebServer at master · mrebola/Arduino--Ajax-Web--Server · GitHub
Je précise que toutes les infos (état des broches digitale et température) sont présente, et accessible via le code.
Quand je me connecte via mon browser sur l'arduino, j'ai bien la page qui s'affiche, et tout fonctionne bien.
J'ai modifier un peu le code pour ajouter quelques fonctionnalité nécessaire à mon projet. Jusque la, pas de soucis.
Le problème viens du fait que ce petit bout de code génère une ligne (callback ??) en JSONP, qui contient toutes les infos en temps réel, mais que je ne sais pas comment la récupérer dans une page de mon intranet.
Si quelqu'un avait une piste pour un exemple applicable à mon cas, ca serait super.
Parce que la, j'avoue que je suis perdu.
Merci d'avance.
void GestionClientWeb()
{
// listen for incoming clients
EthernetClient client = Localserver.available();
if (client) {
// an http request ends with a blank line
Serial.println("Client");
boolean currentLineIsBlank = true;
String CL="myButton";
while (client.connected()) {
if (client.available()) {
char c = client.read();
// serch parameter from "HTTP GET"
if(gflag){
if(c != ' '){
parm += c;
}else{
gflag = false;
}
}
if(c == '?' && parm == ""){
gflag = true;
}
//Serial.print(c);
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
if(parm == ""){ // normal HTTP access
Serial.println("normal HTTP access");
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.println("<html><head><title>Hello Arduino</title>");
client.println("<meta name='viewport' content='width=240px' />");
client.println("<script type='text/javascript'>");
client.println("function createXMLHttpRequest(cbFunc){");
client.println(" var XObj = new XMLHttpRequest();");
client.println(" if(XObj) XObj.onreadystatechange = cbFunc;return XObj;}");
client.println("function setData(val){htObj = createXMLHttpRequest(displayData);");
client.println(" if(htObj){htObj.open('GET','/?btn='+val,true);htObj.send(null);}}");
client.println("function getData(){htObj = createXMLHttpRequest(displayData);");
client.println(" if(htObj){htObj.open('GET','/?=',true);htObj.send(null);}}");
client.println("function displayData(){ if((htObj.readyState == 4) && (htObj.status == 200)){");
client.println(" document.getElementById('result').innerHTML = htObj.responseText;}}");
client.println("function strT(){ getData(); timerID=setTimeout('strT()',");
client.println(" document.getElementById('tf1').value);}");
client.println("function clrT(){clearTimeout(timerID);}");
client.println("</script>");
client.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"http://xxx.xxx.xxx.xxx/......./Bouton.css\" />");
client.println("</head><body onLoad='getData()'><form action='/' method='GET'>
");
for (int i=0; i<50; i++)
{
if (i!=4 && i!=10 && i!=11 && i!=12 && i!=13)
{
CL="myButton";
if (digitalRead(i)==1) {CL="myButton2";}
if (i%10!=0)
{
client.println("<a href=\"#\" class=\""+CL+"\" onClick='setData(" + String (i) + ")'>D" + String (i) + "</a>");
//client.println("<input id='btn" + String (i) + "0' type='button' value=' D" + String (i) + " ' onClick='setData(" + String (i) + ")'>");
}
else
{
client.println("<a href=\"#\" class=\""+CL+"\" onClick='setData(" + String (i) + ")'>D" + String (i) + "</a>
");
//client.println("<input id='btn" + String (i) + "' type='button' value=' D" + String (i) + " ' onClick='setData(" + String (i) + ")'>
");
}
}
}
client.println("</form><form><input id='btn100' class=\"myButton\" type='button' value=' START ' onClick='strT()'>");
client.println("<input id='btn200' class=\"myButton\" type='button' value=' STOP ' onClick='clrT()'>");
client.println("<input id='tf1' type='text' size='6' value='1000'>");
client.println("</form>
<div id='result'></div>");
client.println("
</body></html>");
}else{ // using XMLhttpObject access
int check = parm.indexOf('=');
if(check != -1){
//Set Digital Port
int check2 = parm.indexOf('btn');
if(check2 != -1){
int port = (parm.substring(check+1)).toInt();
dout[port] = !dout[port];
digitalWrite(port, dout[port]);
}
int check3 = parm.indexOf('tmp');
if(check3 != -1){
int port = (parm.substring(check+1)).toInt();
client.print (Temperature[port]);
}
int check4 = parm.indexOf('eta');
if(check4 != -1){
int port = (parm.substring(check+1)).toInt();
client.print (digitalRead(port));
}
//Write JSONP Data (Digital & Analog Ports Status, Callback function name is 'cb')
client.print("cb({\"D\":[");
for(int i=0; i<50; i++ ){
client.print(dout[i]);
if(i<50){
client.print(",");
}
}
client.print("],\"A\":[");
for(int i=0; i<16; i++ ){
client.print(analogRead(i));
if(i<15){
client.print(",");
}
}
client.print("],\"T\":[");
for(int i=3; i<13; i++ ){
client.print(Temperature[i]);
if(i<13){
client.print(",");
}
}
client.print("]});");
}
parm = "";
}
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
}
}