Hardware Reference
In-Depth Information
If a connection is detected, we create this client, and print it out on the Serial port for
debugging purposes, as shown in the following code:
EthernetClient client = server.available();
if (client) {
Serial.println("New client");
The next part is a bit technical. We need to read out the request that comes from the client,
but also detect the moment when the request is over so that we can close the connection.
This is handled by always checking whether the current line is blank or not. The first step
is then to read out the request that comes from the client, character per character, using the
following piece of code:
boolean currentLineIsBlank = true;
while (client.connected()) {
// Read data
if (client.available()) {
char c = client.read();
Serial.write(c);
Note that the incoming request is also printed out on the Serial port for debugging pur-
poses. Now, if we detect that we have an end of line character and that the current line is
blank, it's a sign that the request is over, as shown in the following line:
if (c == '\n' && currentLineIsBlank) {
Therefore, we can answer the client. We will first send a standard HTTP header that states
everything went OK, and that we want to refresh the page automatically every 5 seconds,
as shown in the following code:
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println("Refresh: 5"); // Refresh the page
automatically every 5 sec
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
Search WWH ::




Custom Search