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
- [Server is disconnected]
- Turn on LED (ledPin.write(1))
- Call Connect function
- Server not connected -> so turn LED off
- 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! 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