An ESP32-based controller for managing multiple WiZ smart bulbs using physical toggle switches. This project provides reliable, local control of up to 6 WiZ bulbs through 5 toggle switches, with automatic retry logic and periodic synchronization.
- Configure WiFi: Copy
main/wifi_config.h.exampletomain/wifi_config.hand add your credentials - Configure Bulbs: Edit the
switchesarray inmain/main.cwith your bulb MAC addresses. IP addresses are automatically discovered. - Wire Switches: Follow the Wiring Guide to connect 5 toggle switches
- Build & Flash: Use
idf.py build flash monitorto build and flash the firmware
IMPORTANT: Before building, you must configure your WiFi credentials:
-
Copy the template file to create your WiFi configuration:
cp main/wifi_config.h.example main/wifi_config.h
-
Edit
main/wifi_config.hand replace the placeholders with your actual WiFi credentials:#define WIFI_SSID "YOUR_WIFI_SSID_HERE" #define WIFI_PASSWORD "YOUR_WIFI_PASSWORD_HERE"
The wifi_config.h file is gitignored and will not be committed to the repository, keeping your credentials secure.
This project is a Multi-Switch WiZ Smart Bulb Controller for ESP32. It connects to your WiFi network and allows you to control 6 WiZ smart bulbs using 5 physical toggle switches.
- WiFi Connectivity: Connects to your local WiFi network using credentials from
wifi_config.h - Multi-Bulb Control: Controls 6 WiZ smart bulbs via UDP protocol (port 38899)
- 5 Toggle Switches: Physical toggle switches for intuitive bulb control
- Reliable Detection: Hybrid polling + interrupt system ensures all switch changes are detected
- Automatic Retry: Failed commands are automatically retried up to 3 times
- Periodic Sync: Every 2 seconds, the system syncs bulb states with switch positions
- Status LED: Visual feedback via status LED on GPIO 2
- Automatic Reconnection: Automatically reconnects to WiFi if connection is lost
- An ESP32 development board
- 5 physical toggle switches (SPST)
- 6 WiZ smart bulbs connected to the same WiFi network
- Optional: Status LED on GPIO 2 (many ESP32 boards have a built-in LED)
| Switch # | GPIO Pin | Controls Bulbs (MAC addresses) | Logic |
|---|---|---|---|
| Switch 1 | GPIO 4 | d8a01162ba16, 444f8e308782 |
LOW=ON, HIGH=OFF |
| Switch 2 | GPIO 5 | d8a01162bc9e |
HIGH=ON, LOW=OFF |
| Switch 3 | GPIO 18 | d8a01170b374 |
HIGH=ON, LOW=OFF |
| Switch 4 | GPIO 19 | 444f8e26e796 |
HIGH=ON, LOW=OFF |
| Switch 5 | GPIO 21 | 444f8e26e756 |
HIGH=ON, LOW=OFF |
Note: Switch 1 uses different logic than switches 2-5 due to wiring differences. See Wiring Guide for detailed connection instructions.
Before building, you need to configure:
- WiFi Credentials: See WiFi Configuration Setup above
- Bulb MAC Addresses: Update the
switchesarray inmain/main.c(lines 52-57) with your bulb MAC addresses.
Auto-Discovery: The system automatically discovers bulb IP addresses at startup using the configured MAC addresses. This ensures reliable operation even if bulb IP addresses change (DHCP).
To find your WiZ bulb MAC addresses:
- Check the bulb label
- Use the WiZ app to view bulb details
- Use
wizlight discovertool (Python)
The app_main() function initializes the system in the following order:
- WiFi Initialization: Connects to WiFi using credentials from
wifi_config.h - Status LED Setup: Initializes GPIO 2 as output for status indication
- WiFi Connection Wait: Waits up to 15 seconds for WiFi connection
- UDP Socket Initialization: Sets up UDP socket for WiZ bulb communication
- Toggle Handler Task: Creates a task to handle switch state changes
- Toggle Switch GPIO Setup: Configures all 5 GPIO pins with pull-up resistors and interrupt handlers
- Initial State Sync: Reads initial switch positions and syncs bulb states
- Ready State: System ready for operation
Switch Operation:
- Switch 1: When toggle is ON (closed/LOW), bulbs 2 & 7 turn ON. When OFF (open/HIGH), bulbs turn OFF.
- Switches 2-5: When toggle is ON (closed/LOW), bulbs turn OFF. When OFF (open/HIGH), bulbs turn ON.
- Switch changes are detected via hybrid polling (100ms) + interrupt system
- Changes are debounced (50ms) to prevent false triggers
- Commands are retried up to 3 times if they fail
- Every 2 seconds, the system automatically syncs bulb states with switch positions
Status LED Feedback:
- 1 blink = Command sent successfully
- 2 blinks = Error sending command (after retries)
- 3 blinks = WiFi not connected
Reliability Features:
- Polling: Primary detection method - polls GPIO every 100ms to catch all changes
- Interrupts: Fast path for immediate response when switches change
- Debouncing: Multiple readings with majority vote to filter noise
- Retry Logic: Failed commands automatically retry with 200ms delays
- Periodic Sync: Ensures bulbs stay in sync even if commands are missed
Serial Monitor Output:
- WiFi connection status and IP address
- Switch initialization and configuration
- Switch change events with GPIO levels
- Bulb control commands sent
- Success/failure status for each command
- Periodic sync operations (when corrections are needed)
The project sample_project contains one source file in C language main.c. The file is located in folder main.
ESP-IDF projects are built using CMake. The project build configuration is contained in CMakeLists.txt
files that provide set of directives and instructions describing the project's source files and targets
(executable, library, or both).
Below is short explanation of remaining files in the project folder.
├── CMakeLists.txt
├── main
│ ├── CMakeLists.txt
│ ├── main.c Main application code
│ ├── wifi_config.h.example WiFi configuration template
│ └── wifi_config.h Your WiFi credentials (gitignored)
├── WIRING.md Detailed wiring guide for switches
└── README.md This is the file you are currently reading
-
main/main.c: Main application code containing:
- WiFi initialization and connection handling
- UDP socket management for WiZ bulb communication
- Switch GPIO configuration and interrupt handlers
- Switch state polling and change detection
- Bulb control logic with retry mechanism
- Periodic sync functionality
-
main/wifi_config.h: Your WiFi credentials (not in git)
-
WIRING.md: Complete wiring instructions for connecting 5 toggle switches
Additionally, the sample project contains Makefile and component.mk files, used for the legacy Make based build system. They are not used or needed when building with CMake and idf.py.