Problem with the reconnection of the imp

I’ve been observing that when I execute a server.sleepfor or after a wifi loss, the imp can’t connect to the server.

My hypothesis about the wake issue would be that the withoutwifi routine could be intruding.
I’ve been testing where could be the error in the withoutwifi routine and I realized that never gets to execute the onConnected callback.

What could be happening?

`server.setsendtimeoutpolicy(RETURN_ON_ERROR, WAIT_TIL_SENT, 20)

function onConnected(){
LED_ON()//Turn on led to show this is working

function tryToConnect()
if (!server.isconnected()) {

    server.connect(onConnected, 15);//Connect
    imp.wakeup(50, tryToConnect);


function onUnexpectedDisconnect(status)
{ imp.wakeup(15, tryToConnect);


Your onConnected callback needs a parameter (and since you’re offline when it’s being executed, it’s not logging the runtime error). Here’s some basic code to get you started (note, in this example we set it up so that the onConnected callback is ALWAYS executed when we try to connect, even if we’re already connected):

`server.setsendtimeoutpolicy(RETURN_ON_ERROR, WAIT_TIL_SENT, 30);

function onConnectedCallback(state) {
// if we’re connected
if (state == SERVER_CONNECTED) {
// do something
} else {
// do something else?

function Connect(callback, timeout) {
// check if we’re connected before calling server.connect()
// to avoid race condition
if (server.isconnected()) {
// if we’re already connected execute the callback
} else {
// otherwise, proceed as normal
server.connect(callback, timeout);

server.onunexpecteddisconnect(function(reason) {
Connect(onConnectedCallback, 30);

You were right. There was a mistake on the code.

Moreover, I discovered the reason of the failure on reconnection after a sleep.

Since I declared a RETURN ON ERROR policy, the wakeup sends me to ACTIVE-OFFLINE state. Then I just need to do a explicit server.connect call to going to normal mode.