11
11
NOTE: You need to get a Refresh token to use this example
12
12
Use the getRefreshToken example to get it.
13
13
14
+ Compatible Boards:
15
+ - Any ESP8266 or ESP32 board
16
+
14
17
Parts:
15
- ESP32 D1 Mini stlye Dev board* - http://s.click.aliexpress.com/e/C6ds4my
18
+ ESP32 D1 Mini style Dev board* - http://s.click.aliexpress.com/e/C6ds4my
16
19
17
- * * = Affilate
20
+ * * = Affiliate
18
21
19
- If you find what I do usefuland would like to support me,
22
+ If you find what I do useful and would like to support me,
20
23
please consider becoming a sponsor on Github
21
24
https://github.com/sponsors/witnessmenow/
22
25
27
30
Twitter: https://twitter.com/witnessmenow
28
31
*******************************************************************/
29
32
30
-
31
33
// ----------------------------
32
34
// Standard Libraries
33
35
// ----------------------------
34
36
37
+ #if defined(ESP8266)
38
+ #include < ESP8266WiFi.h>
39
+ #elif defined(ESP32)
35
40
#include < WiFi.h>
41
+ #endif
42
+
36
43
#include < WiFiClientSecure.h>
37
44
38
45
// ----------------------------
39
46
// Additional Libraries - each one of these will need to be installed.
40
47
// ----------------------------
41
48
42
- #include < ArduinoSpotify .h>
49
+ #include < SpotifyArduino .h>
43
50
// Library for connecting to the Spotify API
44
51
45
52
// Install from Github
46
- // https://github.com/witnessmenow/arduino-spotify-api
53
+ // https://github.com/witnessmenow/spotify-api-arduino
54
+
55
+ // including a "spotify_server_cert" variable
56
+ // header is included as part of the SpotifyArduino libary
57
+ #include < SpotifyArduinoCert.h>
47
58
48
59
#include < ArduinoJson.h>
49
60
// Library used for parsing Json from the API responses
56
67
char ssid[] = " SSID" ; // your network SSID (name)
57
68
char password[] = " password" ; // your network password
58
69
59
- char clientId[] = " 56t4373258u3405u43u543" ; // Your client ID of your spotify APP
70
+ char clientId[] = " 56t4373258u3405u43u543" ; // Your client ID of your spotify APP
60
71
char clientSecret[] = " 56t4373258u3405u43u543" ; // Your client Secret of your spotify APP (Do Not share this!)
61
72
73
+ // Country code, including this is advisable
74
+ #define SPOTIFY_MARKET " IE"
75
+
62
76
#define SPOTIFY_REFRESH_TOKEN " AAAAAAAAAABBBBBBBBBBBCCCCCCCCCCCDDDDDDDDDDD"
63
77
64
78
// ------- ---------------------- ------
65
79
66
- // including a "spotify_server_cert" variable
67
- // header is included as part of the ArduinoSpotify libary
68
- #include < ArduinoSpotifyCert.h>
69
-
70
80
WiFiClientSecure client;
71
- ArduinoSpotify spotify (client, clientId, clientSecret, SPOTIFY_REFRESH_TOKEN);
81
+ SpotifyArduino spotify (client, clientId, clientSecret, SPOTIFY_REFRESH_TOKEN);
72
82
73
- void playSingleTrack (){
83
+ void playSingleTrack ()
84
+ {
74
85
char sampleTrack[] = " spotify:track:4uLU6hMCjMI75M1A2tKUQC" ;
75
86
char body[100 ];
76
87
sprintf (body, " {\" uris\" : [\" %s\" ]}" , sampleTrack);
77
- if (spotify.playAdvanced (body)) {
88
+ if (spotify.playAdvanced (body))
89
+ {
78
90
Serial.println (" sent!" );
79
91
}
80
92
}
81
93
82
- void playMultipleTracks (){
94
+ void playMultipleTracks ()
95
+ {
83
96
char sampleTrack1[] = " spotify:track:6vW1WpedCmV4gtOijSoQV3" ;
84
97
char sampleTrack2[] = " spotify:track:4dJYjR2lM6SmYfLw2mnHvb" ;
85
98
char sampleTrack3[] = " spotify:track:4uLU6hMCjMI75M1A2tKUQC" ;
86
99
87
100
char body[200 ];
88
101
sprintf (body, " {\" uris\" : [\" %s\" , \" %s\" , \" %s\" ]}" , sampleTrack1, sampleTrack2, sampleTrack3);
89
- if (spotify.playAdvanced (body)) {
102
+ if (spotify.playAdvanced (body))
103
+ {
90
104
Serial.println (" sent!" );
91
105
}
92
106
}
93
107
94
- void playAlbum (){
108
+ void playAlbum ()
109
+ {
95
110
char sampleAlbum[] = " spotify:album:6N9PS4QXF1D0OWPk0Sxtb4" ;
96
111
97
112
char body[100 ];
98
113
sprintf (body, " {\" context_uri\" : \" %s\" }" , sampleAlbum);
99
- if (spotify.playAdvanced (body)) {
114
+ if (spotify.playAdvanced (body))
115
+ {
100
116
Serial.println (" sent!" );
101
117
}
102
118
}
103
119
104
- void specifyTrackNumOfAlbum (){
120
+ void specifyTrackNumOfAlbum ()
121
+ {
105
122
char sampleAlbum[] = " spotify:album:2fPcSpVFVo1dXEvarNoFkB" ;
106
123
// The position has an index of 0, so passing in 2
107
124
// like this will actually play the 3rd song.
108
125
int trackNum = 2 ;
109
126
110
127
char body[200 ];
111
128
sprintf (body, " {\" context_uri\" : \" %s\" , \" offset\" : {\" position\" : %d}}" , sampleAlbum, trackNum);
112
- if (spotify.playAdvanced (body)) {
129
+ if (spotify.playAdvanced (body))
130
+ {
113
131
Serial.println (" sent!" );
114
132
}
115
133
}
116
134
117
- void specifyTrackOfAlbum (){
135
+ void specifyTrackOfAlbum ()
136
+ {
118
137
char sampleAlbum[] = " spotify:album:2BLjT6yzDdKojUyc3Gi6y2" ;
119
138
char trackOnAlbum[] = " spotify:track:25IZtuJS77yXPCXMhPa1ze" ;
120
139
121
140
char body[200 ];
122
141
sprintf (body, " {\" context_uri\" : \" %s\" , \" offset\" : {\" uri\" : \" %s\" }}" , sampleAlbum, trackOnAlbum);
123
- if (spotify.playAdvanced (body)) {
142
+ if (spotify.playAdvanced (body))
143
+ {
124
144
Serial.println (" sent!" );
125
145
}
126
146
}
127
147
128
- void playArtist (){
148
+ void playArtist ()
149
+ {
129
150
char sampleArtist[] = " spotify:artist:0gxyHStUsqpMadRV0Di1Qt" ;
130
151
131
152
char body[100 ];
132
153
sprintf (body, " {\" context_uri\" : \" %s\" }" , sampleArtist);
133
- if (spotify.playAdvanced (body)) {
154
+ if (spotify.playAdvanced (body))
155
+ {
134
156
Serial.println (" sent!" );
135
157
}
136
158
}
137
159
138
- void playPlaylist (){
160
+ void playPlaylist ()
161
+ {
139
162
char samplePlaylist[] = " spotify:playlist:37i9dQZF1DZ06evO05tE88" ;
140
163
141
164
char body[100 ];
142
165
sprintf (body, " {\" context_uri\" : \" %s\" }" , samplePlaylist);
143
- if (spotify.playAdvanced (body)) {
166
+ if (spotify.playAdvanced (body))
167
+ {
144
168
Serial.println (" sent!" );
145
169
}
146
170
}
147
171
148
- void specifyTrackNumOfPlaylist (){
172
+ void specifyTrackNumOfPlaylist ()
173
+ {
149
174
char samplePlaylist[] = " spotify:playlist:37i9dQZF1DZ06evO05tE88" ;
150
175
// The position has an index of 0, so passing in 31
151
176
// like this will actually play the 32nd song.
152
177
int playlistTrackNum = 31 ;
153
178
154
179
char body[200 ];
155
180
sprintf (body, " {\" context_uri\" : \" %s\" , \" offset\" : {\" position\" : %d}}" , samplePlaylist, playlistTrackNum);
156
- if (spotify.playAdvanced (body)) {
181
+ if (spotify.playAdvanced (body))
182
+ {
157
183
Serial.println (" sent!" );
158
184
}
159
185
}
160
186
161
- void specifyTrackOfPlaylist (){
187
+ void specifyTrackOfPlaylist ()
188
+ {
162
189
char samplePlaylist[] = " spotify:playlist:37i9dQZF1DZ06evO05tE88" ;
163
190
char trackOnPlaylist[] = " spotify:track:6vW1WpedCmV4gtOijSoQV3" ;
164
191
165
192
char body[200 ];
166
193
sprintf (body, " {\" context_uri\" : \" %s\" , \" offset\" : {\" uri\" : \" %s\" }}" , samplePlaylist, trackOnPlaylist);
167
- if (spotify.playAdvanced (body)) {
194
+ if (spotify.playAdvanced (body))
195
+ {
168
196
Serial.println (" sent!" );
169
197
}
170
198
}
171
199
172
- void setup () {
200
+ void setup ()
201
+ {
173
202
174
- Serial.begin (115200 );
203
+ Serial.begin (115200 );
175
204
176
205
// Set WiFi to station mode and disconnect from an AP if it was Previously
177
206
// connected
@@ -194,13 +223,21 @@ void setup() {
194
223
IPAddress ip = WiFi.localIP ();
195
224
Serial.println (ip);
196
225
226
+ // Handle HTTPS Verification
227
+ #if defined(ESP8266)
228
+ client.setFingerprint (SPOTIFY_FINGERPRINT); // These expire every few months
229
+ #elif defined(ESP32)
197
230
client.setCACert (spotify_server_cert);
231
+ #endif
232
+ // ... or don't!
233
+ // client.setInsecure();
198
234
199
235
// If you want to enable some extra debugging
200
236
// uncomment the "#define SPOTIFY_DEBUG" in ArduinoSpotify.h
201
237
202
238
Serial.println (" Refreshing Access Tokens" );
203
- if (!spotify.refreshAccessToken ()){
239
+ if (!spotify.refreshAccessToken ())
240
+ {
204
241
Serial.println (" Failed to get access tokens" );
205
242
return ;
206
243
}
@@ -232,11 +269,9 @@ void setup() {
232
269
delay (10000 );
233
270
Serial.println (" Playing specific track on Playlist" );
234
271
specifyTrackOfPlaylist ();
235
-
236
272
}
237
273
238
-
239
274
// Example code is at end of setup
240
- void loop () {
241
-
275
+ void loop ()
276
+ {
242
277
}
0 commit comments