Code structure
The Spotify MacroBoard code is organized into several functional sections:- Setup and initialization: Hardware configuration and WiFi connection
- Display management: OLED screen updates and graphics
- LED control: RGB LED strip animations and color transitions
- Button handling: Input detection and action triggering
- API communication: HTTP requests to Spotify control server
- State management: Tracking playback state and metadata
Libraries and dependencies
The code uses several libraries for hardware control and networking:Global objects and variables
Hardware objects
wifiClient: Secure HTTPS client for API communicationI2C: I2C bus interface for OLED displaydisplay: SSD1306 OLED display object (128x64 pixels)LEDs: Array of RGB LED objects (20 LEDs)
Button state tracking
HIGH when not pressed and LOW when pressed.
Timing control
Playback state
Setup function
Thesetup() function runs once when the ESP32 boots and initializes all hardware components.
1
Configure GPIO pins
2
Initialize LED strip
3
Initialize OLED display
4
Connect to WiFi
5
Indicate successful connection
6
Configure SSL certificate
Main loop
Theloop() function continuously monitors buttons, updates the display, and synchronizes playback state.
Loop execution flow
- Button detection: Scans all 7 buttons for state changes (edge detection)
- WiFi monitoring: Updates signal strength indicator every 10 seconds
- Track sync: Fetches current track metadata every 5 seconds
- Progress tracking: Updates progress bar every second (increments locally if playing)
- LED animation: Smoothly fades LEDs when track changes (4ms per step)
The loop uses non-blocking delays with
millis() timing. This allows multiple tasks to run concurrently without freezing the system.Key functions
Button handling
Each button triggers a specific function through a function pointer array:updateState() with an action character:
API communication
TheupdateState() function sends control commands to the server:
API endpoint format:
GET /api/manageState/{PASSWORD}/{ACTION}Actions: playPause, back, skip, vdec, vinc, loop, shuffleFetching current track
TheupdateCurrent() function retrieves track metadata:
Expected JSON response format
Expected JSON response format
JSON parsing
The code uses a simple string-based JSON parser:LED color transitions
When a new track starts, LEDs smoothly transition to the album’s dominant color:Display updates
TheupdateScreen() function draws the top status bar with WiFi signal and volume:
Progress bar rendering
TheupdateTime() function draws the playback progress bar:
Error indication
The code uses LED colors to indicate different error states:Memory usage
The compiled sketch has the following resource utilization on ESP32:Performance considerations
Non-blocking architecture
All timing usesmillis() instead of delay(), allowing:
- Responsive button input
- Smooth LED animations
- Concurrent display updates
- Uninterrupted network communication
Update frequencies
Connection management
HTTP connections useKeep-Alive to reduce overhead:
Customization opportunities
Adjust update intervals
Modify timing constants to change update frequencies:Change LED brightness
Adjust LED brightness inSMBCredentials.h:
Modify button actions
Reorder or replace button functions:Next steps
Now that you understand the code structure:Hardware assembly
Build the physical MacroBoard
Troubleshooting
Resolve common issues