The Vision
I wanted a way to automatically adjust the ambiance of my room based on the music I'm listening to. Instead of manually fiddling with an infrared remote or relying on closed-ecosystem apps, I decided to build a centralized web application that connects my Bluetooth Low Energy (BLE) light strips directly to my Spotify playback state.
This is just the first step. My ultimate goal is to evolve this infrastructure into a full-fledged, Jarvis (Iron Man) style automated assistant—an AI that intuitively understands my environment, manages my devices, and anticipates my needs across my entire room and workflow.
How The Processing Works
To make this system reliable, responsive, and seamless, I architected a Python backend using Flask and asynchronous background workers. Here is a breakdown of the core processing pipeline:
1. Persistent BLE Communication
Using the bleak library, the system maintains a persistent, asynchronous connection to the LED controller's 0000fff3 characteristic. This runs in a dedicated background thread with a thread-safe queue. When you select a new "Mood" on the web dashboard, the color command is immediately pushed to the queue and transmitted to the LEDs without blocking the main web server. The connection is self-healing; if the Bluetooth drops, the worker silently attempts to reconnect.
2. Smart Playback Synchronization
It's not enough to just change the lights; they need to sync with the music. The backend leverages Windows Media Transport Controls (winrt) to monitor the exact playback state of the operating system. This ensures that the LEDs only transition to the new mood when the Spotify track actually starts playing, eliminating awkward pauses where the lights change but the music hasn't kicked in yet.
3. Computer Vision Fallback
Sometimes API calls aren't enough to force Spotify to start playing. To guarantee playback, the application utilizes a computer vision fallback mechanism. Using mss for rapid screen reading andpyautogui for automated OS interaction, the system can visually locate the distinct green "Play" button within the Spotify desktop app interface and physically click it, mimicking human interaction perfectly.
Looking Forward
The current stack—Flask, SQLite for storing custom URIs/Colors, and async Python workers—provides a robust foundation. As I move towards a more intelligent "Jarvis" system, I plan to integrate voice commands, LLM-based intent recognition, and broader smart-home IoT protocols.
Feel free to check out the code, fork it, and automate your own setup!