FinianLandes / SpotifyEsp32

This is a library to connect to and control spotify from an esp
GNU Affero General Public License v3.0
13 stars 4 forks source link

Spotify Library for ESP32

This library is a wrapper for the Spotify Web API and is designed to work with the ESP32 microcontroller.

Dependencies

Setup

Youtube Tutorial for the setup

  1. Create a new application on the Spotify Developer Dashboard and copy the Client ID and Client Secret into your code. Leave the developer dashboard open as you will need to set the callback url later.
  2. Now you will have to use the login without a refresh token which can be implemented the following way:
    
    #include <Arduino.h>
    #include <WiFi.h>
    #include "SpotifyEsp32.h"

const char SSID = "your_ssid"; const char PASSWORD = "your_password"; const char CLIENT_ID = "your_client_id"; const char CLIENT_SECRET = "your_client_secret";

//Create an instance of the Spotify class Optional: you can set the Port for the webserver the debug mode(This prints out data to the serial monitor) and number of retries Spotify sp(CLIENT_ID, CLIENT_SECRET);

void setup() { Serial.begin(115200); connect_to_wifi();//Connect to your wifi

sp.begin();//Start the webserver
while(!sp.is_auth()){//Wait for the user to authenticate
    sp.handle_client();//Handle the client, this is necessary otherwise the webserver won't work
}
Serial.println("Authenticated");

}

void loop() { //Add your code here } void connect_to_wifi(){ WiFi.begin(SSID, PASSWORD); Serial.print("Connecting to WiFi..."); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.print("."); } Serial.printf("\nConnected to WiFi\n"); }

3. Now you will have to set the callback url in the Spotify Developer Dashboard before you can log in the callback url is the url printed to the serial plus callback so e.g. if your url printed to the serial is: ```https://192.1.1.128/``` then the callback url will be ```https://192.1.1.128/callback```  </br>

4. If you want to set your tokens during runtime you can use the same code pass an empty char array to the constructor and set the tokens later in the webserver. By calling the "get_tokens" function you could then save these tokens with [SPIFFS](https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/storage/spiffs.html). And during next runtime pass them from the memory. </br>
5. Now you can use the library. </br>
## Usage
- The normal functions return an response object. You can get the http code or incase the token request failed and the request wasn't sent -1 with ```response_obj.status_code```. The response message can be obtained as JsonDocument with ```response_obj.reply```. </br>
To print the response you can use the ```print_response(response_obj)``` function. </br>
- To minimize RAM usage all functions making a GET request have an optional last parameter which is a filter JsonDocument which can be used to filter the response and only get the necessary data. Filtering works the following way: you pass a JsonDocument with the same structure than the response (This structure can be looked up in the Documentation of the Spotify Web API) where wanted data is just set to true and unwanted data is not included. Sadly there is no new tutorial for v7 of Arduino Json but the one of v6 is nearly identical except the definition of the JsonDocumet which can now be done in all cases like that: ```JsonDocument doc;``` [Filter tutorial](https://arduinojson.org/news/2020/03/22/version-6-15-0/)  </br>
- To search for methods you can use the [Spotify Web API Reference](https://developer.spotify.com/documentation/web-api/reference/), all of the methods shown there are implemented </br>
- To minimize FLASH usage you can add these Macros before including the library, these can disable the individual endpoints and the webserver functionality. </br>
```c++
#define DISABLE_PLAYER
#define DISABLE_ALBUM
#define DISABLE_ARTIST
#define DISABLE_AUDIOBOOKS
#define DISABLE_CATEGORIES
#define DISABLE_CHAPTERS
#define DISABLE_EPISODES
#define DISABLE_GENRES
#define DISABLE_MARKETS
#define DISABLE_PLAYLISTS
#define DISABLE_SEARCH
#define DISABLE_SHOWS
#define DISABLE_TRACKS
#define DISABLE_USER
#define DISABLE_SIMPLIFIED
#define DISABLE_WEB_SERVER