Skip to content

SuccessAT/esp32-wifi-button-debounce

Repository files navigation

WiFi Button Debounce Example

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

Features

  • 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

Hardware Required

  • ESP32 development board
  • Two push buttons (active low, internally pulled up)
  • One LED
  • Jumper wires and breadboard

Default GPIO Configuration

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.

Button Functions

LED Control Button

  • Short Press: Toggle LED state (manual override)
  • Long Press: Reset to automatic mode (LED reflects WiFi status)

WiFi Control Button

  • Short Press: Trigger WiFi reconnection
  • Double Press: Display current WiFi status information

How to Use

Configure the Project

Open the project configuration menu (idf.py menuconfig):

idf.py menuconfig

Navigate to "WiFi Button Debounce Configuration" and configure:

  • WiFi SSID
  • WiFi Password
  • Maximum connection retry attempts
  • GPIO pin assignments
  • Authentication mode
  • Other WiFi parameters

Build and Flash

Build the project and flash it to the board:

idf.py -p PORT flash monitor

Replace PORT with your board's serial port (e.g., COM3 on Windows, /dev/ttyUSB0 on Linux).

Monitor Output

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: -------------

Configuration Options

WiFi Settings

  • CONFIG_ESP_WIFI_SSID: Network name
  • CONFIG_ESP_WIFI_PASSWORD: Network password
  • CONFIG_ESP_MAXIMUM_RETRY: Maximum connection retry attempts
  • CONFIG_ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD: Minimum accepted authentication mode

GPIO Settings

  • CONFIG_LED_GPIO: LED indicator pin
  • CONFIG_LED_BUTTON_GPIO: LED control button pin
  • CONFIG_WIFI_BUTTON_GPIO: WiFi control button pin

Advanced WiFi Settings

  • CONFIG_ESP_WIFI_PMF_ENABLE: Enable/disable Protected Management Frames
  • CONFIG_ESP_WIFI_STATIC_RX_BUFFER_NUM: Static RX buffer count
  • CONFIG_ESP_WIFI_DYNAMIC_RX_BUFFER_NUM: Dynamic RX buffer count

Troubleshooting

  1. Connection Failures

    • Verify WiFi credentials
    • Check signal strength
    • Ensure authentication mode matches AP settings
  2. Button Issues

    • Verify GPIO connections
    • Check button pull-up configuration
    • Ensure proper debounce timing
  3. LED Not Working

    • Verify GPIO connection
    • Check LED polarity
    • Ensure manual override is not active

Technical Details

WiFi Connection States

  • WIFI_STATE_INIT: Initial state
  • WIFI_STATE_DISCONNECTED: Not connected to any AP
  • WIFI_STATE_CONNECTING: Attempting to connect
  • WIFI_STATE_CONNECTED: Successfully connected
  • WIFI_STATE_ERROR: Connection failed after retries

Event Handling

The example implements a robust event handling system for:

  • WiFi events (connection, disconnection)
  • IP events (IP address acquisition)
  • Button events (press types)

Memory Management

  • Efficient use of static and dynamic buffers
  • Configurable buffer sizes through menuconfig
  • Proper resource cleanup on errors

Contributing

Feel free to contribute to this example by:

  1. Reporting issues
  2. Suggesting enhancements
  3. Creating pull requests

License

This example code is in the Public Domain (or CC0 licensed, at your option.)

Acknowledgments

This example uses the ESP-IDF development framework and builds upon Espressif's WiFi station example.

About

No description, website, or topics provided.

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published