Building Arduino Projects for the Internet of Things

(Steven Felgate) #1

CHAPTER 7 ■ IOT PATTERNS: ON-DEMAND CLIENTS


char c = client.read();
Serial.write(c);
}


if (millis() - lastConnectionTime > postingInterval)
{
client.stop();


Serial.println("[INFO] Connecting to Server");


String requestData = "parkingUpdate=" + updateParkingSpot;


// Prepare data or parameters that need to be posted to server
if (client.connect(server, port))
{
Serial.println("[INFO] Server Connected - HTTP GET Started");


// Make HTTP request:
client.println("GET /smartparking/update.php?" + requestData + "
HTTP/1.1");
client.println("Host: " + String(server));
client.println("Connection: close");
client.println();


lastConnectionTime = millis();


Serial.println("[INFO] HTTP GET Completed");
}
else
{
// Connection to server:port failed
Serial.println("[ERROR] Connection Failed");
}
}


Serial.println("-----------------------------------------------");
}


Standard Functions


The fifth and final code section is shown in Listing 7-8. It implements Arduino’s standard
setup() and loop() functions.
The setup() function initializes the serial port, sets the pin modes for the trigger and
echo pins, connects to the Internet, and calibrates the proximity sensor.
The loop() function needs to call readSensorData() at regular intervals as it
internally calls the publishSensorData() function.

Free download pdf