How to use GPS API?

I have 2 beacons(dev kit) one with Hologram sim card you provided and another with local 3 mobile sim card.Both are not sending signals 90% of the time after out of bluetooth range.(Cell radio config app) I tried using LTE only LTE_Mobile function setRadioTech() {lte.setTech(lte.Tech.GSM_LTE)} or LTEor GSM but it only gets worst when not using the default. Some times they work as it should but mostly they don’t.They are tested outside when I am going to work and back. The only difference is that some times 3 mobile is a bit faster.

this is my mini app:

var DESTINATION = {lat: 55.669274, long: 12.452558};
var SYNC_PERIOD = 12600;
sync.setSyncPeriod(SYNC_PERIOD);

print('Sync period set');

// location.startUpdates((position) => {
//   cloud.enqueue('position-change', { lat: position.lat, long: position.long, at: new Date() });
// }, { minInterval: 12600});
// print('sync set to 12600s, 600 interval');

var lastKnownLocation = null;

timers.repeat('4 hours', () => {
  location.startUpdates((location) => {
    // fix found
    lastKnownLocation = location;
    location.stop();
  });
});
print(lastKnownLocation)

io.press(() => {
    sync.setSyncPeriod(300);
    sync.now();
    print('ALARM MODE');
    
    // location.startUpdates((position) => {
    //   cloud.enqueue('position-change', { lat: position.lat, long: position.long, at: new Date() });
    // }, { minInterval: 240, minDistance: 1, timeout: 0});
    // location.stop();
    timers.repeat('5 m', () => {
  location.startUpdates((location) => {
    // fix found
    lastKnownLocation = location;
    location.stop();
  });
});
    print('cordinates are send to cloud');
      
});

cloud.onReceive( msg => {
 if(msg.type === 'stolen') {
  sync.setSyncPeriod(msg.payload.sync_period);}
  
}
);
cloud.onReceive( msg => {
 if(msg.type === 'alarm') {
  sync.setSyncPeriod(msg.payload.sync_period);}
  
});

Can anyone tell me what I am doing wrong?

  1. Code that actually collects data to be sent to Cloud is commented out, so I don’t know if this might be a problem.
  2. print(lastKnownLocation) will always be print null because it is called in main code block which is run once when application is installed, but is updated in handler functions that will be run later. (check this tip).
  3. If I consider commented code in io.press block: calling location.startUpdates in timer will override callback of previously called location.startUpdates after 5min (docs). If device won’t get GPS fix in those 5min, new callback will only update lastKnownLocation, but will not enqueue any data.
  4. If I consider commented code in main block: It only enqueues position. And since sync period is set to 3.5 hours you will have to wait for a long time before you receive anything.
  5. Using at: new Date() in cloud.enqueue is redundant, because every event is already timestamped
  6. Calling cloud.onReceive two times in a row will make second call override previous one (see docs).
  7. You mention using lte.setTech, but I don’t see that code there.
1 Like

Thanks Lukasz, I removed the commented and useless fields out. Does this look ok?

var DESTINATION = {lat: 55.669274, long: 12.452558};
var SYNC_PERIOD = 12600;
sync.setSyncPeriod(SYNC_PERIOD);
var lastKnownLocation = null;
print('Sync period set');

// setting 2,3,4G
lte.setTech(lte.Tech.GSM_LTE, lte.Region.EUROPE)

.catch((error) => {
print("error");
     cloud.enqueue('lte-tech', {error: error});
     sync.now(); });
     
// updating location every 4 hours conserving battery 
location.startUpdates((position) => {
      cloud.enqueue('position-change', { lat: position.lat, long: position.long});
    }, { minInterval: SYNC_PERIOD, minDistance: 1, timeout: 0});
    location.stop();
    
// button turns alarm mode 
io.press(() => {
    sync.setSyncPeriod(300);
    sync.now();
    print('ALARM MODE');
    
    location.startUpdates((position) => {
      cloud.enqueue('position-change', { lat: position.lat, long: position.long});
    }, { minInterval: 300, minDistance: 1, timeout: 0});
    location.stop();
    print('cordinates are send to cloud');
      
});

// alarm mode activated from distance 
cloud.onReceive( msg => {
 if(msg.type === 'stolen') {
  sync.setSyncPeriod(msg.payload.sync_period);}
  
}
);

No, it doesn’t. I see that you are calling location.startUpdates and then next line location.stop() (two times in the code) so it basically stops GPS immediately after it is started.

1 Like

I was getting gps updates couple of times per turn every 2 seconds before that even when I was seting the sync period to 4 hours and 5 minutes based on the mode . Maybe it is something with ‘timeout’ key value pair. So I wanted to make sure the beacon is not sending the data more than once because of the battery, I guess this is not the way to go. I will try again without stoping the gps. I’m going to change the battery,box and antena but before that I want to make sure it is performing at its best.

everithing look ok with the gps from the last test. Thank you.