#include "thingProperties.h"
#include <TinyGPS++.h>
TinyGPSPlus gps;
void setup() {
// Initialize serial and wait for port to open:
Serial.begin(9600);// Serial monitor
Serial1.begin(9600); // Hardware serial for GPS communication
// This delay gives the chance to wait for a Serial Monitor without blocking if none is found
delay(1500);
// Defined in thingProperties.h
initProperties();
// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
/*
The following function allows you to obtain more information
related to the state of network and IoT Cloud connection and errors
the higher number the more granular information you’ll get.
The default is 0 (only errors).
Maximum is 4
*/
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
}
void loop() {
ArduinoCloud.update();
// Your code here
// Check if data is available from the GPS module
while (Serial1.available() > 0) {
gps.encode(Serial1.read()); // Feed the GPS data to the TinyGPS++ library
// If a valid GPS fix is obtained (location updated), get the coordinates
if (gps.location.isUpdated()) {
// Update latitude and longitude cloud variables
latitude = gps.location.lat();
longitude = gps.location.lng();
Serial.print("Latitude: ");
Serial.println(latitude);
Serial.print("Longitude: ");
Serial.println(longitude);
}
}
}
/*
Since Latitude is READ_WRITE variable, onLatitudeChange() is
executed every time a new value is received from IoT Cloud.
*/
void onLatitudeChange() {
// Add your code here to act upon Latitude change
latitude = gps.location.lat();
}
/*
Since Longitude is READ_WRITE variable, onLongitudeChange() is
executed every time a new value is received from IoT Cloud.
*/
void onLongitudeChange() {
// Add your code here to act upon Longitude change
longitude = gps.location.lng();
}
/*
Since Location is READ_WRITE variable, onLocationChange() is
executed every time a new value is received from IoT Cloud.
*/
void onLocationChange() {
// Add your code here to act upon Location change
location =Location (latitude,longitude);
}