I register for device.ondisconnect but it only seems to be called once the imp is powered back on. So i have my imp connected powered via USB port and everything works well. I then remove the power cable but i don’t see the event being called on the agent. When i power the board back on i see the disconnect event followed by the power one events.
Tue Sep 03 2013 22:40:51 GMT+1000 (EST): Device has disconnected Tue Sep 03 2013 22:40:51 GMT+1000 (EST): Device booting Tue Sep 03 2013 22:40:51 GMT+1000 (EST): Device configured to be "First Program"
Any ideas on what i may be doing wrong?
When you pull the power cable, the imp doesn’t get a chance to tell the server that it’s going away. So the server may take a little while to notice that the imp isn’t there any more (it thinks the imp just hasn’t got anything to say to it). Once the server has noticed, which may take up to ten minutes, device.ondisconnect will get called.
Makes sense and i guess this will also pertain to calling device.isconnected from the agent.
Is there any other mechanism to detect a powered down or disconnected device in a more timely fashion.
The only way is by polling the device from the agent, and looking for a timely reply. The problem is that an idle TCP connection looks identical to an unplugged imp, that’s just the way TCP works.
Would it better for the imp to send a heartbeat at a configurable period.
It does already, but the period is not user-configurable.
Can you try powering the imp with a capacitor across the power leads (which will delay how long it takes to drain the power and shut down) and have a voltage compare watching for a drop in the power supply that will give time to send out your signal?
It would have to be a big Cap. It’s better to send your own heartbeat from the Agent and expect a response within 1 second.