How to integrate custom physical switch actions?


For a portable application I want to toggle the WiFi with a physical switch that the user can initiate a WiFi connection only when the lighting has to be modfied.
In this post I wanted to implement a debounce library but @tonyno told me that there already is a debounce routine.

So upon searching this forum and the web and looking through the button.cpp I couldn’t find a hint how to get the pressed state of a button. There is neither an explanation of the functions in the file nor an example code, so I’m left helpless now :confused:

Thank you.

You can do that as of right now with disabling AP mode and re-enable AP when you want change something. To do so just press default button (GPIO0) for 6 sec. When Wi-Fi network will be present controller will automatically connect to network.

Thanks for the hint.
But I need it to be switched off quickly by a rocking or a latching pressure switch. Also I want to properly understand how to integrate custom code into the usermod.cpp.

Did you integrate a usermod before?

There is a guidance for adding your code to sermons Add own functionality · Aircoookie/WLED Wiki · GitHub

Yes I know that tutorial.
My problem is, that I need either an additional debounce library (which isn’t recognized by the IDE/the WLED-project at the moment) or a source that tells me how to apply the button.cpp routines to my own button.

First you don’t need a debounce it’s already implemented. Second you do not describe what exactly is your goal.

Yes, somebody else told me that debounce is already implemented but HOW do I initiate a custom button then? I found no tutorials.

And what I want to achieve I already described after you gave me the hint with the default button. So I don’t know how I can be more precise with my problem. :man_shrugging:

Please join WLED Discord. You can talk your problem there with multiple people and more quickly.

I am looking for something similar also. It will help to secure my local network if I can (temporarily) turn off the WIFI via a hardware interface (read: switch). Only to switch it on again for maintenance purposes.

The reason is to reduce attack vectors on the local network. The esp board are fun to use, but security is not a prio in their design and applications. And with WLED increasingly adding hardware UI options, the use for WIFI as primary input - ignoring setup - is not always required anymore.

The functionality is built-in.

Do not connect your WLED to your wifi and disable auto AP creation (select Never) in wifi settings.

If you want AP to appear you press and hold button for >6s and the WLED-AP will appear in your network list until you reboot WLED.

Yes, this was already mentioned…

But @woehaa and me want to toggle it with the 2 switch states instantly and not wait time while holding down a pushbutton.

1 Like

Then you will need to start coding.
Everything you need is in button.cpp.

Of course I have, but I don’t find an instruction how to use the functions in button.cpp.
First I must tell the esp, where my button is connected and so far I couldn’t identify a spot in the code where I can add a new button in the pinmanager class allocatePin-function

Have you done it as you say that everything I need is in button.cp?

Definition of GPIOs used by buttons is in the hardware settings (LED settings) where you define a GPIO pin used by the button and a button type (pushbutton, switch, PIR sensor, analog, etc).

What happens when you press a button is defined in Time & Macros settings, where you can apply certain preset to a short, long or double press (for push button type, etc).

All of the information about buttons can be found in knowledge base.

BTW, do not bother with pin manager. It does nothing regarding buttons.

Can http/JSON be used to toggle the AP? I read the Knowledge base pages on both but didn’t see anything on it.

No. I was refering on where to look for pin definitions and actions assigned to buttons.
But as you suggested a JSON API can be enhanced to allow AP/WiFi to be switched on or off without changing any of behaviour of other components of the code.

Hey @blazoncek,
Yes, I can change the type of button there in the LED settings, but for these I can only assign LED presets but no behaviour regarding what I want.
I need a way to add a GPIO for my switch and then integrate my functionality as a regular usermod.
The other important question is, if I can somehow use the button.cpp functions for the debounce handling of my switch or if I need to implement a separate debounce functionality.

Sample is included in PWM Fan Multi Relay usermod.