Agent Code to Check if Imp Device is "offline"


I would like to send an SMS message from the Agent via Twilio when the associated Imp goes offline. Is there an Agent function to retrieve the current device status?


You will be able to register event handlers for imp online/offline, but this feature doesn’t exist as yet.

Right now you could do something like this, that sets a timeout then asks the imp to reply. If it doesn’t reply, the timeout handler will be called and imponline will still be false.

imponline <- false;
imp.wakeup(5,function(){if (!imponline) send_sms()});



Thanks Hugo, this will work.


I have a follow-up question on this (I am still trying to wrap my mind around the model/device concept).

Let’s say I have two devices (“temp_reader:MainStreet” and “temp_reader:OakStreet”). The two devices share the model (both Device and Agent code). Now the Imp at MainStreet goes offline. How does the Agent know which Imp did not reply?

Thanks again,


Each device gets its own instance of the agent code: the Main Street and Oak Street agents run independently.



Is the corresponding device name currently available via API in the Agent so I don’t have to hard-code it?


Not right now, no. In a production environment you’d tend to use the atsha ID to identify a device against a database vs a human entered name within the imp IDE (though I can see how this would be useful for named prototypes during development)


I would be very useful. Other users are asking for the device name and the agent URL here.


This is a solution for me to get messages if and Imp goes offline. However, it doesn’t appear that this “ping” snippet of code checks repeatedly?

Do I need to device.send the ping with a wakeup handler to make it ping every hour for example or am I misunderstanding the code?

imponline <- false;
imp.wakeup(5,function(){if (!imponline) send_sms()});



“6 years later” now that’s an old thread!

So, you don’t need to be doing this in the general case. The imp system takes care of this, and of calling the ondisconnect handler registered in the agent when the device fails to respond to pings and is marked offline. On average, a wifi/ethernet device will be marked offline when it has not been seen for ~4.5 minutes (max is ~9 minutes) - cellular is longer.

Here’s some recommended agent code, which waits 30s after a disconnect to check that it wasn’t triggered by a device reconnect - if a connection is determined to have gone stale, the device will reconnect which will cause a disconnect closely followed by a connect.

disconnect_timer <- null;

function definitely_offline() {

device.ondisconnect(function() {
  // Set a timer for 30s; if it isn't cancelled by a subsequent connect, then
  // it'll call "definitely_offline()"
  disconnect_timer = imp.wakeup(30, definitely_offline, "offlinetimer");

device.onconnect(function() {
  // Cancel a disconnect_timer if there's one running
  if (disconnect_timer != null) {
    disconnect_timer = null;


Thanks. I thought there might be a better implemented since then :wink: