Imp.onidle()

Hello,

Could somebody help me with this question please?

If I run imp.sleep(.01) in a sensor reading function, would that also trigger imp.onidle()?
I also have an imp.sleep(1) inside my loop to intentionally trigger imp.onidle() every 10 secs. But I don’t want imp.onidle to be triggered when imp.sleep(.01) runs in my sensor reading function.

imp.onidle(function() {
loop();
server.sleepfor(10);
});

loop()
{
// doing something…
imp.sleep(1);
}

Thank you

No, as imp.sleep() is a busy sleep. onidle triggers when you fall back to the OS level (no code is running) and there are no event handlers to run.

If you want to do something every 10 seconds, you should do this:

`function dosomething() {
// request the impOS calls this function in 10 seconds from now
imp.wakeup(10, dosomething);

// do the thing
}

// start the loop of dosomething’s; only call this once!
dosomething();`

…note that you can have any number of these on the imp side, only limited by resources, to have different periodic tasks running in what looks like a concurrent fashion.

Thank you Hugo for your response.
Actually what I’m trying to do is to run my code every 10 mins. then turn off wifi to save battery power using the server.sleepfor() function, do you have some code sample that uses server.sleepfor()?

Have a look here:
http://electricimp.com/docs/api/server/sleepfor/
…and here:
http://electricimp.com/docs/api/imp/deepsleepfor/

Thank you