Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
267 views
in Technique[技术] by (71.8m points)

Javascript/HTML - get data from Arduino

I'm trying to get data from an Arduino using Javascript. The problem is that I can't take more than 10 variabiles; everything that goes over is ignored by the code. I put the code sequence below.

The variables test0 and in 1 receive the values the variables test 1 and 2 do not receive anything.

I tried to use another reference like "analog2" and another index, but nothing worked.

I'm not specialized in this language, I just need to make it work.

Do you have any suggestions on how I can get more than 10 variables?

Thank you in advance!

Javascript:

function GetArduinoInputs() {
  nocache = "&nocache=" + Math.random() * 1000000;
  var request = new XMLHttpRequest();
  request.onreadystatechange = function() {
    if (this.readyState == 4) {
      if (this.status == 200) {
         if (this.responseXML != null) {
           document.getElementById("test0").innerHTML = this.responseXML.getElementsByTagName('analog')[0].childNodes[0].nodeValue;
           document.getElementById("in1").innerHTML = this.responseXML.getElementsByTagName('analog')[1].childNodes[0].nodeValue;
           document.getElementById("in2").innerHTML = this.responseXML.getElementsByTagName('analog')[2].childNodes[0].nodeValue;
           document.getElementById("in3").innerHTML = this.responseXML.getElementsByTagName('analog')[3].childNodes[0].nodeValue;
           document.getElementById("in4").innerHTML = this.responseXML.getElementsByTagName('analog')[4].childNodes[0].nodeValue;  
           document.getElementById("in5").innerHTML = this.responseXML.getElementsByTagName('analog')[5].childNodes[0].nodeValue;
           document.getElementById("in6").innerHTML = this.responseXML.getElementsByTagName('analog')[6].childNodes[0].nodeValue;
           document.getElementById("in7").innerHTML = this.responseXML.getElementsByTagName('analog')[7].childNodes[0].nodeValue;       
           document.getElementById("in8").innerHTML = this.responseXML.getElementsByTagName('analog')[8].childNodes[0].nodeValue;
           document.getElementById("in9").innerHTML = this.responseXML.getElementsByTagName('analog')[9].childNodes[0].nodeValue;   
           document.getElementById("test1").innerHTML = this.responseXML.getElementsByTagName('analog')[10].childNodes[0].nodeValue;
           document.getElementById("test2").innerHTML = this.responseXML.getElementsByTagName('analog')[11].childNodes[0].nodeValue;           
        }
      }
    }
  }
  request.open("GET", "ajax_inputs" + nocache, true);
  request.send(null);
  setTimeout('GetArduinoInputs()', 200);           
}

Data in HTML:

    <td class="tg-fia5">Putere instalata</td>
    <td class="tg-fia5" id="test0">p11</td>
    <td class="tg-fia5">kW</td>
    <td class="tg-fia5" >Putere instalata</td>
    <td class="tg-fia5" id="test1" >p21</td>
    <td class="tg-fia5">kW</td>
    <td class="tg-fia5">Putere instalata</td>
    <td class="tg-fia5" id="in1">p31</td>
    <td class="tg-fia5">kW</td>

Arduino code:

void date_trimise(EthernetClient cl) {
    cl.print("<inputs>");
    cl.print("<analog>");
    cl.print(u1);
    cl.print("</analog>");
    cl.print("<analog>");
    cl.print(u2);
    cl.print("</analog>");
    cl.print("<analog>");
    cl.print(i1);
    cl.print("</analog>");
    cl.print("<analog>");
    cl.print(p1);
    cl.print("</analog>");
    cl.print("<analog>");
    cl.print(t1);
    cl.print("</analog>");
    cl.print("<analog>");
    cl.print(h1);
    cl.print("</analog>");
    cl.print("<analog>");
    cl.print(v1);
    cl.print("</analog>");
    cl.print("<analog>");
    cl.print(d1);
    cl.print("</analog>");
    cl.print("<analog>");
    cl.print(pz1);
    cl.print("</analog>");
    cl.print("<analog>");
    cl.print(pz2);
    cl.print("</analog>");   
    cl.print("<analog>");
    cl.print(u3);
    cl.print("</analog>");
    cl.print("<analog>");
    cl.print(u4);
    cl.print("</analog>");
    cl.print("</inputs>"); 
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...