// This #include statement was automatically added by the Particle IDE.
#include <AssetTracker.h>
#include <math.h>
// How many minutes between publishes? 10+ recommended for long-time continuous publishing!
int delayMinutes = 2;
// Used to keep track of the last time we published data
long lastPublish = 0;
// Creating an AssetTracker named 't' for us to reference
AssetTracker t = AssetTracker();
int isMoving = 0;
//Device number
char did[9] = "58965412";
float gpsPrevLat;
float gpsPrevLon;
int decimal = 100000;
// setup() and loop() are both required. setup() runs once when the device starts
// and is used for registering functions and variables and initializing things
void setup() {
// Sets up all the necessary AssetTracker bits
t.begin();
// Enable the GPS module. Defaults to off to save power.
// Takes 1.5s or so because of delays.
t.gpsOn();
gpsPrevLon = 0.0f;
gpsPrevLat = 0.0f;
// Opens up a Serial port so you can listen over USB
Serial.begin(9600);
Particle.function("gps", gpsPublish);
}
// loop() runs continuously
void loop() {
//variable to hold JSON data
char data[90];
// You'll need to run this every loop to capture the GPS output
t.updateGPS();
// if the current time - the last time we published is greater than your set delay...
if (millis()-lastPublish > delayMinutes*60*1000) {
// Remember when we published
lastPublish = millis();
//Particle.publish("A", pubAccel, 60, PRIVATE);
// GPS requires a "fix" on the satellites to give good data,
// so we should only publish data if there's a fix
if (t.gpsFix()) {
float gpsCurrLat = roundf(t.readLatDeg() * decimal)/decimal ;
float gpsCurrLon = roundf(t.readLonDeg() * decimal)/decimal ;
bool isMoved = hasMoved(gpsPrevLat, gpsPrevLon, gpsCurrLat, gpsCurrLon);
if(isMoved){
gpsPrevLat = gpsCurrLat;
gpsPrevLon = gpsCurrLon;
//creates a string in JSON structure
snprintf(data,sizeof(data), "{\"did\": %s, \"gpsLat\": %e, \"gpsLon\": %e, \"isMoving\" : %d}",did,gpsCurrLat,gpsCurrLon,isMoving);
// Short publish names save data!
Particle.publish("Data", data, 60, PRIVATE);
//try webhook and check if it worked
Particle.publish("update_gps",data, 60, PRIVATE);
}
// Short publish names save data!
//Particle.publish("G", t.readLatLon(), 60, PRIVATE);
// but always report the data over serial for local development
//Serial.println(t.readLatLon());
}
}
}
// Actively ask for a GPS reading if you're impatient. Only publishes if there's
// a GPS fix, otherwise returns '0'
int gpsPublish(String command) {
if (t.gpsFix()) {
Particle.publish("G", t.readLatLon(), 60, PRIVATE);
// uncomment next line if you want a manual publish to reset delay counter
// lastPublish = millis();
return 1;
} else {
return 0;
}
}
bool hasMoved(float gpsPrevLat, float gpsPrevLon, float gpsCurrLat, float gpsCurrLon){
float latDiff = fabsf(gpsPrevLat - gpsCurrLat);
float lonDiff = fabsf(gpsPrevLon - gpsCurrLon);
Particle.publish("LaD", String(latDiff), 60, PRIVATE);
Particle.publish("LoD", String(lonDiff), 60, PRIVATE);
if(latDiff > .001f || lonDiff > .001f){
Particle.publish("hM","true",60,PRIVATE);
return true;
}else{
Particle.publish("hM","false",60,PRIVATE);
return false;
}
}