Skip to content

ispyisail/esp32-badminton-timer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

48 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ESP32 Badminton Court Timer

This project transforms an ESP32 microcontroller into a sophisticated, web-controlled timer specifically designed for badminton courts and sports facilities. It provides a clear, responsive user interface that can be accessed from any device with a web browser on the same local network.

Version: 3.1.0

The timer is ideal for clubs, training facilities, or personal use, ensuring fair and consistent timing for games, practice sessions, and automated scheduling for multiple clubs sharing court time.

Key Features

Timer Management

  • Web-Based Interface: Control the timer from any device with a web browser. No app installation required.
  • Responsive UI: Works seamlessly on desktop, tablet, and mobile devices.
  • Simple Start/Stop Timer: Quick manual control for practice sessions and matches.
  • Real-Time Synchronization: All connected devices show the same time with 60fps smooth countdown.
  • Non-Blocking Siren: External relay/siren control without freezing the interface.

Multi-Club Scheduling System

  • Automated Scheduling: Create weekly recurring schedules that automatically start the timer.
  • Weekly Calendar View: Visual grid showing all scheduled sessions across the week.
  • Club-Based Organization: Each operator manages their own club's schedules.
  • Enable/Disable Toggle: Turn scheduling system on/off without deleting schedules.
  • Flexible Time Slots: Minute-level precision for scheduling (e.g., Monday 18:30 for 90 minutes).

Three-Tier Authentication System

  • Viewer Mode: Anyone can view the timer without credentials (no control access).
  • Operator Role: Club coordinators can create schedules for their club and control the timer.
  • Admin Role: Full access to user management, all schedules, and system configuration.
  • Default Credentials: Admin username is admin, password is admin (change immediately!).
  • Factory Reset: Restore all settings and users to defaults.

User Management

  • Add/Remove Operators: Admin can create operator accounts for different clubs.
  • Password Management: Users can change their own passwords.
  • Session Timeout: Automatic logout after 30 minutes of inactivity.
  • Rate Limiting: Protection against rapid-fire requests (10 messages/second limit).

System Features

  • Real-Time Clock: Displays current time, automatically synchronized from the internet via NTP.
  • NTP Sync Indicator: Visual indicator showing time synchronization status.
  • Configurable Timezone: Choose from 20 common timezones worldwide with automatic DST adjustment.
  • Persistent Settings: All settings, schedules, and users saved to non-volatile memory.
  • Over-the-Air (OTA) Updates: Wireless firmware updates protected by password.
  • Flexible WiFi Configuration: Captive portal or hardcoded credentials.

What's New in v3.1

  • 🔒 Enhanced Security: SHA-256 password hashing with automatic migration from plaintext
  • 🔒 HTTPS Validation: Let's Encrypt certificate validation for Hello Club API integration
  • 🔒 XSS Protection: HTML escaping for all user-generated content
  • 🔒 Stronger Passwords: Minimum password length increased from 4 to 8 characters
  • 🌍 International Support: Configurable timezone selection (20 common timezones)
  • 🔧 Reliability Improvements: Timer overflow handling, 30-second schedule checks, API retry logic
  • 💾 Memory Safety: Dynamic JSON allocation to prevent stack overflow
  • 🛡️ Input Validation: IANA timezone format validation, password hash verification

What's New in v3.0

  • Complete Authentication System: Three-tier role-based access control
  • Automated Scheduling: Weekly recurring schedules with calendar view
  • User Management: Add/remove operators, password changes
  • NTP Sync Status: Real-time indication of time synchronization
  • Session Management: 30-minute timeout with automatic viewer downgrade
  • Rate Limiting: Protection against abuse
  • Local Test Server: Node.js mock server for testing without hardware
  • 🔧 16 Bug Fixes: All critical, high, and medium priority issues resolved
  • 📚 Comprehensive Documentation: Complete code reviews, user guides, API docs

Quick Start

For Testing (No Hardware Required)

Want to try the interface before deploying to ESP32?

