Files
2026-04-11 14:48:23 +02:00

104 lines
2.9 KiB
Arduino

#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <SpotifyArduino.h>
#include <ArduinoJson.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// --- KONFIGURATION ---
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define SCREEN_ADDRESS 0x3C
const char* ssid = "Wayne Manor";
const char* password = "einfach10zeichen!";
// Spotify Credentials
char clientId[] = "510f19fec68e42f39aad137b7666433f";
char clientSecret[] = "f13ea80ae8d641a0a449e84cbac7efde";
char refreshToken[] = "AQB5Orf0ymkJabrduB8uxYgtrwoUrrY6Y5upjoiyyQm7pklhFSzWuqX-fdLUZprQjpZ17seJja0wuHYM0bGPCUas22jcla-evEqu6PhtJkLRealUn8L9w2D8TZlijxfON0I";
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
WiFiClientSecure client;
SpotifyArduino spotify(client, clientId, clientSecret, refreshToken);
// Globale Variablen
char globalScrollText[256] = "Warte auf Spotify...";
int16_t scrollPos = SCREEN_WIDTH;
unsigned long lastSpotifyUpdate = 0;
const unsigned long spotifyInterval = 30000; // Alle 5 Sek. prüfen
// Hilfsvariable zur Berechnung der Textbreite
int16_t textWidth = 0;
void printSongDetails(CurrentlyPlaying currentlyPlaying) {
String track = String(currentlyPlaying.trackName);
String artist = String(currentlyPlaying.artists[0].artistName);
snprintf(globalScrollText, sizeof(globalScrollText), "+++ %s - by %s +++ ", track.c_str(), artist.c_str());
// Breite neu berechnen (TextSize 2 -> 12 Pixel pro Zeichen)
textWidth = strlen(globalScrollText) * 12;
}
void updateDisplay() {
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
display.setTextWrap(false);
// Laufschrift anzeigen
display.setTextSize(2);
display.setCursor(scrollPos, 20); // Mittig platziert
display.print(globalScrollText);
display.display();
// Scrollen
scrollPos -= 3; // Erhöhe für schnelleres Scrollen
if (scrollPos < -textWidth && textWidth > 0) {
scrollPos = SCREEN_WIDTH;
}
}
void setup() {
Serial.begin(115200);
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
for(;;);
}
display.clearDisplay();
display.setTextSize(1);
display.setCursor(0,10);
display.print("WiFi Connecting...");
display.display();
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
client.setInsecure();
display.clearDisplay();
display.setCursor(0,10);
display.print("Spotify Online");
display.display();
delay(1000);
}
void loop() {
// Spotify Abfrage
if (millis() - lastSpotifyUpdate > spotifyInterval) {
// Dieser Aufruf blockiert leider kurzzeitig die CPU
int status = spotify.getCurrentlyPlaying(printSongDetails, "DE");
if (status == 204) {
snprintf(globalScrollText, sizeof(globalScrollText), "Keine Musik... ");
textWidth = strlen(globalScrollText) * 12;
}
lastSpotifyUpdate = millis();
}
// Display aktualisieren
updateDisplay();
delay(20);
}