I would like to understand the following (I am using Spark Core):
When I use wi-fi sleep (Spark.sleep(seconds)), I understand that it switches off the wi-fi for the specified number of seconds. If I call this inside a Spark function, does it continue executing the rest of the function code after it comes out of wi-fi sleep, or it goes back to setup() and loop() again? Also, if my external function call depends on wi-fi availability, does it make sense to add a delay() so there’s enough time for wi-fi to come up and get connected before the rest of the function code gets executed successfully?
When I use deep sleep (Spark.sleep(SLEEP_MODE_DEEP, seconds)), I understand that the whole Spark chip gets into sleep/hibernate mode consuming very less current (in the range of micro amp). When it comes out of the sleep, do setup() and loop() get executed, as if the system is being reset? Also, where would be the best place to put sleep call - inside setup() or inside my Spark function call (i.e. the local method that gets called from the Spark function over the net). My requirement is to keep the system idle for user-controlled duration.
Since my basic requirement is to save my battery power, without interrupting my service (i.e. Spark functions to execute from cloud whenever I want), I realize that #1 and 2 above won’t help much (but I would still like to know the answers to #1 and 2 ). What I think makes sense is to use the interrupt-driven sleep (Spark.sleep(pin, triggerCond)), so when I send a command on a particular pin (let’s say I attach a servo with A0, and rotate it) via cloud (Spark Function call), it should come out of sleep and start execution.
Here also, I have the same question as in #2: once it comes out of deep sleep (after interruption, i.e. my command to pin A0), does it execute setup() and loop() again, as if the system is reset? If that’s the case, my rest of the function code won’t be executed if I place my sleep code inside the function, right? Where should I typically put this sleep call?
Basic use case/requirement is this:
During initial boot up, I would like Spark Core to do the init stuff in the setup() call (btw, there’s nothing in my loop()), and then get into interrupt-driven deep sleep - so it’s in lowest power consumption mode. Now if I send a command to rotate my servo x degree, the deep sleep should get interrupted and come out and execute my command (on pin A0…I believe I can use CHANGE as the 2nd parameter?). Once my command is executed successfully, I would like this again going into a deep sleep (interrupt mode).
I am little confused with where should I put my sleep call - if I put this inside setup(), and if setup() gets called after it comes out of sleep, this will go into a forever loop! How do I make sure that my function call invocation always ends its deep sleep mode and still my rest of the command proceeds with the intended action?
I am new to Core, so would really appreciate if you could clarify the above.