cd test-server
npm install
npm start

Open http://localhost:8080 and login with:

  • Admin: admin / admin
  • Operator: operator1 / pass123
  • Viewer: Leave fields empty

See test-server/README.md for full details.

For Production Deployment

  1. Hardware Setup: Connect ESP32, relay module, and siren (see INSTALL_GUIDE.md)
  2. Configure Security: Rename src/wifi_credentials.h.example to src/wifi_credentials.h and set passwords
  3. Build & Upload:
    platformio run --target upload      # Upload firmware
    platformio run --target uploadfs    # Upload web interface
  4. Configure WiFi: Connect to BadmintonTimerSetup network and enter your WiFi credentials
  5. Access Timer: Navigate to http://badminton-timer.local

Hardware Requirements

  • ESP32 Development Board
  • 5V Single-Channel Relay Module
  • External Siren or Buzzer (12V recommended)
  • 5V USB Power Supply (for ESP32)
  • Separate Power Supply for Siren (match siren voltage)
  • Jumper Wires

See INSTALL_GUIDE.md for complete wiring diagram and assembly instructions.

Security Setup

⚠️ IMPORTANT: Change default credentials before deployment!

Configure Passwords

  1. Navigate to src/ directory and rename wifi_credentials.h.example to wifi_credentials.h

  2. Edit src/wifi_credentials.h and update:

    • OTA_PASSWORD - Password for firmware updates
    • WEB_PASSWORD - Password for web interface (backward compatibility - now uses user system)
  3. Change admin password after first login:

    • Login as admin / admin
    • Click user icon → Change Password
    • Set a strong password immediately
  4. Create operator accounts:

    • Login as admin
    • Click user icon → Manage Users
    • Add operators for each club with unique passwords (minimum 8 characters)

Example:

const char* OTA_PASSWORD = "MySecureOTA2024!";
const char* WEB_PASSWORD = "legacy"; // Not used in v3.0

Security Note: The wifi_credentials.h file is ignored by git to protect your passwords. Never commit this file to a public repository.

Timezone Configuration

New in v3.1: The system now supports configurable timezones for international deployment.

Setting Your Timezone

  1. Login as admin (only administrators can change timezone)
  2. Navigate to Settings (user icon in top navigation)
  3. Locate System Settings section (admin-only area)
  4. Select your timezone from the dropdown menu (20 common timezones available)
  5. Changes apply immediately - all schedules and time displays update instantly

Available Timezones

The system includes 20 commonly-used timezones:

  • Oceania: Pacific/Auckland (NZ), Australia/Sydney, Pacific/Fiji
  • Asia: Asia/Tokyo, Asia/Singapore, Asia/Hong_Kong, Asia/Kolkata, Asia/Dubai
  • Europe: Europe/London (UK), Europe/Paris, Europe/Berlin, Europe/Moscow
  • Americas: America/New_York (EST/EDT), America/Chicago (CST/CDT), America/Denver (MST/MDT), America/Los_Angeles (PST/PDT), America/Toronto, America/Mexico_City, America/Sao_Paulo
  • UTC: UTC (Universal Coordinated Time)

How It Works

  • Persistent Storage: Timezone setting is saved to non-volatile memory (survives reboots)
  • Automatic DST: Timezones with daylight saving time automatically adjust
  • Schedule Conversion: Hello Club events are converted from UTC to your local timezone
  • Default Timezone: Pacific/Auckland (New Zealand) - change on first setup if deploying elsewhere

Important: Ensure timezone is set correctly before creating schedules. Changing timezone after schedules are created does not retroactively update existing schedule times.

User Roles Explained

Viewer (No Login Required)

  • View timer countdown
  • View current schedules
  • Cannot: Control timer, create schedules, change settings

Operator (Club Coordinator)

  • Everything viewers can do, plus:
  • Start/stop timer manually
  • Create/edit/delete their own club's schedules
  • View only their own club's schedules
  • Change their own password
  • Cannot: See other clubs' schedules, manage users, factory reset

