Can't get imp.cancelwakeup() to work

I am trying to get imp.cancelwakeup() to let me change the sample period of a loop as shown in the following code:

`samplePeriod <- 3600.0;
t_handle <-0;

function loop()
{
t_handle = imp.wakeup(samplePeriod, loop);
}

agent.on(“period”, function (value) {
imp.cancelwakeup (t_handle);
samplePeriod = value.tofloat();
server.log(format(“new samplePeriod = %f”, samplePeriod));
});
`
if I change the sample period by sending a new value via http (say 100) I get this in the log:

new samplePeriod = 100.000000

so the new value has been received - but the previous wakeup function does not get cancelled i.e. the sample period remains until the original 3600 seconds has transpired and only then does it change to 100 seconds - I want it to change immediately. Any ideas what I am doing wrong?

imp.cancelwakeup should remove the callback/timer from the stack immediately (ie - it will never execute the callback).

There’s a couple problems with your code:

  1. You don’t call loop() to get it started
  2. You don’t call loop() after you change the period in your agent.on callback.

Try adding a server.log() to your loop() function to see when it’s being called…

Here’s some code I wrote that’s working fine:

`p <- 5.0
t_handler <- null;

function loop() {
server.log("bump: " + p);
t_handler = imp.wakeup(p, loop);
} loop();

button <- hardware.pin7;
function buttonStateChange() {
imp.sleep(0.02);
if (button.read() == 0) {
if (p < 5) p = 5;
else p = 1;
imp.cancelwakeup(t_handler);
server.log("Changed p to: " + p);
loop();
}
}
button.configure(DIGITAL_IN_PULLUP, buttonStateChange);
`

It’s possible your imp hasn’t been upgraded to release 27 yet. What’s returned when you run the following line of code in your device window:

server.log(imp.getsoftwareversion())

If you’re on the latest release (ie - Release 27) it should return:

9af2fae - release-27.9 - Mon Nov 25 09:58:12 2013

Sorry - I forgot to add this. Yes it does return:

sw version = 9af2fae - release-27.9 - Mon Nov 25 09:58:12 2013

Many thanks. Calling loop from agent.on seems to fix things:
`agent.on(“period”, function (value) {
imp.cancelwakeup (t_handle);
samplePeriod = value.tofloat();
server.log(format(“new samplePeriod = %f”, samplePeriod));
loop();
});/code>