Sending current location of estimote on click and automatically daily

Hi I am trying to code for sending location on click and recurring daily but there is a lag.

Following is my code.

io.press(() => {
  print("Button pressed, sending location now");
  location.startUpdates((position) => {
    cloud.enqueue('btn-pos', {
      lat: position.lat,
      long: position.long,
      at: new Date()
    });
    sync.now();
  }, {
    minInterval: 3600,
    timeout: 0
  });

  print("Location sent, now registering back to normal routine");
  sync.setSyncPeriod(3600);
  location.startUpdates((position) => {
    cloud.enqueue('auto-pos', {
      lat: position.lat,
      long: position.long,
      at: new Date()
    });
    sync.now();
  }, {
    minInterval: 3600,
    timeout: 0
  });


});

Two things that jump out:

  • in your on-press handler, you never actually stop the updates, so you’ll get another position in ~ 3600 seconds (as specified by the minInterval), and send another btn-pos event (even if there was no new button press)

  • your “normal routine” calls startUpdates again, which actually overwrites your previous startUpdates, so with this code, I don’t think you’ll be getting any btn-pos events

I think to begin with, I’d try doing something like this:

io.press(() => {
  // send location on button press
});

timers.repeat(3600, () => {
  // send location every hour (3600 seconds);
});

… and in both of the location updates callbacks, remember to stopUpdates:

location.startUpdates((position) => {
  cloud.enqueue('...', { // both btn-pos and auto-pos
    lat: position.lat,
    long: position.long,
    at: new Date()
  });
  sync.now();
  location.stop(); // <=========
// ...

This still doesn’t prevent the button-press-initiated and the timer-initiated location updates from overlapping and interfering with each other (a race condition of sorts), but it’s a start and should work most of the time (: And then you can work on making it more resilient.

On another note, it might also be helpful for you to read something about promises and asynchronous programming in JavaScript, because this:

location.startUpdates(
  // ...
);
print("Location sent, now registering back to normal routine");

… doesn’t actually work this way. startUpdates will return immediately, and just schedule the updates to happen in the future, and then your app will immediately move to the next instruction, the print. So actually, you’ll get “Location sent” message in your console, but that’s not gonna be true, because the location updates didn’t necessarily happen yet.

startUpdates returns a thing called a promise, which you can use to actually schedule some code to run when updates do stop:

var promise = location.startUpdates(
  // ...
);
promise.then(() => {
  print("Location sent, now registering back to normal routine");
});

// or shorter:
location.startUpdates(
  // ...
).then(() => {
  print("Location sent, now registering back to normal routine");
);

Thanks a lot for the suggestions and recommendations. I have now updated my piece of code. Any suggestions to improve it?

io.press(() => {
  print("Sending position via button press");
   location.startUpdates((position) => {
    cloud.enqueue('btn-pos', {
      lat: position.lat,
      long: position.long,
      at: new Date()
    });
   sync.now();
   location.stopUpdates(); 
  });
  
});

timers.repeat(1000000, () => {
  print("via timer");
  location.startUpdates((position) => {
    cloud.enqueue('scheduled-pos', {
      lat: position.lat,
      long: position.long,
      at: new Date()
    });
    sync.now();
    location.stopUpdates(); 
  });
});

And I have a question, with this methodology, my device storage is exceeding. I see location.stopUpdates() doesn’t stop the location updates making my device out of storage.

My use case is to click a button and send the current location only and then stop. So it should be like pushing the button and sharing the current location and bingo. Close.

The other use case along with the above one is, that my device automatically share the current location at the time of sending, daily.

Method that stops updates is called location.stop() not location.stopUpdates(). See https://developer.estimote.com/lte-beacon/micro-app-api-reference/#stop
You should receive message in web console about undefined function, something like TypeError: Expected a function.

1 Like

Ooops, that’s on me, sorry about that @Bilal_Aurangzeb!

I updated my post above to location.stop(), for any future readers ^_^