Admin (System Administrator)

  • Everything operators can do, plus:
  • View all schedules from all clubs
  • Add/remove operator accounts
  • Change admin password
  • Perform factory reset
  • Manage system settings

WiFi Configuration

Method 1: Captive Portal (Recommended)

  1. Power on the ESP32
  2. Connect to WiFi network: BadmintonTimerSetup
  3. Browser should auto-open setup page (or go to http://192.168.4.1)
  4. Select your network, enter password, click Save
  5. ESP32 restarts and connects to your network

Method 2: Hardcode Credentials

  1. Rename src/wifi_credentials.h.example to src/wifi_credentials.h
  2. Edit file and set your SSID and password:
    #define WIFI_SSID "YourNetworkName"
    #define WIFI_PASSWORD "YourNetworkPassword"
  3. Upload firmware - ESP32 will auto-connect, bypassing captive portal

Using the Scheduling System

For Operators

  1. Login: Enter your operator username and password
  2. Access Schedules: Click the calendar icon in the top navigation
  3. Enable Scheduling: Toggle the "Automatic Scheduling" switch to ON
  4. Create Schedule:
    • Click "Add Schedule" button
    • Enter your club name
    • Select day of week (Sunday-Saturday)
    • Set start time (hour and minute)
    • Set duration in minutes
    • Click Save
  5. View on Calendar: Your schedule appears in the weekly grid
  6. Edit/Delete: Click any schedule block to edit or delete it

For Admins

Admins can see and manage schedules from all clubs. Use this to:

  • Resolve scheduling conflicts between clubs
  • View overall court utilization
  • Remove schedules if needed

How Auto-Start Works

When scheduling is enabled:

  • ESP32 checks every minute if any schedule should trigger
  • If current time matches a schedule's start time, timer automatically starts
  • Timer runs for the scheduled duration
  • Prevents re-triggering within 2 minutes (safety window)

Important: Ensure NTP time is synced (check the ✓ indicator next to the clock) for schedules to work correctly.

Accessing the Timer

Once connected to your local network:

Recommended: http://badminton-timer.local

Alternative: Use ESP32's IP address (find in router's DHCP client list)

Troubleshooting mDNS

If badminton-timer.local doesn't work:

  • Use IP Address: Find ESP32's IP in your router and use that
  • Configure Static IP: Set a fixed IP for the ESP32 in your router
  • Enable mDNS: Check router settings for mDNS/Bonjour support
  • Try Different Browser: Some browsers have better mDNS support

Over-the-Air (OTA) Updates

Update firmware wirelessly after initial USB upload:

  • OTA Hostname: badminton-timer.local
  • OTA Password: Set in src/wifi_credentials.h

In PlatformIO:

platformio run --target upload --upload-port badminton-timer.local

Security Note: Use a strong, unique OTA password. Default is insecure and MUST be changed.

Documentation

  • README.md (this file) - Overview and quick start
  • INSTALL_GUIDE.md - Complete hardware assembly and software installation
  • USER_GUIDE.md - End-user guide for operators and admins
  • ARCHITECTURE.md - System architecture and design decisions
  • API.md - WebSocket API reference for developers
  • CHANGELOG.md - Version history and release notes
  • CODE_REVIEW_FINDINGS.md - First code review (16 issues found and fixed)
  • CODE_REVIEW_ROUND_2.md - Second code review (verification and final assessment)
  • test-server/README.md - Local testing server documentation

Project Structure

esp32-badminton-timer/
├── src/
│   ├── main.cpp              # Main application with WebSocket handlers
│   ├── users.h/cpp           # User authentication system
│   ├── schedule.h/cpp        # Schedule management system
│   ├── timer.h/cpp           # Timer logic and state machine
│   ├── siren.h/cpp           # Non-blocking siren control
│   ├── settings.h/cpp        # NVS persistence layer
│   ├── config.h              # System configuration and constants
│   └── wifi_credentials.h    # WiFi and security credentials (not in git)
├── data/
│   ├── index.html            # Web interface structure
│   ├── style.css             # Complete UI styling
│   └── script.js             # Client-side WebSocket and UI logic
├── test-server/
│   ├── server.js             # Node.js mock server for local testing
│   ├── package.json          # Dependencies
│   └── README.md             # Test server documentation
├── docs/
│   └── (documentation files)
├── platformio.ini            # PlatformIO configuration
└── README.md                 # This file

