Uploaded main.ino

Initial commit
This commit is contained in:
rintyuu 2024-05-02 20:05:23 -07:00
commit 8a3451e1a1

112
main.ino Normal file
View file

@ -0,0 +1,112 @@
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <SpotifyArduinoCert.h>
#include <SpotifyArduino.h>
#include <ArduinoJson.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
char ssid[] = ""; // your network SSID (name)
char password[] = ""; // your network password
char clientId[] = ""; // Your client ID of your Spotify APP
char clientSecret[] = ""; // Your client Secret of your Spotify APP (Do Not share this!)
// Country code, including this is advisable
#define SPOTIFY_MARKET "US"
#define SPOTIFY_REFRESH_TOKEN "" // a really long string of characters, do some reasearch to find refresh token
WiFiClientSecure client;
SpotifyArduino spotify(client, clientId, clientSecret, SPOTIFY_REFRESH_TOKEN);
unsigned long delayBetweenRequests = 10000; // Time between requests (10 seconds)
unsigned long requestDueTime; // Time when request is due
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
#if defined(ESP8266)
client.setFingerprint(SPOTIFY_FINGERPRINT); // These expire every few months
#elif defined(ESP32)
client.setCACert(spotify_server_cert);
#endif
// ... or don't!
//client.setInsecure();
// If you want to enable some extra debugging
// uncomment the "#define SPOTIFY_DEBUG" in SpotifyArduino.h
Serial.println("Refreshing Access Tokens");
if (!spotify.refreshAccessToken())
{
Serial.println("Failed to get access tokens");
}
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
delay(2000);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
}
void printCurrentlyPlayingToDisplay(CurrentlyPlaying currentlyPlaying) {
display.clearDisplay();
display.setCursor(0, 10);
display.println("Currently Playing:");
display.println(currentlyPlaying.trackName);
display.println("by");
display.println(currentlyPlaying.artists[0].artistName); // Assuming only one artist is printed
display.display();
}
void loop()
{
if (millis() > requestDueTime)
{
Serial.print("Free Heap: ");
Serial.println(ESP.getFreeHeap());
Serial.println("Getting currently playing song:");
// Market can be excluded if you want e.g. spotify.getCurrentlyPlaying()
int status = spotify.getCurrentlyPlaying(printCurrentlyPlayingToDisplay, SPOTIFY_MARKET);
if (status == 200)
{
Serial.println("Successfully got currently playing");
}
else if (status == 204)
{
Serial.println("Doesn't seem to be anything playing");
}
else
{
Serial.print("Error: ");
Serial.println(status);
}
requestDueTime = millis() + delayBetweenRequests;
}
}