I have an Yun, and i uploaded the "bridge" sketch into it.
inside the /www i created a "led" directory, the a index.html in the last one.
index.html
<body background="imagens/caldeira.jpg">
<form action=/arduino/digital/13 method=post>
<p>
<input name=Status type=text id=status value=status>
</p>
<p> </p>
</form>
<form action=/arduino/digital/13/1 method=post enctype="application/x-www-form-urlencoded" target="_self">
<p>
<input name="Liga Caldeira" type=submit class="Azul" id="Liga Caldeira" value="Ligar Caldeira">
</p>
<p> </p>
</form>
<form action=/arduino/digital/13/0 method=post enctype="application/x-www-form-urlencoded" target="_self">
<p>
<input name="Desligar Caldeira" type=submit class="vermelho" id="Desligar Caldeira" value="Desligar Caldeira">
</p>
</form>
<h1 class="cabe..alho"><span class="vermelho">Controlo Remoto da Caldeira de Aquecimento de ..gua</span></h1>
<p> </p>
</body>
Works perfectly!!
But the compiled bridge sketch generates another page to the browser with the updated value of the led 13 , and i didn't wanted that.
this function in the "bridge":
void digitalCommand(YunClient client) {
int pin, value;
// Read pin number
pin = client.parseInt();
// If the next character is a '/' it means we have an URL
// with a value like: "/digital/13/1"
if (client.read() == '/') {
value = client.parseInt();
digitalWrite(pin, value);
}
else {
value = digitalRead(pin);
}
// Send feedback to client
client.print(F("Pin D"));
client.print(pin);
client.print(F(" set to "));
client.println(value);
// Update datastore key with the current pin value
String key = "D";
key += pin;
Bridge.put(key, String(value));
}
To catch the value of this function without reloading the page, i need in java script , an "HttpRequest".
I want to retrieve this same updated value without reloading the whole page but the "status" value in the textbox of the first form, and i need a javascript code for it...
something like that..
AJAX portion ?
<span id="ajaxButton" style="cursor: pointer; text-decoration: underline">
status
</span>
<script type="text/javascript">
(function() {
var httpRequest;
document.getElementById("ajaxButton").onclick = function() { makeRequest('/arduino/digital/13'); };
function makeRequest(url) {
if (window.XMLHttpRequest) { // Mozilla, Safari, ...
httpRequest = new XMLHttpRequest();
} else if (window.ActiveXObject) { // IE
try {
httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
try {
httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {}
}
}
if (!httpRequest) {
alert('Giving up :( Cannot create an XMLHTTP instance');
return false;
}
httpRequest.onreadystatechange = alertContents;
httpRequest.open('GET', url);
httpRequest.send();
}
function alertContents() {
if (httpRequest.readyState === 4) {
if (httpRequest.status === 200) {
alert(httpRequest.responseText);
} else {
alert('There was a problem with the request.');
}
}
}
})();
</script>
Does anybody can gave a heads up cause i put the AJAX SCRIPT in the index.html, but the error returns:
""There was a problem with the request."
i think, i not using right the span tag ... but ... i dont know, does anyone did something with the Bridge sketch and update a value without having change page, i mean reloads just the value without generating another page (default of the bridge)
In other words i meant to catch the "value" variable updated without having to reload the whole page...
Thank's in advance....
Rui Pereira