Asset Tracker V2: How do I obtain GPS data once during startup? [SOLVED]

Hi All.

How do I get one shot GPS location data during startup?

Here are my initial thoughts - see below code:

void setup() {

  // Enable the Asset Trackers V2 GPS module. Defaults to off to save power.
  // Takes 1.5 or so seconds because of delays.


  if (waitFor(tr.gpsFix(),10*60*1000UL)) { // Timeout is 10 minutes

    // Read GPS data once - Lat, Long, and GPS accuracy
    float Lat = tr.readLatDeg();
    float Lng = tr.readLonDeg();
    float gpsAcc = tr.getGpsAccuracy();

    // Failed to read GPS data 
    if ( isnan(Lat) || isnan(Lng) || isnan(gpsAcc) ) { 
       Particle.publish("intialchecks","Failed to read GPS data!",PRIVATE);


    // Turn off GPS

    Particle.publish("intialchecks","I read GPS data successful... lets keep going!",PRIVATE);

// Loop() runs over and over again
void loop() {
  // Core code goes here!

Any issue with this code?
I would say using setup() answer to your question.

@samb375, you might want to check the syntax for waitFor(). The function to wait on should not include the brackets. :wink:

Thank you for the suggestions. I’m presently working on another issue… Hope to share/post solution soon.

Still having trouble here… my challenge is: how do I capture GPS data strings fast enough without using the void loop(). See my above code for example.

Ideally, for my project, I would like to start the Electron, turn on the GPS, get GPS fix, get satellite data and turn off GPS then loop by reading the ADC, publish to Particle Cloud and sleep every minute. Also, check if it is nighttime:- go to deep sleep for 12 hours and start/reset from the beginning.

Any suggestions or help would be greatly appreciated. Thank you.

What’s wrong with running the code in the main loop? You can structure your code to accomplish what you want to do without trying to do it in the run once Setup() part of the code.

You could set create a GPSFix bool variable that is either True/False and once a valid GPS location has been received and saved you could set that GPSFix variable to TRUE.

Then in your main loop have an IF statement that will skip the get GPS location code if the GPSFix variable is TRUE and then move along to the other task you want to take care of before going to sleep.

I think you need to run the main loop to continually poll the data being returned from the GPS sensor to find out if you have a valid GPS fix or not.

Not sure if that makes sense or not :slight_smile: More than one way to accomplish your goals here.

1 Like

@RWB great suggestion. I also thought of using a counter, but your way is faster and more straightforward. I will try tonight and post results.

1 Like

Works like a charm. I also added an if statement in to check if my GPS accuracy is less than 6 meters. See below example code… hope this helps someone.

// Allows the GPS to find a fix / location
bool gpsFixCase = true;

// Enable the Asset Trackers V2 GPS module. Defaults to off to save power.
// Takes 1.5 or so seconds because of delays.

void loop() {

  // Get GPS fix and location - if false then turn-off GPS and stop looking
  if (gpsFixCase == true){

    // Capture the Asset Trackers V2 GPS output

    // GPS requires a "fix" on the satellites to give good data,
    // so we should only publish data if there's a fix.
    if (tr.gpsFix()) {

      // Make sure the GPS accuracy is less than 6 m, i.e. check if good GPS data
      if (tr.getGpsAccuracy() < 6.0 ){

        // Stop looking for GPS LAt, Lng and and accuracy i.e. set gpsFixCase to false
        gpsFixCase = false;

        // Turns off the GPS, and make sure it is turned off till next reset

        // Delay is to make sure GSP is off

        // Publish that GPS fix has been achieved
        Particle.publish("checks","GPS fix has been achieved. I'm going to shut down GPS!",PRIVATE);

  // Else - do this if gpsFixCase is false
   // Take a measurement and publish data to Particle Cloud
1 Like