Changing wifi state instead of sleep

Hi all,

Wondering if anyone’s had succes with turning wifi on /off? It’s not quite working out for me and I wanted to see if I could control the state of wifi rather than put the core to sleep or deep sleep (I don’t want to lose stored data).

Thanks for any help!

Continuing the discussion from Alternative Power Sources for the Spark Core:

@jkkim93, Spark.sleep(time) is the same as except with a timer. Both will set a flag for the background task which will then put the CC3000 module in standby mode. All this to say that if you set and don’t let the background run, it will not put the wifi in standby.

What exactly is not working for you. Perhaps you can share some of your code (gist?) so I can see what the problem may be? :smile:

I’m trying to confirm that the wifi.on function works by changing the state of the led I have if it turns on/off but not quite getting an output here… any help is greatly appreciated- thanks @peekay123

const int button = 1;
const int blitz = 7;
int press;
int lastPress = 0;
int count = 1;
char publishString[40];
unsigned wifiState;

#include “application.h”
#include “spark_disable_wlan.h”
#include “spark_disable_cloud.h”

void setup() {

pinMode(button, INPUT);
pinMode(blitz, OUTPUT);

Spark.variable(“wifistate”, &publishString, STRING);


void loop() {

press = digitalRead(button);
if(press != lastPress) {
if(press == HIGH) {
if (count%2 == 0) {;
digitalWrite(blitz, HIGH);
wifiState = WiFi.status();
sprintf(publishString, “%u”, wifiState);
Spark.publish(“wifi”, publishString);
else {
digitalWrite(blitz, LOW);
wifiState = WiFi.status();
sprintf(publishString, “%u”, wifiState);
Spark.publish(“wifi”, publishString);



lastPress = press;


@jkkim93, when you run WiFi.on/off() a flag is set. Your code doesn’t give the background task any time to run before you read the status and publish it. You could do this:

while (WiFi.status() != WIFI_ON)
   SPARK_WLAN_Loop()    // run backgound task so wifi can turn on

while(WiFi.status != WIFI_OFF)
   SPARK_WLAN_Loop()    // run backgound task so wifi can turn off

My real question would be the above- from digging around a bit, it seems like a delay function to let the wifi state on or off work, but what is the logic behind turning wifi on and off that makes the delay necessary? If you can give me a bit of background, that would help tremendously!!!

Another wifi-related question, when I incorporate the above example code into mine, I get two errors that I think may have to do either with my syntax or including the right library- any help or clarification on that would be awesome.

/wifi.cpp:30:31: error: no match for ‘operator!=’ (operand types are ‘WiFi_Status_TypeDef()’ and ‘WiFi_Status_TypeDef’)
/wifi.cpp:31:33: error: ‘SPARK_WLAN_Loop’ was not declared in this scope

The WiFi.on() and functions only set a flag that the background process acts upon. The delay() function calls the background process so the cloud connection is not lost. The “normal” way that the background process is called is when the user loop() function “ends”. In reality, the Core firmware will run the background process, then call loop() and so on. So as long as the user program allows the background process to run after setting wifi on or off then they will work as expected.

The errors you get are due to some missing include files… my bad! You need to include these in your program:

#include "spark_disable_wlan.h"
#include "spark_disable_cloud.h"

These will start your Core with wifi and cloud OFF. To start these services, add this to the start of setup():




An afterthought- do you think there is a way to do it without including all the files? I’m trying to minimize memory as much as possible while conserving the battery.
Wondering if I need all those libraries to do spark.sleep with a delay in the middle? Looked like I needed to include the libraries to do SPARK_WLAN_Loop(), but trying to see if I can get by with just putting it to sleep for a certain amount of time, then waking it back up, and looping that in a delay- do I need the SPARK_WLAN_Loop() for the delay portion?
Thanks @peekay123! (I know I probably gave you a bunch of notifications just now, but I’ve been reading through all your comments and they are fantastic!!!)

@jkkim93, those extra libraries don’t necessarily add code and they do not affect RAM. They simply give you access to those functions. What is your concern regarding “memory”?

If you are so concerned about battery life then you should consider deep sleep instead of sleep. I would need to understand your application to best advise you. :smile: