This example demonstrates how to use the SinricPro ESP-IDF component to create a smart switch that can be controlled via Alexa or Google Home.
- Voice control via Alexa/Google Home ("Alexa, turn on the switch")
- Physical button control (BOOT button toggles LED)
- Real-time state synchronization
- Auto-reconnection on network disruption
- Event rate limiting
- ESP32 development board
- Built-in LED (GPIO2)
- Built-in BOOT button (GPIO0)
-
Get SinricPro Credentials:
- Sign up at sinric.pro
- Create a new Switch device
- Note down your
APP_KEY,APP_SECRET, andDEVICE_ID
-
Modify the Example: Edit
main/switch_example.cand update these values:#define WIFI_SSID "YOUR_WIFI_SSID" #define WIFI_PASS "YOUR_WIFI_PASSWORD" #define DEVICE_ID "YOUR_DEVICE_ID" /* 24-character hex */ #define APP_KEY "YOUR_APP_KEY" /* UUID format */ #define APP_SECRET "YOUR_APP_SECRET" /* Long secret key */
cd examples/switch
idf.py set-target esp32
idf.py menuconfig # Optional: Configure SinricPro settings
idf.py build
idf.py flash monitor-
Via Voice:
- "Alexa, turn on the switch"
- "Alexa, turn off the switch"
- "Hey Google, turn on the switch"
-
Via Physical Button:
- Press the BOOT button to toggle the LED
- State is synchronized to SinricPro cloud
-
Via Mobile App:
- Use the SinricPro mobile app to control the device
I (XXX) switch_example: ==============================================
I (XXX) switch_example: SinricPro ESP-IDF Switch Example
I (XXX) switch_example: Version: 1.0.0
I (XXX) switch_example: ==============================================
I (XXX) switch_example: Connecting to WiFi...
I (XXX) switch_example: Got IP:192.168.1.100
I (XXX) switch_example: Connected to WiFi SSID:MyWiFi
I (XXX) switch_example: Initializing SinricPro...
I (XXX) switch_example: Creating switch device...
I (XXX) switch_example: Starting SinricPro...
I (XXX) switch_example: SinricPro started successfully!
I (XXX) switch_example: ✓ Connected to SinricPro server
I (XXX) switch_example: Button task started. Press BOOT button to toggle switch.
- Check SSID and password
- Ensure 2.4GHz WiFi (ESP32 doesn't support 5GHz)
- Check signal strength
- Verify APP_KEY and APP_SECRET are correct
- Check DEVICE_ID is exactly 24 hex characters
- Ensure internet connectivity
- Enable debug logging in menuconfig
- Ensure SinricPro skill is enabled
- Link your account in Alexa/Google Home app
- Discover devices in the app
- Check device name matches in SinricPro portal
/* Callbacks */
bool on_power_state(const char *device_id, bool *state, void *user_data);
bool on_setting(const char *device_id, const char *setting_id,
const char *value, void *user_data);
/* Main Flow */
1. Initialize NVS and GPIO
2. Connect to WiFi
3. Initialize SinricPro
4. Create switch device
5. Register callbacks
6. Start SinricPro
7. Button task for physical controlSee component README for full API documentation.
Copyright (c) 2019-2025 Sinric. All rights reserved.
Licensed under Creative Commons Attribution-Share Alike (CC BY-SA 4.0)