This example demonstrates a robust WiFi station implementation with hardware button control and LED status indication. It showcases proper button debouncing, WiFi connection management, and status indication through LED feedback.
| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-C61 | ESP32-S2 | ESP32-S3 |
|---|
- WiFi station mode with automatic connection management
- Button debouncing with support for multiple press types:
- Short press
- Long press
- Double press
- LED status indication with manual override capability
- Real-time WiFi status monitoring and display
- Configurable through menuconfig
- Automatic reconnection with configurable retry limit
- Protected Management Frames (PMF) support
- Comprehensive error handling and status reporting
- ESP32 development board
- Two push buttons (active low, internally pulled up)
- One LED
- Jumper wires and breadboard
| Component | GPIO Pin | Description |
|---|---|---|
| Status LED | GPIO2 | Indicates WiFi status |
| LED Button | GPIO3 | Controls LED behavior |
| WiFi Button | GPIO4 | Controls WiFi connection |
All GPIO pins are configurable through menuconfig.
- Short Press: Toggle LED state (manual override)
- Long Press: Reset to automatic mode (LED reflects WiFi status)
- Short Press: Trigger WiFi reconnection
- Double Press: Display current WiFi status information
Open the project configuration menu (idf.py menuconfig):
idf.py menuconfigNavigate to "WiFi Button Debounce Configuration" and configure:
- WiFi SSID
- WiFi Password
- Maximum connection retry attempts
- GPIO pin assignments
- Authentication mode
- Other WiFi parameters
Build the project and flash it to the board:
idf.py -p PORT flash monitorReplace PORT with your board's serial port (e.g., COM3 on Windows, /dev/ttyUSB0 on Linux).
The serial monitor will display:
- WiFi connection status changes
- Button events
- Connection details (SSID, channel, RSSI, etc.)
- Error conditions and retry attempts
Example output:
I (589) wifi_button: ESP_WIFI_MODE_STA
I (599) wifi_button: Initializing WiFi
I (769) wifi_button: WiFi State: Connecting
I (889) wifi_button: Connected to AP
I (2089) wifi_button: Got IP: 192.168.1.100
I (2089) wifi_button: WiFi State: Connected
I (2089) wifi_button: WiFi Status:
I (2089) wifi_button: -------------
I (2089) wifi_button: SSID: MyNetwork
I (2089) wifi_button: Channel: 6
I (2089) wifi_button: RSSI: -65 dBm
I (2089) wifi_button: Auth Mode: WPA2 PSK
I (2089) wifi_button: -------------
CONFIG_ESP_WIFI_SSID: Network nameCONFIG_ESP_WIFI_PASSWORD: Network passwordCONFIG_ESP_MAXIMUM_RETRY: Maximum connection retry attemptsCONFIG_ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD: Minimum accepted authentication mode
CONFIG_LED_GPIO: LED indicator pinCONFIG_LED_BUTTON_GPIO: LED control button pinCONFIG_WIFI_BUTTON_GPIO: WiFi control button pin
CONFIG_ESP_WIFI_PMF_ENABLE: Enable/disable Protected Management FramesCONFIG_ESP_WIFI_STATIC_RX_BUFFER_NUM: Static RX buffer countCONFIG_ESP_WIFI_DYNAMIC_RX_BUFFER_NUM: Dynamic RX buffer count
-
Connection Failures
- Verify WiFi credentials
- Check signal strength
- Ensure authentication mode matches AP settings
-
Button Issues
- Verify GPIO connections
- Check button pull-up configuration
- Ensure proper debounce timing
-
LED Not Working
- Verify GPIO connection
- Check LED polarity
- Ensure manual override is not active
WIFI_STATE_INIT: Initial stateWIFI_STATE_DISCONNECTED: Not connected to any APWIFI_STATE_CONNECTING: Attempting to connectWIFI_STATE_CONNECTED: Successfully connectedWIFI_STATE_ERROR: Connection failed after retries
The example implements a robust event handling system for:
- WiFi events (connection, disconnection)
- IP events (IP address acquisition)
- Button events (press types)
- Efficient use of static and dynamic buffers
- Configurable buffer sizes through menuconfig
- Proper resource cleanup on errors
Feel free to contribute to this example by:
- Reporting issues
- Suggesting enhancements
- Creating pull requests
This example code is in the Public Domain (or CC0 licensed, at your option.)
This example uses the ESP-IDF development framework and builds upon Espressif's WiFi station example.