Skip to main content

How To Disconnect And/or Deep-sleep Devices Correctly

Make Sure impOS Has Sufficient Leeway To Process Messages, Squirrel/OS Updates Etc.

Whenever your code puts your imp into deep sleep, or disconnects from the server manually, it’s vital that you first allow impOS™ sufficient opportunity to deal with pending messages from the server. These messages include not only communication between the device and its agent, but also signal important system changes, such as impOS updates and the availability of fresh Squirrel code.

Allowing impOS to process these messages is essential. A pending impOS release may contain crucial security updates that are vital to keeping your product and your end-users’ data safe. New Squirrel may contain important bug fixes that you need to deploy — you don’t want your existing code to block the installation of these fixes.

Even relatively less crucial messages, such as agent-device communications, may contain information which prompts your application to step back from going into deep sleep or disconnecting at this time.

It is very easy to allow impOS scope to process these messages before disconnection or deep sleep: just ensure that every call to any of the relevant imp API methods, listed below, take place within a function registered with imp.onidle(). This function is guaranteed not to be executed until impOS has performed any necessary message-processing tasks and is ready for disconnection or deep sleep. The examples below demonstrate this.

Note Using imp.wakeup() as an alternative to imp.onidle() is not recommended in this case, for example:

imp.wakeup(0, function() {
    server.flush(10);
    imp.deepsleepfor(3600);
});

This is because any timeout provided can be no guarantee that the imp has become idle at that point. Using imp.onidle() is on the only way to be sure.

Sample Code

API Methods Used

Further Reading