Does WiFi.off() work when called within an interrupt routine? I would like to have the WiFi module off most of the time to conserve battery so I set up a switch to turn the WiFi module on and off when desired. Any time I try using WiFi.off() from my interrupt the Photon hangs and the RGB LED shows solid cyan. If I move the WiFi.off() anywhere else, it works fine. I would like to avoid checking a flag on every loop, if possible, which is why I created an interrupt.
In the test code below, everything works as expected except the “OFF” in the dipSwitch2 ISR. (I did try including Particle.disconnect() ahead of the WiFi.off() call but that didn’t change the behavior.)
// Set up the input pins for the dip switches
volatile bool wifiModulePower = false;
int dip1 = D6; // dip 1 is connected to D3
int dip2 = D5; // dip 2 is connected to D4
int LED1 = D7; // Use the built-in LED on D7
// Put system into SEMI_AUTOMATIC to control access to the cloud
SYSTEM_MODE(SEMI_AUTOMATIC);
// Turn off WiFi as soon as possible to conserve battery
STARTUP( WiFi.off() ); // Works Fine
void setup(void) {
// Start the serial port
Serial1.begin(115200);
// Set up pin modes
pinMode(LED1, OUTPUT);
pinMode(dip1, INPUT_PULLDOWN);
pinMode(dip2, INPUT_PULLDOWN);
// attach interrupts to the 2 dip switches
attachInterrupt(dip1, dipSwitch1, CHANGE);
attachInterrupt(dip2, dipSwitch2, CHANGE);
}
// Interrupt Service Routine for DIP 1
void dipSwitch1()
{
int val = digitalRead(dip1);
// Connect to the cloud (turns on wifi module by default)
if (val == HIGH && Particle.connected() == false) {
wifiModulePower = true;
Particle.connect(); // This Works Fine
Serial1.println("DIP 1 CONNECTED TO CLOUD & TURNED ON WIFI");
// ISR doesn't return to main loop until after connection is made... cool
}
// Set wifiModulePower false so the main loop will turn off the wifi module
if (val == LOW && Particle.connected() == true) {
wifiModulePower = false;
Serial1.println("DIP 1 DISCONNECTED FROM CLOUD");
}
}
// Interrupt Service Routine for DIP 2
void dipSwitch2()
{
int val = digitalRead(dip2);
// Connect to the cloud (turns on wifi module by default)
if (val == HIGH && Particle.connected() == false) {
wifiModulePower = true;
Particle.connect(); // This Works Fine
Serial1.println("DIP 2 CONNECTED TO CLOUD & TURNED ON WIFI");
// ISR doesn't return to main loop until after connection is made... cool
}
// turn WiFi module off (disconnect from cloud by default)
if (val == LOW && Particle.connected() == true) {
wifiModulePower = false;
Serial1.println("DIP 2 ATTEMPTING TO TURN OFF WIFI");
//************************** THIS SECTION DOESN'T WORK**********************************
WiFi.off(); // *** Photon hangs and shows solid cyan ***
Serial1.println("DIP 2 TURNED OFF WIFI MODULE POWER"); // *** This line never prints ***
//***************************************************************************************
}
}
void loop(void) {
Serial1.println("Hello World!");
// No need for the wifi module sucking power if you are not connected to the cloud
if ( wifiModulePower == false && WiFi.ready() ) {
WiFi.off(); // This Works Fine
Serial1.println("MAIN LOOP TURNED OFF WIFI MODULE POWER");
}
delay(1000);
}