Publishing gps data to cloud (NEO6mv)


#1

For the following code I am implementing a light sensor and a gps. The light sensor works fine, but I’m having trouble with the gps. It can’t seem to get the long and lat values for some reason…? (It just returns invalid) When I use the example code from the tinygps library though, the correct data is sent via serial to putty. Any ideas? Thank you so much in advance!


PS Also, is there a way to send the long and lat values in one publish command rather than two?


#2

The GPS device continously sends data via the serial connection to the microcontroller but your code needs to read that data and feed it into the library via gps.encode() for the library to parse it. I can’t see your code doing any of that.

Yes, the common way to wrap multiple variables into one string goes via snprintf()


#3

You may want to start with one of the example files in the Library that you’re using to get started, and add your Light Sensor to it:
https://build.particle.io/libs/AssetTrackerRK/0.1.7/tab/example/1_SimpleGPS.cpp

It makes use of snprintf(), as ScruffR suggests:

char buf[128];
if (gps.location.isValid() && gps.location.age() < MAX_GPS_AGE_MS) {
  snprintf(buf, sizeof(buf), "%f,%f,%f", gps.location.lat(), gps.location.lng(), gps.altitude.meters() );
  Particle.publish("gps", buf);
}

You can swap your light() variable for gps.altitude, but remember to change the 3’rd (%f) to (%i) in the snprintf.
Something like:

snprintf(buf, sizeof(buf), "%f, %f, %i", gps.location.lat(), gps.location.lng(), light() );

#4

This is the gps code I have that works for when my electron is connected to my computer via USB. I’d like for my gps to work solely via battery (no USB connection) though, so I thought I would need to get rid of the serial prints/reads. If that is the case, how would I get the gps data (from gps.encode()) without having a usb connection?


#5

Serial1 has nothing to do with USB but is the way the controller and the GPS module communicate - you need to keep that.
And even if there is no connection to your computer, the Serial.xxx() functions won’t interfere - so just leave them there.
Don’t confuse/conflate Serial and Serial1 - they are two different interfaces.

But to get the data off of your device into the world, then you may want to look into the docs regarding Particle.publish()