I am trying to get my Arduino to receive commands from a website in real time.
I am using the Arduino WifiShield with the WebServer example that I modified to receive data from clients.
Connecting and disconnecting is slow (1~2 seconds) and I am trying to keep the connection open using something like:
client.println("Connection: keep-alive");
client.println("Keep-Alive: timeout=30, max=100");
in the HTTP header. This by itself doesn't work and the Arduino continues to disconnect from the client. So I tried removing the client.stop(); command, but then the page wouldn't load at all. I'm thinking, should I try tinkering with the
if (c == '\n') {
// you're starting a new line
}
else if (c != '\r') {
// you've gotten a character on the current line
}
lines to make the Arduino understand that it should not disconnect yet. Here is the main part of my loop function. Any thoughts?
void loop() {
// listen for incoming clients
WiFiClient client = server.available();
if (client) {
Serial.println("client exists");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
String POST = "";
while(client.available()){
c = client.read();
// save the variables somewhere
POST += c;
}
Serial.println("sending data to client");
//HEADER
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: keep-alive");
client.println("Keep-Alive: timeout=30, max=100");
client.println();
//HTML
client.println("<!DOCTYPE html>");
client.println("<html>");
client.println("<head>");
client.println("<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js'></script>");
client.println("<script src='https://rawgit.com/joestox/moll_e/master/script.js'></script>");
client.println("</head>");
client.println("</html>");
delay(1000);
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
} // if client is available
} //while client is connected
// give the web browser time to receive the data
delay(100);
client.stop();
Serial.println("client disonnected");
} //if client
}