Go Down

Topic: Ethernet shield with SD web server simple question (Read 1 time) previous topic - next topic

Sotsot

Hello,
I'm trying to use arduino with ethernet shield and SD card as web server, following the examples of https://startingelectronics.org/tutorials/arduino/ethernet-shield-web-server-tutorial/.

I've managed to create a web page and send various values from arduino to the server using the following:

index.html

data_t_out  = this.responseXML.getElementsByTagName('analog')[0].childNodes[0].nodeValue;
data_t_in = this.responseXML.getElementsByTagName('analog')[1].childNodes[0].nodeValue;
data_hum = this.responseXML.getElementsByTagName('analog')[2].childNodes[0].nodeValue;
data_barom  = this.responseXML.getElementsByTagName('analog')[3].childNodes[0].nodeValue;

arduino side

        cl.print("<inputs>");

        cl.print("<analog>");
        cl.print(data_in.temp_ext);
        cl.print("</analog>");

        cl.print("<analog>");
        cl.print(data_out.temp_room);
        cl.print("</analog>");

        cl.print("<analog>");
        cl.print(data_out.hum_room);
        cl.print("</analog>");

        cl.print("<analog>");
        cl.print(data_out.barom);
        cl.print("</analog>");

        cl.print("</inputs>");

However, when I'm adding 4 strings:

in index.html and before the final cl.print("</inputs>");

short_term = this.responseXML.getElementsByTagName('meteo')[0].childNodes[0].nodeValue;
short_term_extra = this.responseXML.getElementsByTagName('meteo')[1].childNodes[0].nodeValue;
long_term = this.responseXML.getElementsByTagName('meteo')[2].childNodes[0].nodeValue;
long_term_extra = this.responseXML.getElementsByTagName('meteo')[3].childNodes[0].nodeValue;

and in arduino

        cl.print("<meteo>");
        cl.print(oneHour_text);
        cl.print("</meteo>");

        cl.print("<meteo>");
        cl.print(oneHour_extra);
        cl.print("</meteo>");

        cl.print("<meteo>");
        cl.print(threeHour_text);
        cl.print("</meteo>");

        cl.print("<meteo>");
        cl.print(threeHour_extra);
        cl.print("</meteo>");

the web page doesn't get the strings.

I am really green regarding Java, AJAX, ecc, so please forgive my question.

Juraj

I recommend you to use JSON format.
and set the Content-length header

Sotsot

Thanks for your recommendation, but my question was why, while it passes values for 'analog', it ignores the strings in 'meteo'.

ocrdu

What does the complete responseXML XML instance look like? Does the browser show any errors?

Sotsot

No errors from the browser side. I managed to show the text strings with:

              <p>Previsione breve 1: <span class="meteo">...</span></p>
              <p>Previsione breve 2: <span class="meteo">...</span></p>
              <p>Previsione lunga 1: <span class="meteo">...</span></p>
              <p>Previsione lunga 2: <span class="meteo">...</span></p>

but I don't know how to get the values and use them for other computations. I've tried with:

             <p><script type="text/javascript">
             var short_term = document.getElementsByClassName("meteo")[0].innerHTML;
             var short_term_extra = document.getElementsByClassName("meteo")[1].innerHTML;
             var long_term = document.getElementsByClassName("meteo")[2].innerHTML;
             var long_term_extra = document.getElementsByClassName("meteo")[3].innerHTML;
             document.write("Test meteo: ");
             document.write(short_term);
            document.write(short_term_extra);
        </script></p>

and no show on browser.

My ignorant question is how to assign a damn name to all those variables that I manage to send and use then elsewhere in the html or javascript code.

ocrdu

Your text strings contain markup, so they become an integral part of the DOM of the XML document you retrieve.

this.responseXML.getElementsByTagName('meteo')[0].childNodes[0].nodeValue now tries to get the nodeValue of a <p>, not the nodeValue of a text node, so you either need a different query, or send text without HTML markup.

The quickest solution if you insist on using XML and want to keep the HTML markup is probably sending your text strings as CDATA sections, so something like:

cl.print("<meteo>");
cl.print("<![CDATA[");
cl.print(oneHour_text);
cl.print("]] >");
cl.print("</meteo>");

I haven't tested that for you, though.

Another option is to use this.responseText instead of this.responseXML, and get the response as plain text instead of XML.

Juraj


Sotsot

I really thank you for your replies. The code I used was published on the startingelectronics site (https://startingelectronics.org/tutorials/arduino/ethernet-shield-web-server-tutorial/SD-card-gauge/), it worked for the gauges, I repeated the code for more gauges and it still worked, so I used it. The web server is in the SD card, it makes requests to arduino, arduino sends the data, the web page shows all dials correctly (about 10 of them) and everybody is happy. BUT ....(and this is a new but) I cannot find a way to use the variables arduino has sent. All of them, not just the string variables.
I know my Fortran, Pascal, C and Python but nothing about html and javascript. So I can leave the part that uses the variables for the gauges and try something else for the rest of the variables. Searching the internet was useless, that's why I asked the question here. I'm not a dummy but I can't invest time studying something I will not use again (and due to my age, I will forget in a few months) so I ask for help. The SD server must request various data (int, float, bool and strings) from arduino, arduino must send the data and lastly, the server must get the data in a way a html-java ignorant like me can use them again (as variables or arrays) without difficulties. A simple example SD server-side and arduino-side would be sufficient.
Again I thank you very very much.

Juraj

In my project I have a web server and web page, but I don't know if it can help you. It has what you need but does it a little more complicated as the tutorial. Mainly on the server side the additional thing is that I use my StreamLib for buffering and chunked transfer encoding. On the browser side the complication is that I generate most of the page with Java Script.

the server https://github.com/jandrassy/Regulator/blob/master/Regulator/WebServer.ino

web file https://github.com/jandrassy/Regulator/tree/master/Regulator/data

Go Up