Hardware Reference
In-Depth Information
After the while() block, the ther-
mostat check block from the
original sketch closes out the loop()
method.
8
Continued from previous page.
// give the client time to receive the data:
delay(1);
// close the connection:
Serial.println(F("Closing the connection"));
client.stop();
} // close of the if (client.available() block
} // close of the while (client.connected() block
} // close of the if (client) block
// NOTE: the thermostat check (the body of the loop() method on
// page 374) goes here,
}
8
The sendFile() method needs to
change so you can send to the client
instead of printing it out. It also needs
to send the HTTP headers that come
before the file. If the file isn't available,
you should tell the client that, too. And
you might as well use the standard
HTTP error codes to do it. So now
sendFile() sends an HTTP 200 OK
header if it can read the file, and an
HTTP 404 File Not Found header if it
can't. Changes are shown in blue.
// send the file that was requested:
void sendFile(Client thisClient, char thisFile[]) {
String outputString = ""; // a String to get each line of the file
// open the file for reading:
File myFile = SD.open(thisFile);
if (myFile) {
// send an OK header:
sendHttpHeader(thisClient, 200);
// read from the file until there's nothing else in it:
while (myFile.available()) {
// add the current char to the output string:
char thisChar = myFile.read();
outputString += thisChar;
// when you get a newline, send out and clear outputString:
if (thisChar == '\n') {
thisClient.print(outputString);
outputString = "";
}
}
// if the file does not end with a newline, send the last line:
if (outputString != "") {
thisClient.print(outputString);
}
// close the file:
myFile.close();
}
else {
// if the file didn't open:
sendHttpHeader(thisClient, 404);
}
}
 
Search WWH ::




Custom Search