Hardware Reference
In-Depth Information
Just as you had a method earlier to
listen to serial data, now you're adding
one to listen to incoming HTTP client
requests. This method reads the bytes
and saves them in a string until it gets
a linefeed. It's not actually parsing the
lines, except to look for linefeeds or
carriage returns. Whenever it gets two
linefeeds in a row, it knows the HTTP
request is over, and it should respond
by calling the makeResponse() routine.
8
// this method listens to requests from an HTTP client:
void listenToClient( Client thisClient) {
while (thisClient.connected()) {
if (thisClient.available()) {
// read in a byte:
char thisChar = thisClient.read();
// for any other character, increment the line length:
requestLine = requestLine + thisChar;
// if you get a linefeed and the request line
// has only a newline in it, then the request is over:
if (thisChar == '\n' && requestLine.length() < 2) {
// send an HTTP response:
makeResponse(thisClient);
break;
}
//if you get a newline or carriage return,
// you're at the end of a line:
if (thisChar == '\n' || thisChar == '\r') {
// clear the last line:
requestLine = "";
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
thisClient.stop();
}
The makeResponse() method
sends an HTTP header and HTML
response to the client, then closes the
connection. It calls a method, getAver-
ageReading() , which gets the average
of all messages in the last 10 seconds
and converts them to a voltage:
8
void makeResponse(Client thisClient) {
// read the current average voltage:
float thisVoltage = getAverageReading();
// print the HTTP header:
thisClient.print("HTTP/1.1 200 OK\n");
thisClient.print("Content-Type: text/html\n\n");
// print the HTML document:
thisClient.print("<html><head><meta http-equiv=\"refresh\"
content=\"3\">");
thisClient.println("</head>");
// if the reading is good, print it:
if (thisVoltage > 0.0) {
thisClient.print("<h1>reading = ");
thisClient.print(thisVoltage);
thisClient.print(" volts</h1>");
} // if the reading is not good, print that:
else {
thisClient.print("<h1>No reading</h1>");
}
thisClient.println("</html>\n\n");
}
 
Search WWH ::




Custom Search