i have an arduino server with a ENC28J60 ethernet controller. I am using the ethercard.h library to run it. when a browser makes a request, it makes a number of requests, chrome seems to be the worst, making 4 requests for the actual page and an additional 2 for the favicon. my arudino polls my sensors everytime there is a request so i would like to minimize the requests. my only solution so far is if a request is made to soon after a previous request is to ignore it, which seems to be work, but this just seems wrong? im not sure for the reason for the multiple request or if i should (and how) be telling the browser that it has all the information and stop requesting additional information?
code for loop:
void loop() {
word len = ether.packetReceive();
word pos = ether.packetLoop(len);
// check if valid tcp data is recieved
if (pos) {
bfill = ether.tcpOffset();
char *data = (char *) Ethernet::buffer + pos;
Serial.println("\n***** New Request *****");
Serial.println(data);
// recieve buf hasn't been clobbered by reply yet
// if string starts with s, reply with sensor data
if (strncmp("GET /s", data, 6) == 0) {
if ((millis() - lastrequest) > 5000) {
Serial.println("\nReturn Sensor Data");
homePage(bfill);
lastrequest = millis();
}
else {
Serial.println("\nHammer");
bfill.emit_p(PSTR(
"HTTP/1.0 200 OK\r\n"
"Content-Type: text/plain\r\n"
"\r\n"
"Hammer Request"));
}
}
// all other requests get a 404 page
else {
Serial.println("\n404");
bfill.emit_p(PSTR(
"HTTP/1.0 200 OK\r\n"
"Content-Type: text/plain\r\n"
"\r\n"
"404 - Not Found"));
}
// send web page data
ether.httpServerReply(bfill.position());
}
delay(200);
}
the homePage function is essentially the same as the 404 and hammer returns, except with sensor data (but is still text)