Unihiker Spotify Controller
About the project
Will guide you to build a Linux based powerfull Spotify controller.
Estimated time: 1 hour
Items used in this project
UNIHIKER is a single-board computer that runs on Linux and has a 2.8-inch touchscreen. It is designed for learning, coding, and creating projects related to artificial intelligence, robotics, STEM, and more. It has Wi-Fi and Bluetooth connectivity and can interact with various sensors and actuators using a built-in co-processor.
In the last tutorial, we have seen how to start with UNIHIKER, now in this tutorial will see how to build a Spotify music controller with UNIHIKER.
Step 1: Spotify Developer Portal Setup
Before moving to the hardware setup, first, we need to set up the Spotify developer API to control the playlist.
Make sure you have a premium account.
Navigate to the Spotify Developer portal. Then create a new application.
Use this URL as redirect URL - http://localhost/redirect
Next, open the settings.
And note down the Client ID and Client Secret key.
That's all about the API setup. Next, we need to implement a Python script to control the Spotify media.
Step 2: UNIHIKER Python Setup
First, copy this folder to UNIHIKER's root folder. It includes Python scripts and images for the controls.
Once you copied the scripts next open the VS Code and connect to the UNIHIKER.
Next, open the script folder in the VS Code workspace.
Once the connection is done, you will see the files.
Next, open the Spotify_functions script.
from spotipy.oauth2 import SpotifyOAuth
SPOTIPY_CLIENT_ID = ''
SPOTIPY_CLIENT_SECRET = ''
SPOTIPY_REDIRECT_URI = 'http://localhost/redirect'
scope = "user-read-private user-read-playback-state user-modify-playback-state"
#Create Spotipy object
sp = spotipy.Spotify(auth_manager=SpotifyOAuth(SPOTIPY_CLIENT_ID, SPOTIPY_CLIENT_SECRET, SPOTIPY_REDIRECT_URI, scope=scope))
#Gets connected users Spotify username
user_values = sp.current_user()
username = user_values["uri"].replace("spotify:user:", "")
#Grabs users playback status to determine wheather to play or pause music
playback_info = sp.current_playback()
playing = playback_info['is_playing']
#Skips to the next song queued up
#Goes back to the previous song
And then enter your credentials.
Finally, open the terminal and run the main.py script.
from pinpong.extension.unihiker import *
from pinpong.board import Board,Pin
from unihiker import GUI
import spotify_functions as sf
# Event callback function
Use the following command to run the script
Once you run the script, it will show you this message.
You need to verify and give the API to access Spotify. Click open.
Just copy the site URL and paste it into the terminal window.
That's all. Just open your Spotify app and try to play something. and let's try to click on the buttons in the UNIHIKER.
That's all about this UNIHIKER-based Spotify media controller, hope you guys like this project. Will be back with another project. Thank you.