WiFi.off (without Particle.disconnect) blocks indefinitely

I was doing some testing, and noticed, that in single-threaded operation, SEMI_AUTOMATIC mode (Photon, 0.7.0), calling WiFi.off() without first calling Particle.disconnect() causes loop to stop running indefinitely (as evidenced by lack of Serial prints and the lack of D7 flashing). Is this expected behavior? The device continues to breathe white, and current measurements show that the WiFi is indeed off. This is the test code that shows that behavior,

unsigned long now;

//SYSTEM_THREAD(ENABLED)
SYSTEM_MODE(SEMI_AUTOMATIC);

void setup() {
	//Serial.begin();
	delay(30000); // delay to allow current measurement to be observed
	pinMode(D7, OUTPUT);
	//Serial.println("In setup");
	now = millis();
}


void loop() {
	static unsigned long counter = 0;
	if (counter % 50000 == 0) {
		digitalWrite(D7, !digitalRead(D7));
	}

	if (millis() - now >= 30000) {
		//Serial.println("into millis if-clause");
		now = millis();
		Particle.connect();
		if (waitFor(Particle.connected, 30000)) {
			//Serial.println("about to publish");
			Particle.publish("test", "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz");
			delay(10000);
			//Particle.disconnect();
			WiFi.off();
		}else{
			//Serial.println("resetting");
			delay(500);
			System.reset();
		}
	}

	counter++;
}

Calling Particle.disconnect(), or running in threaded mode fixes this problem.

As a side note, I get some interesting current readings that I don’t understand. With the above code, I see the following values:

In setup: 47 mA
first 30 seconds in loop: 40 mA
Publishing: ~180 mA
Cloud connected: 96 mA
After WiFi off: 43 mA
Subsequent times through loop: 43 mA (when calling Particle.disconnect)

Why is the the current draw somewhat higher in setup, and why is it slightly higher after the first time through the loop?

1 Like

@Ric, I can confirm your finding about WiFi.off() which would call for a GitHub issue - are you going to file it?

I tested with this even simpler sketch

SYSTEM_MODE(SEMI_AUTOMATIC);

void setup()
{
  pinMode(D7, OUTPUT);
  Particle.connect();
}

void loop() 
{
    static bool withDisconnect = true;
    
    if (!digitalRead(BTN)) {
        if(Particle.connected()) {
            if (withDisconnect)
                Particle.disconnect();
            WiFi.off();
            withDisconnect = !withDisconnect;
        }
        else
            Particle.connect();
            
        delay(100);
    }

    digitalWrite(D7, (millis() >> 2) & 0x88);
}
1 Like

Thanks for the conformation. I will file a GitHub issue.

1 Like

I think this just happened to me but I was playing with tinker, did touch all the pins and created a state where the photon is blinking white no matter what. Even if I put it in listen mode via usb once I set up the wifi it will not connect and revert to white state…

Can it be fixed?

Hi @essence25 - I don’t presently believe the issue you describe is related to the above. Would you mind filing a support ticket (support.particle.io)? We’ll get you sorted. :slight_smile: