From d86d3632fa26bac831998ede477f881c77017a3e Mon Sep 17 00:00:00 2001 From: Sascha Seidel Date: Sat, 16 Mar 2024 03:24:49 +0100 Subject: [PATCH] Update getCurrentArtistAndSong.ino --- .../getCurrentArtistAndSong.ino | 31 +++++++++++++------ 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/examples/getCurrentArtistAndSong/getCurrentArtistAndSong.ino b/examples/getCurrentArtistAndSong/getCurrentArtistAndSong.ino index 15f7662..4069243 100644 --- a/examples/getCurrentArtistAndSong/getCurrentArtistAndSong.ino +++ b/examples/getCurrentArtistAndSong/getCurrentArtistAndSong.ino @@ -1,11 +1,15 @@ /* - An example of how to authenticate with Spotify without using a refresh token. + An example of how to authenticate with Spotify without using a refresh token and print Artist and Track via Serial This example is useful to get the refresh token for the first time. It can also be used to authenticate every time without using the refresh token. 15.03.2024 Created by: Finian Landes + 16.03.2024 + edited by: Sascha Seidel + * added getting artist and trackname to print it Serial + Documentation: https://github.com/FinianLandes/Spotify_Esp32 */ // Include the required libraries @@ -19,9 +23,7 @@ const char* CLIENT_ID = "YOUR CLIENT ID FROM THE SPOTIFY DASHBOARD"; const char* CLIENT_SECRET = "YOUR CLIENT SECRET FROM THE SPOTIFY DASHBOARD"; const char* REFRESH_TOKEN = "YOUR REFRESH TOKEN"; -Spotify sp(CLIENT_ID, CLIENT_SECRET);//if you dont have a refresh Token -//Uncomment following if u use a refresh Token -//Spotify sp(CLIENT_ID, CLIENT_SECRET, REFRESH_TOKEN); +Spotify sp(CLIENT_ID, CLIENT_SECRET); void setup() { Serial.begin(115200); @@ -32,15 +34,26 @@ void setup() { sp.handle_client(); } Serial.println("Authenticated"); - Serial.print("Currently playing: "); - Serial.print(sp.current_track_name()); - Serial.print(" by "); - Serial.println(current_artist_names()); } void loop() { - // put your main code here, to run repeatedly: + static String lastArtist; + static String lastTrackname; + + String currentArtist = sp.current_artist_names(); + String currentTrackname = sp.current_track_name(); + + if (lastArtist != currentArtist && currentArtist != "Something went wrong" && !currentArtist.isEmpty()) { + lastArtist = currentArtist; + Serial.println("Artist: " + lastArtist); + } + + if (lastTrackname != currentTrackname && currentTrackname != "Something went wrong" && currentTrackname != "null") { + lastTrackname = currentTrackname; + Serial.println("Track: " + lastTrackname); + } } + void connect_to_wifi(){ WiFi.begin(SSID, PASSWORD); Serial.print("Connecting to WiFi...");