Switching WiFi on/off with Pushbutton

Hi,
For a battery operated system with WLED, I want to disable the WiFi module with a physical pushbutton.I have set up a usermod and am now facing the problem with my code, that apparently the Wifi is never disabled.I am reading the “apActive” global variable to trigger my code. When “initAP” is executed in wledl.cpp it sets “apActive” to true at the end.

So in my code I’m able to properly detect if the button is pushed or not and the correct if condition is accessed accordingly. But the output in form of the onboard LED always stays on. Only sometimes, if I trigger to turn WiFi off, the LED shuts off but automatically turns back on after a few seconds.
So internally some routine is setting that variable back to true.

Can you help me to identify the problem? Or maybe a better way to achieve the same result?

Regards,
Hannes

deactivateAP in wled.cpp:
void WLED::deactivateAP()

{
if (apActive) {
DEBUG_PRINTLN(F("Closing access point"));

WiFi.softAPdisconnect(true);

apActive = false;

}

}

Partial usermod code:

 if( apActive == true){ // Check if Access Point is active

    digitalWrite(BLUE_LED, HIGH); // Turn on Blue LED AP is active

  } else {

    digitalWrite(BLUE_LED, LOW); // Turn off Blue LED AP is inactive

  }

  

  // Set WLAN to sleep when Button is released

  if (Btn_Wlan_Toggle.isReleased()) {        

     if (apActive == true) { // only if AP is on

        WLED::instance().deactivateAP();// Disconnect from WiFi

        WiFi.mode(WIFI_OFF);           // Turn off WiFi hardware      

        //apActive = false;               // Set AP status to false

        Serial.println("WLAN OFF");

      }

    }

  // Wake up WLAN, when Button is pressed

  else if (Btn_Wlan_Toggle.isPressed()) {

    if (apActive == false) { // only if AP is off

      WLED::instance().initAP(); // Initialize Access Point   

      Serial.println("WLAN ON");

    }

    }

Btw. How do I properly post code here? :face_with_crossed_out_eyes: