Thoughts on feedback to android when imp01 device is offline, or no wifi available

as per the title, i am asking for members views on what whould be an acceptable way to alert a android phone that the device is not responding or is not powered up or no wifi. I have currently an imp01 with a simple setup, a led on pin1 and a led on pin2. I have a very basic app created in appinventor on my android phone, (galaxy note2), that when i press a button , it sends to agent to turn on led1, on pin1 on the imp, press again turn off led1, press another button turn on led2, press again turn off led 2, fine that works ok, as this is all in front of me working. I have the agent sending back to my phone the time that the event was triggered as well, so when i press the buttons on the phone i get back " Red led turned on at 09:42.27 ", the time created from the agent, regardless of whether the device actually was online or not. included in my code was server send, so i can see on the console the agent time sent, and can see if the device is online or not in the IDE, but from the user of the android the feedback is really only confirming that i made contact with the agent, not the device. What could i use, back from the device as well to my app, i thought about maybe using a pin to read a ADC value and send that to my phone, or maybe create a random number and send that, maybe something else, not sure. If i replaced the leds with a relay, and was say opening /closing a door striker, there would always be a niggling worry, did the door actually close or open. The workaround may be so simple, however i am working from a very limited knowledge of the Imp and the agent/imp relationship, everthing i have achieved upto now is from example code in the docs and little snippets from the forum, learning as i go. Im not after anyone writing code for me, more pointers, i want to learn as much as i can by myself.
Thanks for any replies, TOM.

In my impClock project I call device.isconnected() and add a value to the string the agent sends back in response to a request from the app. There’s no easy way to signal the app directly, so the app has to poll, but I find it’s enough to get a ‘sorry, your device is offline’ in response to an attempt to change a setting or trigger an action.

A better approach might be to deliver the UI not as an Android app but as a web app hosted by the agent itself (it is a web server, after all). Here’s some sample code to show you how you can set up the agent to serve the UI. It’s then just a matter of coding the page to auto-poll the agent for device status.

Or, just send the confirmation of “red led on” direct from the device, ie:

app sends message to agent asking for red led on; request is not replied to in the http handler
agent sends message to device, sets a timer to reply to the request if a reply is not received
device performs the action, sends confirmation to the agent
agent replies to the app with confirmation
…or, the timer fires and a no-confirmation reply is sent to the app.

This then gives you actual confirmation of the action. It’s still possible that the action happened, and you weren’t told about it (if the link from the device to the agent fails immediately after the action has been taken) but that’s then fairly unlikely.

Something like this: (untested!)

agent:
`
failsafetimer <- null;
pendingreply <- null;
http.onrequest(function(request, response) {
// parse request here
if (red led should be on) {
// Remember reply object
pendingreply = response;

// Send to device
device.send("redled", 1);

// Set reply timer; cancel any already pending one
if (failsafetimer != null) imp.cancelwakeup(failsafetimer);
failsafetimer = imp.wakeup(5, function() {
  failsafetimer = null;

  // Tell app that device didn't confirm within 5s
  pendingreply.send(200, "Device not online; action not taken");
  pendingreply = null;
});

}
});

device.on(“redledset”, function(v) {
// Cancel failsafe timer as we got a response
if (failsafetimer != null) imp.cancelwakeup(failsafetimer);
failsafetimer = null;

// Send reply, if we aren’t too late (timeout may have fired)
if (pendingreply != null) {
pendingreply.send(200,"Device now in state "+v);
pendingreply=null;
}
});
`

device:
agent.on("redled", function(v) { // Set LED hardware.pin1.set(v); server.log("set pin, sending reply"); agent.send("redledset", v); });

Note that this simple example can only deal with a single request “in flight” at once. This can be extended to have a array of outstanding requests, etc. You can also use the MessageManager to do all the heavy lifting agent<>device, and just rely on the device having ACKed the message to confirm the action has been performed.