I’m working with an electron and working from an example using TinyGPS++ where they get the distance between two points. The examples with TinyGPS though use a fixed point, London, for one point and the current location pulled from the GPS. I’m attempting to capture a point and when I pull the next point 60 seconds later, I want to know the distance between the two points. My intent is that if the points are too close together that I will wait another 60 seconds and try again.
The coordinates that I am getting out seem to be right and I’m getting the same correct value at each iteration. But when I try to store the values to calculate distance, the values are changing significantly and the distanceBetween is returning many thousands of Kilometers. I think I am doing something simple wrong but I’m not spotting it, I’m hoping someone will spot it.
/*
Adapted from the example file "FullExample" included with the TinyGPS++ library
*/
#include <TinyGPS++/TinyGPS++.h>
// Change GPSBaud based on GPS unit
// 9600 for Adafruit's Ultimate GPS
static const uint32_t GPSBaud = 9600;
// The TinyGPS++ object
TinyGPSPlus gps;
char nullCoord[21];
// This custom version of delay() ensures that the gps object
// is being "fed".
static void smartDelay(unsigned long ms)
{
unsigned long start = millis();
do
{
while (Serial1.available())
gps.encode(Serial1.read());
} while (millis() - start < ms);
}
static void makeCoord(char *coord1, char *coord2, char *coords)
{
strcpy(coords,coord1);
strcat(coords,",");
strcat(coords,coord2);
}
void setup()
{
// Begin serial at 115200 Baud
Serial.begin(115200);
Serial.println(F("Hello world!"));
// Serial1 reads from the Electron's TX/RX
Serial1.begin(GPSBaud);
// Make the 0 coordinate
makeCoord("0.000000", "0.000000", nullCoord);
}
void loop()
{
// Now we prepare to publish location data to our webhook
// Make strings of the Latitude and Longitude readings
char coord1[10];
sprintf(coord1, "%f", (gps.location.lat(), gps.location.isValid(), 11, 6));
char coord2[10];
sprintf(coord2, "%f", (gps.location.lng(), gps.location.isValid(), 12, 6));
char speed[32];
sprintf(speed, "%ld", (gps.speed.mph(), gps.location.isValid()));
char course[32];
sprintf(course, "%ld", (gps.course.deg(), gps.location.isValid()));
// Combine these strings into the coordinate format accepted by Initial State
char curCoord[21];
makeCoord(coord1, coord2, curCoord);
// Send the data
if (strcmp(curCoord, nullCoord) != 0){
// Particle.publish("gpsData",curCoord);
}
// Save to last position variables
char lastCoord[21];
strcpy(lastCoord, curCoord);
double lastLat;
lastLat = gps.location.lat();
double lastLong;
lastLong = gps.location.lng();
double currLat;
double currLong;
char longchar[150];
sprintf(longchar, "last lat %d, last lng %d", lastLat, lastLong);
Serial.println(longchar);
// Wait 60 seconds
do {
smartDelay(60000);
currLat = gps.location.lat();
currLong = gps.location.lng();
sprintf(longchar, "GPS Lat %d, lng %d",currLat, currLong);
Serial.println(longchar);
Serial.println(TinyGPSPlus::distanceBetween(currLat, currLong, lastLat, lastLong));
} while (TinyGPSPlus::distanceBetween(currLat, currLong, lastLat, lastLong) < 100);
if (millis() > 5000 && gps.charsProcessed() < 10)
{
Serial.println(F("No GPS data received: check wiring"));
Particle.publish("gpsData",nullCoord);
}
}