Complete guide to test the physical button functionality on ESP32.
Connect a push button to ESP32:
ESP32 Push Button
------ -----------
GPIO 0 <---- One terminal
GND <---- Other terminal (via button)
Note: ESP32 has internal pull-up resistor, so button connects to GND when pressed.
Default Button Pin: GPIO 0 (BOOT button on most ESP32 boards)
You can change the button pin in the code:
#define BUTTON_PIN 0 // Change this to your desired GPIO pinMost ESP32 development boards have a BOOT button on GPIO 0. You can use this button directly without any additional wiring!
- Press button once
- ESP32 detects single press after 500ms (double press window expires)
- Triggers SOS alert
- LED turns on
- 10-second countdown starts
- Notification sent to web app
- SMS sent after countdown (if not cancelled)
- Press button twice within 500ms
- ESP32 detects double press immediately
- Cancels SOS alert
- LED turns off
- Notification sent to web app
- Open
ESP32_Arduino_Code.inoin Arduino IDE - Upload to ESP32
- Open Serial Monitor (115200 baud)
- Open web app in Chrome/Edge/Opera
- Go to Bluetooth Scanner
- Click "Scan for ESP32 Devices"
- Select your ESP32 device
- Click Connect
- Navigate to Dashboard
Expected Behavior:
- Press button once
- Serial Monitor shows:
=== SINGLE PRESS DETECTED - Triggering SOS === - LED on ESP32 turns ON
- Web app Dashboard shows SOS alert panel
- 10-second countdown starts
- Activity log shows:
🔘 Physical button pressed - Triggering SOS
Verify:
- ✅ LED is ON
- ✅ SOS panel appears in web app
- ✅ Countdown timer is running
- ✅ Activity log shows button press
Expected Behavior:
- While SOS is active, press button twice quickly (within 500ms)
- Serial Monitor shows:
=== DOUBLE PRESS DETECTED - Cancelling SOS === - LED on ESP32 turns OFF
- SOS alert disappears from web app
- Activity log shows:
🔘🔘 Physical button double pressed - Cancelling SOS
Verify:
- ✅ LED is OFF
- ✅ SOS panel disappears
- ✅ Countdown stops
- ✅ Activity log shows cancellation
- Press button once → SOS starts
- Wait 10 seconds → SMS should be sent (if GSM is configured)
- LED turns off automatically after SMS sent
OR
- Press button once → SOS starts
- Press button twice quickly → SOS cancelled
- LED turns off immediately
When testing, you should see:
=== SINGLE PRESS DETECTED - Triggering SOS ===
SOS Triggered
Button event sent: SINGLE PRESS
Status sent - Battery: 100%, GSM: ON, LED: ON, BLE: ON
=== DOUBLE PRESS DETECTED - Cancelling SOS ===
SOS Cancelled
Button event sent: DOUBLE PRESS
Status sent - Battery: 100%, GSM: ON, LED: OFF, BLE: ON
Problem: Pressing button doesn't trigger anything
Solutions:
- Check button is connected to correct GPIO pin (default: GPIO 0)
- Verify button connects to GND when pressed
- Check Serial Monitor for button state changes
- Try using BOOT button if available
- Test button with simple LED blink code first
Problem: Button press doesn't trigger SOS
Solutions:
- Wait at least 500ms after pressing (double press window)
- Check Serial Monitor for "SINGLE PRESS DETECTED" message
- Verify BLE connection is active
- Check if web app is connected
Problem: Double press doesn't cancel SOS
Solutions:
- Press button twice quickly (within 500ms)
- Make sure first press was registered (check Serial Monitor)
- Try faster double press
- Adjust
DOUBLE_PRESS_WINDOW_MSif needed (default: 500ms)
Problem: Button works on ESP32 but web app doesn't show alert
Solutions:
- Verify BLE connection is active
- Check browser console for errors
- Ensure Data characteristic supports notifications
- Try reconnecting to device
- Check activity log in web app
Problem: Button triggers but LED doesn't change
Solutions:
- Check LED is connected to GPIO 2 (or configured pin)
- Verify LED wiring (anode to GPIO, cathode to GND via resistor)
- Check LED pin definition in code
- Test LED directly with
setLED(true)in code
Edit in code:
#define BUTTON_PIN 0 // Change to your GPIO pinEdit in code:
#define BUTTON_DEBOUNCE_MS 50 // Increase if button is bouncyEdit in code:
#define DOUBLE_PRESS_WINDOW_MS 500 // Time window for double press (milliseconds)You can modify button behavior in handleButtonPress() function:
// Example: Add triple press
if (pressCount == 3) {
// Handle triple press
handleTriggerPolice();
}- Button connected correctly
- Code uploaded to ESP32
- Serial Monitor open and showing output
- Web app connected to ESP32
- Single press triggers SOS
- LED turns on with single press
- Web app shows SOS alert
- Double press cancels SOS
- LED turns off with double press
- Activity log shows button events
- SMS sent after countdown (if GSM configured)
Add this to loop() to see button state:
Serial.print("Button state: ");
Serial.println(digitalRead(BUTTON_PIN));The code already includes Serial output for button events. Just watch Serial Monitor!
- Test all scenarios (single, double, SOS countdown, cancellation)
- Verify SMS sending works after countdown
- Test with physical device in real-world conditions
- Adjust timing if needed for your use case
- Add more button features (triple press, long press, etc.)