Hola, estoy metiendome en la parte de ajax con arduino y en seguida me topé con un problema que no puedo dilucidar. Tengo una Mega 2560 + Ethernet Shield W5100 y el código es (es uno sacado de un ejemplo apenas retocado para simplificar la prueba):
//---------------------------------------------------------------------------
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 0, 10);
EthernetServer server(80);
boolean estado=true;
String HTTP_req;
void setup()
{
Ethernet.begin(mac, ip);
server.begin();
//Serial.begin(115200); // for diagnostics
}
void loop()
{
EthernetClient client = server.available(); // try to get client
if (client) {
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
HTTP_req += c;
if (c == '\n' && currentLineIsBlank) {
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: keep-alive");
client.println();
if (HTTP_req.indexOf("ajax_switch") > -1) {
GetAjaxData(client);
}
else {
client.println("<!DOCTYPE html>");
client.println("<html>");
client.println("<head>");
client.println("<title>Arduino Web Page</title>");
client.println("<script>");
client.println("function GetSwitchAnalogData() {");
client.println("nocache = \"&nocache=\" + Math.random() * 1000000;");
client.println("var request = new XMLHttpRequest();");
client.println("request.onreadystatechange = function() {");
client.println("if (this.readyState == 4) {");
client.println("if (this.status == 200) {");
client.println("if (this.responseText != null) {");
client.println("document.getElementById(\"sw_an_data\").innerHTML = this.responseText;");
client.println("}}}}");
client.println("request.open(\"GET\", \"ajax_switch\" + nocache, true);");
client.println("request.send(null);");
client.println("setTimeout('GetSwitchAnalogData()', 1000);");
client.println("}");
client.println("</script>");
client.println("</head>");
client.println("<body onload=\"GetSwitchAnalogData()\">");
client.println("<h1>Arduino AJAX Input</h1>");
client.println("<div id=\"sw_an_data\">");
client.println("</div>");
client.println("</body>");
client.println("</html>");
}
//Serial.print(HTTP_req);
HTTP_req = "";
break;
}
if (c == '\n') {
currentLineIsBlank = true;
}
else if (c != '\r') {
currentLineIsBlank = false;
}
} // end if (client.available())
} // end while (client.connected())
delay(1);
client.stop();
} // end if (client)
}
void GetAjaxData(EthernetClient cl)
{
cl.print("<p>Estado: ");
cl.print(estado);
cl.println("</p>");
estado=!estado; //to realize when the changes are made
}
//---------------------------------------------------------------------------
El problema es que después de un tiempo X deja de actualizarse el Ajax. Si habilito el debug por serie veo que se recibe el primer HTTP request (el de la web), el mismo se responde sin inconvenientes, después empiezan a llegar los pedidos del ajax que también se reciben sin problema pero después de repetir el pedido varias veces (aprox unas 100/150) el pedido empieza a fallar. Lo que llega como Ajax request es:
//---------------------------------------------------------------------------
GET /ajax_switch&nocache=833922.3657859031 HTTP/1.1
Host: 192.168.0.10
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:51.0) Gecko/20100101 Firefox/51.0
Accept: */*
Accept-Language: es-AR,es;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Referer: http://192.168.0.10/
Connection: keep-alive
GET /ajax_switch&nocache=722496.1500752304 HTTP/1.1
Host: 192.168.0.10
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:51.0) Gecko/20100101 Firefox/51.0
Accept: */*
Accept-Language: es-AR,es;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Referer: http://192.168.0.10/
Connection: keep-alive
GET /ajax_switch&nocache=562945.7436339679 HTTP/1.1
Host: 192.168.0.10
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:51.0) Gecko/20100101 Firefox/51.0
Accept: */*
Accept-Language: es-AR,es;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Referer: httpx_switch&nocache=562945.7436339679 HTTP/1.1
Host: 192.168.0.10
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:51.0) Gecko/20100101 Firefox/51.0
Accept: */*
Accept-Language: es-AR,es;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Referer: /ajax_switch&nocache=562945.7436339679 HTTP/1.1
Host: 192.168.0.10
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:51.0) Gecko/20100101 Firefox/51.0
Accept: */*
Accept-Language: es-AR,es;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
ReferGET /ajax_switch&nocache=562945.7436339679 HTTP/1.1
Host: 192.168.0.10
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:51.0) Gecko/20100101 Firefox/51.0
Accept: */*
Accept-Language: es-AR,es;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Referer: hajax_switch&nocache=562945.7436339679 HTTP/1.1
Host: 192.168.0.10
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:51.0) Gecko/20100101 Firefox/51.0
Accept: */*
Accept-Language: es-AR,es;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
RefereET /ajax_switch&nocache=562945.7436339679 HTTP/1.1
Host: 192.168.0.10
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:51.0) Gecko/20100101 Firefox/51.0
Accept: */*
//---------------------------------------------------------------------------
Por lo que dejo de tener el caracter de fin y nunca completo la lectura. Lo que es raro también, es que si sin desconectar el USB de la PC le vuelvo a bajar el mismo código y luego intento acceder, directamente en el primer pedido (el de la página web) llega con errores:
//---------------------------------------------------------------------------
GET / HTTP/1.1
Host: 192.168.0.10
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:51.0) Gecko/20100101 Firefox/51.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: es-AR,es;q=0.8,en-US;q=0.5,en;q=0.3
Accept-EncodinGET / HTTP/1.1
Host: 192.168.0.10
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:51.0) Gecko/20100101 Firefox/51.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: es-AR,es;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gz HTTP/1.1
Host: 192.168.0.10
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:51.0) Gecko/20100101 Firefox/51.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: es-AR,es;q=0.8,en-US;q=0.5,en;q=0.3
Accept-EncodingET / HTTP/1.1
Host: 192.168.0.10
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:51.0) Gecko/20100101 Firefox/51.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: es-AR,es;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gziHTTP/1.1
Host: 192.168.0.10
//----------------------------------------------------------------------------
Tampoco recibo el caracter de fin y nunca completo el primer pedido. También es raro que si en ese instante desenergizo y vuelvo a energizar, el sistema vuelve a andar como la primera vez; es como si hubiera algún buffer que se satura y hasta no desenergizar no se libera.
Un dato más de color es que el mismo "html" que escribo linea por linea traté de subirlo a la SD (ejemplo de (Arduino SD Card Ajax Web Server Displaying Switch Status) y levantarlo desde ahí y los resultados no fueron buenos ya que nunca completa el envío del archivo.
Alguien puede ayudarme?