Electric Imp: Faster device.isconnected() function

The usage

The device.isconnected() function returns a true or false to tell if the device/impee is connected.

The problem

If it is important you know if the impee is online and ready to receive commands, or is ready to send something right now, the build in function might give you problems. The problem is that the build in function first returns false when the server tried to contact the impee and it has timed out. This can take multiple minutes, but with a simple function you can make your own which is a lot faster.

Some code


devicePing <- 0;
device.on("ping", function(msg)
  devicePing = time();

function deviceOnline()
  if (time() - devicePing >= 5) return false;
  return true;


function ping()
  agent.send("ping", "");
  imp.wakeup(1, ping);

How to use

This code will make the impee send a “ping” message to the agent once every second. The agent will then save the time it received the message on.

To check if the device is online you now call deviceOnline(); instead of device.isconnected(); and it will return true or false, depending on if the agent received the ping message within the last 5 seconds or not. This way the device will only be offline for up to 5 seconds before the function will return false. The 5 seconds can most likely be set to something lower too, but it is plenty for what I use it for.

Leave a Comment