2

OK, so I'm trying to monitor and control something with a Pic-web from microchip. I managed to program it all except for one thing. I need to compare two temperatures that it gives me, and if one of them is smaller that the other then a relay needs to be activated, if its bigger or equal then another relay must be activated. From what I understood Pic-web updates an xml file (status.xml) then the website (using ajax) takes the values from there and displays them on the web page.

The XML file looks like this

<response>
<temp0>~temp~</temp0>
<temp2>~tempc~</temp2>
</response

The ajax code I use to get the values in my web page

document.getElementById('temp0').innerHTML = getXMLValue(xmlData, 'temp0');
document.getElementById('temp2').innerHTML = getXMLValue(xmlData, 'temp2');

And to display them I use

<span id="temp0"style="font-weight:normal">?</span>
<span id="temp2"style="font-weight:normal">?</span>

All I need to do is to compare temp0 with temp2

    var temp0 = parseFloat(document.getElementById('temp0').innerHTML)
    var temp2 = parseFloat(document.getElementById('temp2').innerHTML)

    if( temp0 < temp2 ) 
    {
        document.getElementById('temp3').innerHTML = '<font color="#00FF00">ON</font>';
    } 
    else 
    {
        document.getElementById('temp3').innerHTML = '<font color="#00FFFF">OFF</font>';

}

The I just add <span id="temp3">?</span> to display the result

Am I correct?

4 Answers 4

1
if(getXMLValue(xmlData, 'temp0') < 'temp2')

'temp0' is not the value of document.getElementById('temp0'), it's just a string you made up containing 'temp0'.

You need to assign this to a variable and convert it to a number from a string:

var temp0 = parseFloat(document.getElementById('temp0').innerHTML)
var temp2 = parseFloat(document.getElementById('temp2').innerHTML)

Then use:

if(temp0 < temp2)
Sign up to request clarification or add additional context in comments.

Comments

0

This line looks incorrect...

if(getXMLValue(xmlData, 'temp0') < 'temp2')

Should it be...?

if(getXMLValue(xmlData, 'temp0') < getXMLValue(xmlData, 'temp2'))

And unless getXMLValue returns a number of some variety, you will want want to convert it before comparing

Comments

0

What about something like this:

var temp0 = parseInt(getXMLValue(xmlData, 'temp0'))
  , temp2 = parseInt(getXMLValue(xmlData, 'temp2'));

if( temp0 < temp2 ) {
    ...
} else {
    ...
}

Comments

0
// Compare the two temperatures
var temp0 = parseFloat(document.getElementById('temp0').innerHTML)
var temp2 = parseFloat(document.getElementById('temp2').innerHTML)

if( temp0 < temp2 ) {
    document.getElementById('temp3').innerHTML = '<font color="#00FF00">ON</font>';
} else {
    document.getElementById('temp3').innerHTML = '<font color="#FF0000">OFF</font>';
}

This will display ON if temp2 > temp0

and OFF if temp0 < temp2

thank all of you for your support

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.