Building Arduino Projects for the Internet of Things

(Steven Felgate) #1

CHAPTER 3 ■ COMMUNICATION PROTOCOLS


Listing 3-5. Code for Standard Arduino Functions—loop()


void loop()
{
if (client.available())
{
Serial.println("[INFO] HTTP Response");
}


// Read available incoming bytes from the server and print
while (client.available())
{
char c = client.read();
Serial.write(c);
}


// If the server:port has disconnected, then stop the client
if (!client.connected())
{
Serial.println();
Serial.println("[INFO] Disconnecting From Server");
client.stop();
}
}


Your Arduino code is complete.

Final Product


To test the application, verify and upload the code as discussed in Chapter 1. Once the
code has been uploaded, open the Serial Monitor window. You will start seeing log
messages similar to ones shown in Figure  3-2 for HTTP GET and in Figure  3-3 for HTTP
POST.

Free download pdf