Hardware Reference
In-Depth Information
Now, in the loop() function of the sketch, we will actually connect to the server. It
starts by calling the connect function and checks whether we are indeed connected. If
that's the case, we print it out on the Serial monitor for debugging purposes:
if (client.connect(server, 80)) {
if (client.connected()) {
Serial.println("connected");
Now that we are connected, we can set the GET request for the test page we want to ac-
cess:
client.println("GET /java/host/test.html HTTP/1.1");
client.println("Host: www.brainjar.com");
client.println("Connection: close");
client.println();
After the request is sent, we will read the data that is coming back from the server, to
check whether everything went fine. We will also print out this data on the Serial mon-
itor:
while (client.connected()) {
while (client.available()) {
char c = client.read();
Serial.print(c);
}
}
Finally, when we are sure that the client is not connected anymore, we will print the in-
formation on the Serial monitor and call the close() function on the Ethernet client:
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
}
Finally, we don't want to continuously do this action, but only repeat it every five seconds.
This is done with a delay() function:
delay(5000);
Search WWH ::




Custom Search