Ok - it turns out I was incorrect in what I said before. Regardless of your code - if you have not set server.setsendtimeoutpolicy(RETURN_ON_ERROR, …) your code will stop executing.
Here’s the bit from the documentation you really need to care about:
If SUSPEND_ON_ERROR is in effect (this is the default behavior), the imp will stop executing code, attempt to reconnect to the server for 1 minute, then go to sleep. Every 9 minutes the imp will wake up and try to connect for 1 minute, and then go back to sleep if it cannot establish a connection.
If RETURN_ON_ERROR is in effect, the call will return a send error code to Squirrel and code will continue to run. The onunexpecteddisconnect callback, if any, will be called.
If RETURN_ON_ERROR is in effect, the only way for your imp to reconnect is to explicitly call server.connect() – so a common pattern is to make that call in the unexpected-disconnect callback.
Edit: Here is some sample code to show you the basics:
`hardware.pin9.configure(DIGITAL_OUT);
state ← 1;
blinking ← true;
function blink() {
if (blinking) {
state = 1-state;
hardware.pin9.write(state);
}
imp.wakeup(1.0, blink);
}
blink();
function connect() {
// if we’re connected, drop out of loop
if(server.isconnected()) return;
server.connect(function(result) {
// log that we connected I guess
blinking = true;
server.log("reconnected");
}, 30);
// call wakeup loop again in 30 seconds
imp.wakeup(30, connect);
}
server.setsendtimeoutpolicy(RETURN_ON_ERROR, WAIT_TIL_SENT, 30);
server.onunexpecteddisconnect(function(reason) {
// stop blinking
hardware.pin9.write(0);
blinking = false;
// start connection loop
connect();
});`
That code will blink while the device is connected, stop blinking when it disconnects, and start blinking again when it reconnects. You could easily have the “program continue” (ie - the blinking not stop) when disconnecting by removing the blinking=false; statement in server.onunexpecteddisconnect.