Buenos días, soy novato en esto y me he metido a tope y necesito vuestra ayuda.
Estoy haciendo algo tan facil como en la SD guardar un HTML para luego cargarlo, en este HTML hay unos radiobuttons que dependiendo de cual pulsas varia la intensidad de un led conectado al arduino. Es bastante facil pero no termina de funcionarme bien y no se porque, he juntado cosas de varios tutoriales que he visto y lógicamente no esta bien pero no encuentro el problema exacto.
A continuación os pongo los codigo de todo, includio el log.
Sketch:
#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
// MAC address from Ethernet shield sticker under board
byte mac[] = {0x90, 0xA2, 0xDA, 0x0E, 0x0F, 0xAE};
IPAddress ip(192, 168, 1, 127); // IP address, may need to change depending on network
EthernetServer server(80); // create a server at port 80
String HTTP_req; // stores the HTTP request
boolean LED_status = 0; // state of LED, off by default
File webFile;
void setup()
{
Serial.begin(9600);
Ethernet.begin(mac, ip); // initialize Ethernet device
server.begin(); // start to listen for clients
pinMode(9, OUTPUT); // LED on pin 2
// initialize SD card
Serial.println("Initializing SD card...");
if (!SD.begin(4)) {
Serial.println("ERROR - SD card initialization failed!");
return; // init failed
}
Serial.println("SUCCESS - SD card initialized.");
// check for index.htm file
if (!SD.exists("index.htm")) {
Serial.println("ERROR - Can't find index.htm file!");
return; // can't find index file
}
Serial.println("SUCCESS - Found index.htm file.");
}
void loop()
{
EthernetClient client = server.available(); // try to get client
if (client) { // got client?
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) { // client data available to read
char c = client.read(); // read 1 byte (character) from client
HTTP_req += c; // save the HTTP request 1 char at a time
// last line of client request is blank and ends with \n
// respond to client only after last line received
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println();
// send web page
webFile = SD.open("index.htm"); // open web page file
if (webFile) {
while(webFile.available()) {
client.write(webFile.read()); // send web page to client
}
webFile.close();
}
leerRadioButton();
Serial.print(HTTP_req);
HTTP_req = "";
break;
}
// every line of text received from the client ends with \r\n
if (c == '\n') {
// last character on line of received text
// starting new line with next character read
currentLineIsBlank = true;
}
else if (c != '\r') {
// a text character was received from client
currentLineIsBlank = false;
}
} // end if (client.available())
} // end while (client.connected())
delay(1); // give the web browser time to receive the data
client.stop(); // close the connection
} // end if (client)
}
// switch LED and send back HTML for LED checkbox
void leerRadioButton()
{
String valor;
String intensidad = "GET /?intesidad=";
int lon = intensidad.length();
if (HTTP_req.indexOf("intensidad=") > -1) {
valor = HTTP_req.substring(lon+1,lon+4);
LED_status=1;
}else{
LED_status=0;
}
char valueArray[valor.length() + 1];
valor.toCharArray(valueArray, sizeof(valueArray));
int conversion = atoi(valueArray);
if (LED_status) {
Serial.println("Aquí esta encendida");
analogWrite(9,conversion);// switch LED on
}else {
Serial.println("Aqui esta apagada");
analogWrite(9, 0);
}
}
Codigo HTML:
<html>
<head>
<title>Ejemplo Radio Button</title>
<script>
function cambiaIntensidad(){
var i
for (i=0;i<document.valores.valor.length;i++){
if (document.valores.valor[i].checked)
break;
}
location.href="?intensidad="+document.valores.valor[i].value;
}
</script>
</head>
<body>
<form name="valores">
VALORES LED:
<input type="Radio" name="valor" value="200" checked onchange="cambiaIntensidad()"> 200
<input type="Radio" name="valor" value="300" onchange="cambiaIntensidad()"> 300
<input type="Radio" name="valor" value="400" onchange="cambiaIntensidad()"> 400
<input type="Radio" name="valor" value="500" onchange="cambiaIntensidad()"> 500
<input type="Radio" name="valor" value="600" onchange="cambiaIntensidad()"> 600
</form>
</body>
</html>
Bueno la pagina me carga bien, y cuando pulso sobre los radiobutton por JS debería de recargarse la pagina, pero no lo hace, el arduino no me devuelve nada y el led conectado al pin 9 se enciende con la intensidad pero solo durante un segundo y se vuelve a apagar. No entiendo muy bien donde esta el problema. Lo que me sorprende tambien es el log
Aqui esta apagada
GET / HTTP/1.1
Host: 192.168.1.127
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36
Accept-Encoding: gzip,deflate,sdch
Accept-Language: es-ES,es;q=0.8
Aqui esta apagada
GET /favicon.ico HTTP/1.1
Host: 192.168.1.127
Connection: keep-alive
Accept: */*
User-Agent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36
Accept-Encoding: gzip,deflate,sdch
Accept-Language: es-ES,es;q=0.8
Esta es la salida la primera vez que accedo al arduino, la primera parte la entiendo pero no entiendo porque me sale por segunda vez "Aqui esta apagada" y toda la trama esta ¿Que es todo es? Si pulso sobre un radio button, el arduino no me devuelve nada, se enciende el led durante un segundo y me devuelve esto.
Aquà esta encendida
GET /?intensidad=600 HTTP/1.1
Host: 192.168.1.127
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36
Referer: http://192.168.1.127/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: es-ES,es;q=0.8
Aqui esta apagada
GET /favicon.ico HTTP/1.1
Host: 192.168.1.127
Connection: keep-alive
Accept: */*
User-Agent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36
Accept-Encoding: gzip,deflate,sdch
Accept-Language: es-ES,es;q=0.8
Otra vez lo de "Aqui esta apagada" ¿Por que?
Se que son muchas cosas pero es que estoy perdidisimo. ¿Por qué cuando pulso sobre un checkbox no me aparece la pagina de nuevo y no me devuelve nada? ¿Por qué el led se enciende solo un segundo? ¿Qué es esa segunda parte del log que aparece cada vez que pulso?