Hardware Reference
In-Depth Information
How about printing the states
of the analog inputs? Add the
following to makeResponse() (new
lines are shown in blue).
8
void makeResponse(Client thisClient) {
thisClient.print("HTTP/1.1 200 OK\n");
thisClient.print("Content-Type: text/html\n\n");
thisClient.print("<html><head>");
thisClient.print("<title>Hello from Arduino</title></head><body>\n");
// output the value of each analog input pin
for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
thisClient.print("analog input ");
thisClient.print(analogChannel);
thisClient.print(" is ");
thisClient.print(analogRead(analogChannel));
thisClient.print("<br />\n");
}
thisClient.println("</body></html>\n");
}
Reload the page, and you've got
the states of the analog inputs. But
how about updating them continually?
You could use the methods you used
for the Cat Cam in Chapter 3. Change
the line in makeResponse() that prints
the HTML head, like the code shown
at right.
8
thisClient.print("Content-Type: text/html\n\n");
thisClient.print(
"<html><head><meta http-equiv=\"refresh\" content=\"3\">");
You can do all sorts of things in
the response. Now it's time to take
advantage of those three photocells
you attached to the analog inputs.
This version of the makeResponse()
method prints out a page that changes
its background color with the values
from the three photocells.
8
thisClient.print("Content-Type: text/html\n\n");
thisClient.print(
"<html><head><meta http-equiv=\"refresh\" content=\"3\">");
thisClient.print("<title>Hello from Arduino</title></head>");
// set up the body background color tag:
thisClient.print("<body bgcolor=#");
// read the three analog sensors:
int red = analogRead(A0)/4;
int green = analogRead(A1)/4;
int blue = analogRead(A2)/4;
// print them as one hexadecimal string:
thisClient.print(red, HEX);
thisClient.print(green, HEX);
thisClient.print(blue, HEX);
// close the tag:
thisClient.print(">");
// now print the color in the body of the HTML page:
thisClient.print("The color of the light on the Arduino is #");
thisClient.print(red, HEX);
thisClient.print(green, HEX);
thisClient.println(blue, HEX);
// close the page:
thisClient.println("</body></html>\n");
}
Now that you've got a light color server,
look for colorful places to put it.
 
Search WWH ::




Custom Search