Skip to content

Configuration & Secrets

The firmware separates sensitive credentials (Wi-Fi passwords, Tailscale keys, OTA secrets) from hardware board definitions and runtime logic.


1. Automated Setup Tool (setup_secrets.py)

Run the cross-platform interactive CLI tool to generate main/secrets.h:

bash
python3 setup_secrets.py
Terminal — python3 setup_secrets.py
text
┌────────────────────────────────────────────────────────────────────────┐
│           ESP32-SwitchBot : Interactive secrets.h Generator            │
└────────────────────────────────────────────────────────────────────────┘
  ℹ Target destination : main/secrets.h
  ℹ Step-by-step setup  : Confirm with [Y] or retype with [n]

┌─ [Step 1] Wi-Fi Network Setup ─────────────────────────────────────────┐
│  Configuring Primary & Fallback Wi-Fi Networks                         │
│  Network #1 is mandatory. Up to 5 additional fallback networks optional.│
└────────────────────────────────────────────────────────────────────────┘

  ▸ Enter Wi-Fi #1 (Primary) SSID: Home_Network_2.4G
  ▸ Enter Wi-Fi #1 (Primary) Password: **************** (16 chars)
  ✔ Verified Wi-Fi Network #1.

  ▸ Do you want to add another Wi-Fi network (fallback)? [y/N]: n

┌─ [Step 2] Operation Mode ──────────────────────────────────────────────┐
│  Local-Only vs. Tailscale Remote Access                                │
│  Fully local mode disables Microlink & Tailscale, saving CPU and RAM.   │
│  The ESP32 will only be accessed over local Wi-Fi or subnet router.    │
└────────────────────────────────────────────────────────────────────────┘

  ▸ Are you trying to setup this ESP fully local (no Tailscale)? [y/N]: n
  ▸ Tailscale Auth Key: tskey-auth-****************
  ▸ Tailscale Device Hostname [esp32]: esp32

┌─ [Step 3] Device Security ─────────────────────────────────────────────┐
│  Over-The-Air (OTA) Flash Protection                                   │
│  PIN or passphrase required to authorize wireless firmware updates.    │
└────────────────────────────────────────────────────────────────────────┘

  ▸ Enter OTA Security Password (leave blank for one-click unlock): ******** (8 chars)

┌────────────────────────────────────────────────────────────────────────┐
│                      CONFIGURATION REVIEW SUMMARY                      │
├────┬────────────────────────────┬──────────────────────────────────────┤
│ #  │ Setting                    │ Configured Value                     │
├────┼────────────────────────────┼──────────────────────────────────────┤
│ 1  │ Operation Mode             │ Tailscale Enabled                    │
│ 2  │ Wi-Fi #1 (Primary)         │ Home_Network_2.4G (pass set)         │
│ 3  │ Tailscale Auth Key         │ tskey-auth-*********9876             │
│ 4  │ Tailscale Device Hostname  │ esp32                                │
│ 5  │ OTA Security Password      │ s******t (8 chars)                   │
└────┴────────────────────────────┴──────────────────────────────────────┘

  ✔ Successfully generated main/secrets.h!

The script features clean ANSI formatting and guides you through:

  1. Wi-Fi Network Configuration: 1 primary network + up to 5 automatic fallback networks (with WPA/WPA2 passphrases).
  2. Operation Mode: Choose between:
    • Tailscale Mesh VPN Mode: Global remote access with optional subnet router watchdog.
    • Fully Local Mode: Disables Microlink & WireGuard completely to minimize RAM and CPU overhead.
  3. Over-The-Air (OTA) Key: Password/PIN to protect wireless firmware flashing (or blank for one-click unlock).

2. Hardware Model Override (BOARD_NAME)

Hardware model names are automatically detected at boot by querying the ESP32 chip model, flash size, and PSRAM capabilities via ESP-IDF native heap APIs (e.g. ESP32-S3-N16R8).

If you wish to customize or override the displayed name, you can do so directly in [main/main.cpp](file:///Users/psychostark/Documents/PlatformIO/Projects/ESP32-SwitchBot/main/main.cpp#L53-L57):

cpp
// Hardware Model Name (Optional Manual Override)
// Leave empty ("") to let firmware automatically detect your ESP32 chip model, flash, and PSRAM (e.g. "ESP32-S3-N16R8").
// If you want to change it or if detection is wrong, specify your custom board name here (e.g. "ESP32-S3 DOIT"):
#define BOARD_NAME ""
  • Default (""): Automatically generates ESP32-S3-N16R8, ESP32-N4, etc.
  • Custom String: e.g., #define BOARD_NAME "SwitchBot Pro" renders that exact string on the web dashboard and cURL info screen.

3. Configuration Reference Table

The following parameters are located in [main/main.cpp](file:///Users/psychostark/Documents/PlatformIO/Projects/ESP32-SwitchBot/main/main.cpp) and [main/secrets.h](file:///Users/psychostark/Documents/PlatformIO/Projects/ESP32-SwitchBot/main/secrets.h):

ParameterLocationDefault ValueDescription
BOARD_NAMEmain.cpp:59""Optional manual hardware model override.
TIMEZONE_OFFSETmain.cpp:54"+05:30"Timezone offset for NTP synchronization (supports +05:30, -05:00, 0530, 0).
WIFI_SSID_1..6secrets.h""Up to 6 configured Wi-Fi network SSIDs for automatic failover.
WIFI_PASSWORD_1..6secrets.h""Corresponding Wi-Fi WPA2 passwords.
local_IPmain.cpp:129192.168.1.50Static IP of the ESP32 on the local Wi-Fi subnet.
gatewaymain.cpp:130192.168.1.1Default router gateway IP address.
subnetmain.cpp:131255.255.0.0Subnet mask (/16). Allows communication with both /24 home routers and Windows Hotspots (192.168.137.x).
tailscaleAdvertiseRoutemain.cpp:140"192.168.1.0/24"CIDR advertised to Tailnet for high-availability subnet failover.
servoPinmain.cpp:1471Output GPIO connected to servo PWM line.
MAX_HOLD_DURATION_MSmain.cpp:16220000 (20s)Calibration manual hold safety watchdog; auto-returns arm to rest to protect motor.
OTA_AUTO_TIMEOUT_MSmain.cpp:200600000 (10m)Inactivity auto-close timer for port 3232 after being unlocked.
HEARTBEAT_INTERVAL_MSmain.cpp:20360000 (60s)NVS timestamp write interval for safe flash wear-leveling.
PRESS_COOLDOWN_MSmain.cpp:2104000 (4s)Cooldown interval between successive button pushes to protect the motor.
FIRMWARE_VERSIONmain.cpp:2596"1.2"Monospace firmware version string streamed across dashboards and telemetry.
OTA_KEYsecrets.h""Passphrase to authenticate Over-The-Air updates.

Released under Apache 2.0 License • 24/7 Operational Reliability