Server Connect Fails after DeepSleepFor

Hi - wonder if someone could help me with this please.

I’ve been having trouble contacting the server when my imp wakes from a deep sleep. My intention is to have the device poll hardware in low power mode, then send NV data to the server every 30 mins or so. I noticed that I couldn’t send my data reliably, and have stripped down the code in an effort to find the root cause. I’m left with this (which is butchered from the API docs):

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

function onConnectedCallback(state) {

//flashing means connecting
  ledPin.write(1);
  imp.sleep(0.3);
  ledPin.write(0);
  imp.sleep(0.3);
  ledPin.write(1);
  imp.sleep(0.3);
  ledPin.write(0);
  imp.sleep(0.3);
  ledPin.write(1);
  imp.sleep(0.3);
  ledPin.write(0);
  imp.sleep(0.3);
  ledPin.write(1);
  imp.sleep(0.3);
  ledPin.write(0);
  imp.sleep(0.3);
  ledPin.write(1);
  imp.sleep(0.3);
  ledPin.write(0);
  imp.sleep(0.3);

// if we’re connected
if (state == SERVER_CONNECTED) {
server.log(“hello”);
} else {
// long light means couldn’t connect
ledPin.write(1);
imp.sleep(2);
}
}

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

imp.onidle(function(){
server.log(“here”);
ledPin.write(1);
Connect(onConnectedCallback, 30);
ledPin.write(0);
server.expectonlinein(6);
imp.deepsleepfor(6);
});
`

What I see is that the light comes on once for the briefest of moments, but no server log is made - indicating the code path is

  1. [Server is disconnected]
  2. Turn on LED (ledPin.write(1))
  3. Call Connect function
  4. Server not connected -> so turn LED off
  5. Exit function (i.e. doesn’t call the callback -> as the lights don’t flash)

Obviously I must be doing something wrong, but I can’t see the wood through the trees on this one! :slight_smile: If anyone has any suggestions or could offer assistance I’d be very grateful.

I’m running this on a custom board, but I can connect to WIFI prior to the deepSleepFor and program/log etc. So I’m not inclined to think it’s a power problem?

Appreciate your time,

Adam

I am not one who can help but I can say I have also been having connection troubles that I cannot explain. It would help greatly if the electric imp team could provide a set of test code that is totally confirmed.

I had never tried any kind of sleep on the imp until the last week. I started using imp.sleep and just for 3 seconds. …and my imp device started this behavior of disconnecting. I thought it was the wifi at work. When I brought it home it was at first better and later resumed the troubles. I took out the sleep command and it is working nicely now.

I don’t mean to hijack so getting back to your project… have you had the code working before and only recently started having trouble? or is it a new project that you are bringing up?

Hi mjkuwp94,

It’s a new project. I’ve experienced the problem since the get-go.

If I never ‘disconnect’ it’s not normally an issue, i.e. if I deepsleep without RETURN_ON_ERROR and don’t use expectOnlineIn then it sleeps and wakes into wifi mode no problem. The issue then is, it’s using power for the wifi when I want it offline and logging.

Therefore, if I follow the sleepfor examples ad-verbatim it does indeed sleep and wake as expected. But then that isn’t the logic I want, and - to me - the logic I have above looks like it should behave as expected.

A

So, your code is expecting that the connect call is blocking. It is not. This starts a background connection process, and the callback will be called when this either succeeds or fails.

When the onidle() fires, you are trying to connect then immediately going to sleep… which appears to be exactly what you’re seeing :slight_smile:

There’s example code on the docs site for various connecting/disconnecting scenarios.

Hi Hugo,

Many thanks - I don’t know why I expected that but like I said - couldn’t see the wood through the trees!

Appreciate the assist… no such thing as a stupid question… right? :wink:

A

Absolutely not :slight_smile: