Help with Spark.sleep()

Hey so I’m trying to do Spark.sleep(). However, what it does is it seems to send my spark into an infinite sleep loop. I think i’m definitely doing something wrong. Could someone give me an example of where to place Spark.sleep() in my code? And yes, ive read the documentation on Spark.sleep() but I still dont really understand where to place it within my code. Thank you!

@suckms, which sleep mode are you trying to use and what are you trying a achieve? Can you post the code you are trying to use? :smile:

Hi @peekay123, thanks for the quick reply! The sleep mode i'm trying to use is this one

Spark.sleep() can be used to dramatically improve the
battery life of a Spark-powered project by temporarily deactivating the
Wi-Fi module, which is by far the biggest power draw.

What i'm trying to do is improve the battery life as it states.

My code initially is as follows:

void setup()
{   
    ...
}

void loop()
{
     //Various stuff running
       ...
        delay(5000);
        Spark.sleep(5);

}

Also, is it possible to check if the spark has managed to reconnect to the wifi using WiFi.status() or WiFi.ready()?
Thanks!

@suckms, the sleep mode you are using is simply to turn off the wifi module (CC3000) for the time specified. The Spark.sleep() call does not “stop” the user code. Instead it sets a flag which sets the wifi module to standby in the background. You can use WiFi.ready() to see if the wifi is up and running or Spark.connected() to see if both wifi and a cloud connection are established.

You can achieve lower power still by using SLEEP_MODE_DEEP which puts the Core and the wifi module in low power state and can be awakened after a specified time and/or an external pin event. :slight_smile:

Oh i see. Will attempt to try deep sleep instead. Thanks!

@suckms, one thing to point out is that to use deep sleep, you either need a newer Core or you need to update the bootloader. Also, waking from deep sleep resets the Core.

2 Likes

What do you mean by newer Core? How do I know if my core is the newer Core that you speak of? And if it is not, by updating the bootloader do you mean from here https://github.com/spark/bootloader?

@suckms,

You can simply try the function with something like 30 seconds and see if the core wakes up prematurely.

If it wakes up in 2-5 seconds upon entering the deep sleep, that indicates the watchdog reset occurred. In this case, we just need to do very simple patch using this:

https://github.com/spark/firmware/blob/bootloader-patch-update/build/core-firmware.bin

Have fun! :smiley:

3 Likes

@kennethlimcp and @peekay123 I’ve tried the deep sleep method and it works. However, I just realized during testing that when the core goes into deep sleep, it turns off the relay it’s supposed to control too! I guess this means that I have to use the Spark.sleep() instead of deep sleep as I still need code to be running? So if this is the case, how do I run the spark.sleep() without it running into infinite sleep?
Currently, I have this:

void setup()
{   
    ...
}

void loop()
{
     
        if (Spark.connected()) 
        {
            //Various stuff running
            ...
            delay(5000);
            Spark.sleep(30);
         } else {
         delay(1);
         if(WiFi.connecting())
            {
               Spark.connect();
            }
         }

}

What i’m getting with this code is a blinking green which means that it is attempting to connect, but after some time it does not connect back.

Also, doing this means that I am unable to use Spark.variable and Spark.function right?
Thank you

1 Like

Hi, I’m in the same boat. Just wondering if the Wifi.Connecting() logic is the wrong way around? Wouldn’t you want the spark to connect if it’s NOT already connecting?

Anyhow, I’ll report back if I get it to work.

1 Like

@megabyte Hey, Yeah…Just noticed that I put it the wrong way round! Should be

if(!WiFi.connecting())
            {
               Spark.connect();
            }
         }

:joy:… That aside, I actually did manage to get it working. I added in a counter to prevent it from going into an infinite sleep. Thus the logic is something like if the spark is not connected, means its sleeping, don’t run the spark sleep code. If it’s awake, count a few seconds for it to transfer data then go back to sleep. But in the end I didnt use it as I would be unable to use Spark.variable and Spark.function whilst it is sleeping and my sketch depends on those two to run properly.