Skip to main content

Overview

The Spotify MacroBoard requires a personal server that acts as a proxy between the ESP32 and the Spotify Web API. This server handles OAuth authentication, token refresh, and API requests on behalf of the MacroBoard.
The reference implementation runs on benzhou.tech at port 443 (HTTPS). You’ll need to set up your own server with a similar configuration.

Why a proxy server?

The ESP32 has limited computational resources and memory, making it impractical to:
  • Implement OAuth 2.0 authentication flow
  • Store and refresh access tokens
  • Parse complex JSON responses from Spotify API
  • Handle API rate limiting and retries
The proxy server handles these complexities, allowing the ESP32 to make simple GET requests with minimal processing.

Server requirements

Domain and SSL certificate

Your server must be accessible via HTTPS with a valid SSL certificate:
  • Domain name: The ESP32 connects to a hardcoded domain (e.g., benzhou.tech)
  • SSL certificate: Required for secure HTTPS communication
  • Port 443: Standard HTTPS port
The ESP32 validates the server’s SSL certificate against the benzServerCert constant in SampleCredentials.h. If you use a self-signed certificate, you must include the full certificate chain.

Obtaining your SSL certificate

After setting up your server with an SSL certificate, extract the certificate:
Copy the certificate content (including -----BEGIN CERTIFICATE----- and -----END CERTIFICATE-----) into your SampleCredentials.h file:

Spotify API setup

Before implementing your server, register your application with Spotify:

1. Create a Spotify app

  1. Go to Spotify Developer Dashboard
  2. Log in with your Spotify account
  3. Click “Create app”
  4. Fill in the app details:
    • App name: Spotify MacroBoard Server
    • App description: Personal proxy server for ESP32 MacroBoard
    • Redirect URI: https://your-domain.com/callback
  5. Accept the terms and click “Create”

2. Get your credentials

After creating the app:
  1. Click on your app in the dashboard
  2. Go to “Settings”
  3. Note your Client ID and Client Secret
  4. These will be used in your server implementation

3. Required scopes

Your server needs to request the following OAuth scopes:
  • user-read-playback-state - Get current playback information
  • user-modify-playback-state - Control playback (play, pause, skip, etc.)
  • user-read-currently-playing - Get currently playing track

Server implementation

Your server must implement two endpoints that match the ESP32’s expectations:

Endpoint 1: Manage playback state

This endpoint receives control commands from the MacroBoard and forwards them to Spotify. Implementation requirements:
  1. Validate the password parameter
  2. Map the action to the appropriate Spotify API endpoint:
    • playPause → PUT /me/player/pause or /me/player/play
    • skip → POST /me/player/next
    • back → POST /me/player/previous
    • vinc → PUT /me/player/volume (increase by 10%)
    • vdec → PUT /me/player/volume (decrease by 10%)
    • shuffle → PUT /me/player/shuffle
    • loop → PUT /me/player/repeat
  3. Make the authenticated request to Spotify API
  4. Return success/failure status

Endpoint 2: Get current playback

This endpoint returns the current playback state formatted for the ESP32. Response format:
string
Current track title
string
Artist name (first artist if multiple)
string
Album name
integer
Track duration in seconds
integer
Current playback position in seconds
boolean
Whether playback is currently paused
integer
Current volume level (0-100)
array
RGB color array extracted from album artwork [R, G, B] where each value is 0-255

Album art color extraction

The color field provides an RGB array that represents the dominant color from the album artwork. The ESP32 uses this to set the RGB LED strip color, creating ambient lighting that matches the current track. Implementation approaches:
  1. Image processing library: Use a library like Pillow (Python) or Sharp (Node.js) to analyze the album art
  2. Color quantization: Extract the dominant color using k-means clustering or similar algorithms
  3. Caching: Cache colors by album ID to avoid reprocessing
Example implementation (Python with Pillow):

Example server (Node.js)

Here’s a basic implementation using Node.js and Express:
This is a simplified example. A production server should include:
  • Automatic token refresh logic
  • Error handling and retries
  • Rate limiting
  • Logging and monitoring
  • Environment variables for credentials

Token refresh

Spotify access tokens expire after 1 hour. Implement automatic token refresh:

Testing your server

Before connecting the ESP32, test your endpoints:
Expected response from getCurrent:

Updating ESP32 configuration

After setting up your server, update your SampleCredentials.h file:
If you’re using a different domain, you’ll also need to modify the hardcoded domain in src.ino:195 and src.ino:210:

Next steps

API endpoints

Detailed reference for all API endpoints

Troubleshooting

Common issues and solutions