Since server.connect(onConnectionCallback) is not blocking, how do I ensure that the code inside the onConnectionCallback executes before imp.onidle is called again? Right now, the imp is connecting and then immediately disconnecting before running agent.send() code in onConnectionCallback.
You can’t, the device does go idle in that period (there are times when there is nothing running hence the idle handler will get called).
Maybe unregister the onidle handler by passing null in as the callback, then re-register it in the connect callback?
Yea, I will try that way, I was just hoping there was a cleaner way to do it. Thanks!
unregistering onidle was not enough. However, using server.flush() in conjunction with the above, I got this to work as intended. Hope this helps others.
Hmm. Confused, and can’t see how server.flush() would help. Can you post your code? server.connect() is only non-blocking if you are in RETURN_ON_ERROR mode.
server.connect is synchronous but agent.send in the callback is not being received by the agent
server.flush actually didn’t fix the issue I was having.
I ended up settling for imp.onidle and server.sleepfor().
I wanted to keep the imp disconnected while gathering data, while periodically connecting to update the agent. It seems that server.sleepfor() works well for this.
Out of curiousity, by using imp.setpowersave(true), imp.enableblinkup(false) and server.sleepfor(), how significant is the power reduction vs just server.sleepfor() if we are connecting to the wifi every 2 minutes?
If your server.connect is synchronous, then you are in SUSPEND_ON_ERROR mode. As the docs state, the callback is ignored in this case.
When you are asleep, you are in the lowest power state (~6uA). The other powersave/enableblinkup things have no effect when sleeping.