Technology Stack

Backend (ESP32):

  • Arduino Framework for ESP32
  • AsyncWebServer (HTTP server)
  • AsyncWebSocket (bidirectional real-time communication)
  • ArduinoJson (JSON serialization)
  • ezTime (NTP time synchronization)
  • NVS/Preferences (non-volatile storage)
  • SPIFFS (filesystem for web files)

Frontend (Web Browser):

  • Vanilla JavaScript (no frameworks)
  • WebSocket API
  • CSS Grid Layout (calendar view)
  • requestAnimationFrame (smooth 60fps countdown)
  • Web Audio API (sound effects)

Development:

  • PlatformIO (build system)
  • Node.js + Express + ws (mock server for testing)
  • Git (version control)

Performance Characteristics

Metric Value
WebSocket latency 5-50ms (local network)
Timer precision ±10ms (server-side)
Display update rate 60fps (client-side)
Sync interval 5 seconds
Session timeout 30 minutes inactivity
Rate limit 10 messages/second per client
Max schedules 50 (configurable)
Max operators 10 (configurable)
Memory usage (ESP32) ~60KB RAM
Flash usage ~1.2MB program + ~200KB SPIFFS

Common Issues and Solutions

"Time not synced" warning

  • Check ESP32 has internet access
  • NTP requires UDP port 123 outbound
  • Wait 30 seconds after boot for first sync
  • Check serial monitor for NTP errors

Schedules not auto-starting

  • Ensure "Automatic Scheduling" toggle is ON
  • Verify NTP sync indicator shows ✓ (green checkmark)
  • Check schedule time matches current time in your configured timezone
  • Verify timezone is set correctly (Settings → System Settings)
  • Wait 2 minutes after last trigger (safety cooldown)

Can't login as operator

  • Verify operator account was created by admin
  • Check username and password (case-sensitive)
  • Try factory reset if forgotten (admin only)
  • Check serial monitor for auth errors

Other operators' schedules visible

  • This is normal for admins (they see all schedules)
  • Operators should only see their own club's schedules
  • Check you're logged in with correct username

Session timeout too frequent

  • Default is 30 minutes of inactivity
  • Any message to server resets timeout counter
  • Adjust SESSION_TIMEOUT in main.cpp if needed

Contributing

This project is production-ready as of v3.0. Contributions welcome:

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Test thoroughly (use test-server for UI changes)
  5. Submit a pull request

See CODE_REVIEW_ROUND_2.md for known minor issues that could be fixed.

License

MIT License - See LICENSE file for details

Credits

Developed by: Claude Code (Anthropic) Project Type: Open Source Language: C++ (ESP32), JavaScript (Frontend) Platform: ESP32 / Arduino Framework

Support

For issues, questions, or feature requests:

  • Check existing documentation first
  • Review code review documents for known issues
  • Check serial monitor output (115200 baud) for errors
  • Test with local mock server to isolate hardware vs software issues

Version History

  • v3.1.0 (2025-10-30) - Security hardening (SHA-256, HTTPS, XSS), configurable timezone, reliability improvements
  • v3.0.0 (2025-10-30) - Authentication, scheduling, user management, NTP status, 16 bug fixes
  • v2.0.0 (2025-10-29) - Modular architecture, improved timing, WebSocket sync
  • v1.0.0 (Initial) - Basic timer functionality

See CHANGELOG.md for detailed version history.


Last Updated: 2025-10-30 Project Status: ✅ Production Ready Tested On: ESP32-WROOM-32

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors