Spark core wifi.off() issue

Hi,

I’m running into an issue with wifi.off(). I have the core set to semi-automatic mode. Everytime wifi.off() is called, the core powers the wifi back on and reconnects. What am I missing?

The app im creating is for a door sensor, because it runs on batteries, i need to be able to power the wifi module on and off. Any help is greatly appreciated and thank you in advance for your time :smile:

    // This #include statement was automatically added by the Spark IDE.
#include "MQTT/MQTT.h"

SYSTEM_MODE(SEMI_AUTOMATIC);

double state = 0;
double old_state = 0;
int last_change;
int current_time;

volatile boolean pinChange = false;

void post();
void pinPoll();
void connect();
void callback(char* topic, byte* payload, unsigned int length);



byte ip[] = { 192, 168, 42, 1 }; 
MQTT client(ip, 1883, callback);

// recieve message
void callback(char* topic, byte* payload, unsigned int length) {
    char p[length + 1];
    memcpy(p, payload, length);
    p[length] = NULL;
    String message(p);

   /* if (message.equals("RED"))    
        RGB.color(255, 0, 0);
    else if (message.equals("GREEN"))    
        RGB.color(0, 255, 0);
    else if (message.equals("BLUE"))    
        RGB.color(0, 0, 255);
    else    
        RGB.color(255, 255, 255);
    delay(1000);
    */
}


void setup() {
    
     
    pinMode(A7, INPUT);
    client.connect("OCSensor");

 
    if (client.isConnected()) {
        client.publish("door","Connected!");
    }

    last_change = Time.now();
    
    
}

void loop() {
    
    current_time = Time.now();

    if ( client.isConnected() && WiFi.ready() ) client.loop();
        
    state = analogRead(A7);
    
    pinPoll();
    
    if(pinChange){
        post();
        pinChange = false;
        last_change = Time.now();
    }
    
    //should the wifi be turned off?
    if(current_time - last_change > 45) { //one minute
        RGB.control(true);
        RGB.color(255, 0, 0);

        if( WiFi.ready() ) Spark.disconnect();

        WiFi.off(); //turn off the wifi module 
        delay(1000);

    }
    else if(current_time - last_change < 46) connect();
    
    RGB.color(0, 0, 255);
    
    
}


void post(){
    
    if( !WiFi.ready() ) connect(); //reconnect
    
    RGB.control(true);
    RGB.color(0, 0, 255);
    
            
    //publish the state
    if(state > 4000) client.publish("door", "CLOSED");
    else if(state < 500)  client.publish("door", "OPEN");
    
    RGB.control(false);    
    


}


void pinPoll(){
    
    if( abs(state - old_state) > 3000) {
        pinChange = true;
        old_state = state;
    }
}

void connect(){
    
    RGB.control(false);

    if( !WiFi.ready() ) {
        
        
        WiFi.on(); //turn on the wifi
        WiFi.connect(); //connect to the wireless network
        
        while( !WiFi.ready() ){
            //do nothing
        }
        
        Spark.connect();
        
        client.connect("OCSensor");
    
        // publish/subscribe
        if (client.isConnected()) {
            client.publish("door","Connected!");
        }
    }
}

any help is greatly appreciated! thank you!

Hi @John9570,

Hmm, it seems like the most likely candidate would be calls to your connect() function when you’re not expecting. Have you tried adding some log statements using the serial port? In your setup function, add a line like Serial.begin(115200); and then right before your connect calls, try adding some lines like Serial.println("call 1..."); Serial.println("call 2...");, etc.

Then if you have something like the spark-cli installed, or Spark dev, you can use the serial monitors to watch that serial output coming from your device.

I hope that helps! :slight_smile:

Thanks,
David

Where do you set a new value for last_change?

Since last_change doesn’t seem to change at all after setup() the optimizer might create some trouble with this part

    //should the wifi be turned off?
    if(current_time - last_change > 45) { //one minute
        RGB.control(true);
        RGB.color(255, 0, 0);

        if( WiFi.ready() ) Spark.disconnect();

        WiFi.off(); //turn off the wifi module 
        delay(1000);

    }
    else if(current_time - last_change < 46) connect();

Just try an int volatile last_change; or actually do update last_change somewhere where the optimizier (and we) can see it :wink:

1 Like

Hi Dave,

You’re right, my connect() function is getting called again but I can’t figure out why.

Also, @ScruffR I commented and removed last_change from my code and forgot to put it in this post, sorry! I have updated my original post to show its location

@John9570, what sort of sensor/switch have you connected to your A7?
Could it be that the sensor does trigger a reconnect unexpectedly?

If you follow @Dave 's suggestion to print out checkpoints (e.g. Serial.print(current_time); Serial.println(" Checkpoint1"); to see the call hirachy) wherever you could possibly cause a reconnect (e.g. in post(), in connect(), before any call to these, at begin and end of loop()), you might be able to narrow it down better.

1 Like