Publishing gps data to cloud (NEO6mv)

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?

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()


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:

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.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.lng(), light() );
1 Like

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?

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 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()

1 Like