Building Arduino Projects for the Internet of Things

(Steven Felgate) #1
CHAPTER 9 ■ IOT PATTERNS: LOCATION AWARE

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 final code section is provided in Listing 9-8. It implements Arduino’s standard
setup() and loop() functions.
The setup() function initializes the serial port. Note that the baud rate is 115200 ,
which is different from what you have been using so far. The reason for difference will be
clear when you look at the next line of code: ss.begin(9600). This statement initializes
communication with the GPS module on serial ports D2 and D3 (ss is the instance of
SoftwareSerial library that you initialized in Listing 9-6 ). The GPS module used in this
project communicates at 9600 baud rate by default, therefore 115200 was used for serial
monitor logs. The GPS module that you are using might have a different default baud rate,
so make sure to check the manufacturer’s datasheet to find the correct one. Next, connect
to the Internet using WiFi.
The loop() function just needs to call the getGPSCoordinates() function. It reads
the GPS coordinates and, at regular intervals, calls the transmitSensorData() function to
publish the GPS coordinates to the server.


Listing 9-8. Code for Standard Arduino Functions


void setup()
{
// Initialize serial port
Serial.begin(115200);


// Initialize serial port for GPS data
ss.begin(9600);


//Connect Arduino to internet
connectToInternet();
}

Free download pdf