Unihiker Spotify Controller

Photo of pradeeplogu0

Made by pradeeplogu0

About the project

Will guide you to build a Linux based powerfull Spotify controller.

Project info

Difficulty: Easy

Platforms: DFRobot

Estimated time: 1 hour

License: GNU General Public License, version 3 or later (GPL3+)

Items used in this project

Hardware components

DFRobot UNIHIKER DFRobot UNIHIKER x 1

Story

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.

import spotipy
from spotipy.oauth2 import SpotifyOAuth

#User info
SPOTIPY_CLIENT_ID = ''
SPOTIPY_CLIENT_SECRET = ''
SPOTIPY_REDIRECT_URI = 'http://localhost/redirect'

#define scope
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
def get_user():
user_values = sp.current_user()
username = user_values["uri"].replace("spotify:user:", "")
return username

#Grabs users playback status to determine wheather to play or pause music
def play_pause():
playback_info = sp.current_playback()
playing = playback_info['is_playing']
if(playing):
sp.pause_playback()
elif(not playing):
sp.start_playback()

#Skips to the next song queued up
def next_song():
sp.next_track()

#Goes back to the previous song
def prev_song():
sp.previous_track()

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
import time

# Event callback function
def button_click1():
buzzer.pitch(131,1)
sf.prev_song()
def button_click2():
buzzer.pitch(147,1)
sf.play_pause()
def button_click3():
buzzer.pitch(165,1)
sf.next_song()

u_gui=GUI()
Board().begin()
do=u_gui.draw_image(image="PREV.png",x=20,y=3)
do.config(h=100)
do.config(onclick=button_click1)
re=u_gui.draw_image(image="PLAY.png",x=20,y=110)
re.config(h=100)
re.config(onclick=button_click2)
mi=u_gui.draw_image(image="NEXT.png",x=20,y=215)
mi.config(h=100)
mi.config(onclick=button_click3)

while True:
time.sleep(0.01)

Use the following command to run the script python main.py

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.

Wrap-up:

That's all about this UNIHIKER-based Spotify media controller, hope you guys like this project. Will be back with another project. Thank you.

Code

UNIHIKER Spotify Controller

Credits

Photo of pradeeplogu0

pradeeplogu0

Engineer, Diy Maker, Creator

   

Leave your feedback...