Updated readme and updated the examples

This commit is contained in:
Chaerne
2024-02-20 10:59:16 +01:00
parent 548ab16059
commit ca7b16e22e
6 changed files with 174 additions and 79 deletions

103
README.md
View File

@@ -2,37 +2,90 @@
This library is a wrapper for the [Spotify Web API](https://developer.spotify.com/documentation/web-api/) and is designed to work with the [ESP32](https://www.espressif.com/en/products/socs/esp32/overview) microcontroller.
## Dependencies
- [ArduinoJson](https://arduinojson.org/) </br>
- [WebServer](https://github.com/espressif/arduino-esp32/blob/master/libraries/WebServer/src/WebServer.h) </br>
- [HTTPClient](https://github.com/amcewen/HttpClient) </br>
- [WiFi](https://www.arduino.cc/en/Reference/WiFi) </br>
- [UrlEncode](https://github.com/plageoj/urlencode) </br>
## Setup
1. Create a new application on the [Spotify Developer Dashboard](https://developer.spotify.com/dashboard/applications) and copy the Client ID and Client Secret. </br>
2. Get a refresh token with the needed scopes. (You can do that with this [tool](https://spotify-refresh-token-generator.netlify.app/#welcome)) </br>
3. Save refresh token, client id, client secret and redirect uri in your main.cpp or project_name.ino file. </br>
4. Create an instance of the Spotify class: </br>
1. Create a new application on the [Spotify Developer Dashboard](https://developer.spotify.com/dashboard/applications) 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. </br>
2. Now you will have to use the login without a refresh token which can be implemented the following way:
```c++
Spotify sp("refresh_token", "client_id", "client_secret", "redirect_uri", "bool debug", "int number of retries");
```
Debug and number of retries are optional. Debug is false as default and retries is set to 3. </br>
5. Connect to Wifi: </br>
```c++
void connectToWifi(){
WiFi.begin("your_ssid", "your_password");§
Serial.print("Connecting to WiFi...");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println();
Serial.println("Connected to WiFi");
#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");
}
```
This is a simple example using the WiFi library. </br>
6. Now you can call your needed functions on the Spotify instance. The functions have doxygen comments, so you can read them in your IDE. The functions are also corresponding to the [Spotify Web API](https://developer.spotify.com/documentation/web-api/). </br>
7. The normal functions return an response object. You can get the http code of type int with ```response_obj.status_code``` and the response message of type String with ```response_obj.reply```. </br>
3. When exectuting this code you will have to open the serial monitor and copy the url into your browser. On this page you Login with your spotify account (Most endpoints only work with premium) and authorize your application to access your data. Now you will be redirected to page where your refresh token will be shown. So you don't have to open the browser every time you restart your esp i would recommend to copy the refresh token and use the other constructor afterwards:
```c++
#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";
const char* REFRESH_TOKEN = "your_refresh_token";
//Create an instance of the Spotify class Optional: Debug mode and number of retries
Spotify sp(CLIENT_ID, CLIENT_SECRET, REFRESH_TOKEN);
void setup() {
Serial.begin(115200);
connect_to_wifi();//Connect to your wifi
}
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");
}
```
4. Now you can use the library. </br>
## Usage
- The normal functions return an response object. You can get the http code of type int with ```response_obj.status_code``` and the response message of type String with ```response_obj.reply```. </br>
To print the response you can use the ```print_response(response_obj)``` function. </br>
8. There are also some helper and some simplified functions. </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>
- There are also some helper and some simplified functions. </br>
```c++
// Get the current track name and return it as String
String current_track_name();
@@ -59,7 +112,11 @@ To print the response you can use the ```print_response(response_obj)``` functio
//Convert a context id to a uri and return it as a pointer to the char
char* convert_id_to_uri(char* id, char* type, char* uri);
```
9. You can also include the namespace: ```using namespace Spotify_types;```. These are some types used in the library eg. TYPES, SIZES of uris/ids </br>
- You can also include the namespace: ```using namespace Spotify_types;```. These are some types used in the library eg. TYPES, SIZES of uris/ids </br>
## Trouble Shooting
- If you have any problems with the library you can use the debug mode to print out the data to the serial monitor. I recommend not setting the baud rate lower than 115200 as the data printed can be quite large which can lead to crash if the Serial communication is too slow </br>
- If you have any problems with the library you can also use the [Spotify Web API Console](https://developer.spotify.com/console/) to test the endpoints. </br>
- If there are still issues you can open an issue on this repository. </br>
### Working Devices
- ESP32 WROOM</br>
- Should also work on ESP2866 and other ESP32 models(Untested).</br>

View File

@@ -0,0 +1,39 @@
/*
An example of how to use the Spotify library using a refresh token.
This example is useful as it does not require the user to authenticate in the browser every time they want to use the library.
20.02.2024
Created by: Finian Landes
Documentation: https://github.com/FinianLandes/Spotify_Esp32
*/
// Include the required libraries
#include <Arduino.h>
#include <WiFi.h>
#include "SpotifyESP32.h"
const char* SSID = "YOUR WIFI SSID";
const char* PASSWORD = "YOUR WIFI PASSWORD";
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, REFRESH_TOKEN);
void setup() {
Serial.begin(115200);
connect_to_wifi();
}
void loop() {
// put your main code here, to run repeatedly:
}
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");
}

View File

@@ -1,25 +0,0 @@
#include <Arduino.h>
#include <WiFi.h>
#include "SpotifyESP32.h"
#include "config.h"
using namespace Spotify_types;
Spotify sp(CLIENT_ID, CLIENT_SECRET, REFRESH_TOKEN);
void setup() {
Serial.begin(115200);
connect_to_wifi();
}
void loop() {
// put your main code here, to run repeatedly:
}
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");
}

View File

@@ -0,0 +1,45 @@
/*
An example of how to authenticate with Spotify without using a refresh token.
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.
20.02.2024
Created by: Finian Landes
Documentation: https://github.com/FinianLandes/Spotify_Esp32
*/
// Include the required libraries
#include <Arduino.h>
#include <WiFi.h>
#include "SpotifyESP32.h"
const char* SSID = "YOUR WIFI SSID";
const char* PASSWORD = "YOUR WIFI PASSWORD";
const char* CLIENT_ID = "YOUR CLIENT ID FROM THE SPOTIFY DASHBOARD";
const char* CLIENT_SECRET = "YOUR CLIENT SECRET FROM THE SPOTIFY DASHBOARD";
Spotify sp(CLIENT_ID, CLIENT_SECRET);
void setup() {
Serial.begin(115200);
connect_to_wifi();
sp.begin();
while(!sp.is_auth()){
sp.handle_client();
}
Serial.println("Authenticated");
}
void loop() {
// put your main code here, to run repeatedly:
}
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");
}

View File

@@ -1,31 +0,0 @@
#include <Arduino.h>
#include <WiFi.h>
#include "SpotifyESP32.h"
#include "config.h"
using namespace Spotify_types;
Spotify sp(CLIENT_ID, CLIENT_SECRET);
void setup() {
Serial.begin(115200);
connect_to_wifi();
sp.begin();
while(!sp.is_auth()){
sp.handle_client();
}
Serial.println("Authenticated");
}
void loop() {
// put your main code here, to run repeatedly:
}
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");
}

10
library.properties Normal file
View File

@@ -0,0 +1,10 @@
name = Spotify_Esp32
version = 1.0.0
author = Finian Landes, Pia Piekarek
maintainer = Finian Landes <landesfinian@gmail.com>
sentence = A client to interact with the Spotify API
paragraph = This library provides a client to interact with the Spotify API. It is designed to be used with the ESP32 microcontroller.
category = Communication
url = https://github.com/FinianLandes/Spotify_Esp32
architectures = *
depends = WiFi, WiFiClientSecure, HTTPClient, ArduinoJson, UrlEncode, WebServer, base64