Hardware Reference
In-Depth Information
Change your main loop as follows,
adding the lines shown in blue
after Serial.write(thisChar) .
8
void loop()
{
// listen for incoming clients
Client client = server.available();
if (client) {
Serial.println("Got a client");
String requestLine = "";
while (client.connected()) {
if (client.available()) {
char thisChar = client.read();
// if you get a linefeed and the request line is blank,
// then the request is over:
if (thisChar == '\n' && lineLength < 1) {
// send a standard http response header
makeResponse(client);
break;
}
//if you get a newline or carriage return,
// you're at the end of a line:
if (thisChar == '\n' || thisChar == '\r') {
lineLength = 0;
}
else {
// for any other character, increment the line length:
lineLength++;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
}
}
8
Before you can run this, you'll need
to add a method , makeResponse() , at
the end of your sketch to make a String
to send the client. Here's a start.
void makeResponse(Client thisClient) {
thisClient.print("HTTP/1.1 200 OK\n");
thisClient.print("Content-Type: text/html\n\n");
thisClient.print("Hello from Arduino</head><body>\n");
thisClient.println("</body></html>\n");
}
When you enter the Arduino's address in your
browser now, you'll get a web page. There's
not much there, but it's legitimate enough that
your browser doesn't know the difference between your
Arduino and any other web server. You can add anything
you want in the HTML in the makeResponse() method—
even links to images and content on other servers. Think
of the Arduino as a portal to any other content you want
the user to see, whether it's physical data from sensors or
other web-based data. The complexity is really up to you.
Once you start thinking creatively with the capabilities of
the print() and println() statements, you get a wide range
of ways to dynamically generate a web interface to your
Arduino through the Ethernet shield.
X
 
Search WWH ::




Custom Search