#include #include #include #include #include #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 32 // OLED display height, in pixels Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); #define MPDHOST "music.yannik.intern.yannikenss.de" #define MPDPORT 6600 #define BUTTON_NEXT 14 #define BUTTON_PAUSE 12 #define BUTTON_VOLUP 13 #define BUTTON_VOLDN 0 const char* ssid = "hiddennetworkforchromecast"; const char* password = "raisingelephantsissoutterlyboring"; WiFiClient espClient; // 'volume', 32x32px const unsigned char volume_icon [] PROGMEM = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x02, 0x07, 0xc0, 0x00, 0x07, 0x01, 0xe0, 0x00, 0x0f, 0x00, 0x70, 0x00, 0x1f, 0x18, 0x38, 0x00, 0x3f, 0x1e, 0x1c, 0x00, 0x7f, 0x0f, 0x1c, 0x00, 0xff, 0x03, 0x8e, 0x01, 0xff, 0x01, 0x8e, 0xff, 0xff, 0x71, 0xc6, 0xff, 0xff, 0x78, 0xc7, 0xff, 0xff, 0x38, 0xe7, 0xff, 0xff, 0x1c, 0x63, 0xff, 0xff, 0x1c, 0x63, 0xff, 0xff, 0x1c, 0x63, 0xff, 0xff, 0x1c, 0x63, 0xff, 0xff, 0x38, 0xe7, 0xff, 0xff, 0x78, 0xc7, 0xff, 0xff, 0x71, 0xc6, 0x03, 0xff, 0x01, 0x86, 0x00, 0xff, 0x03, 0x8e, 0x00, 0x7f, 0x0f, 0x1c, 0x00, 0x3f, 0x1e, 0x1c, 0x00, 0x1f, 0x18, 0x38, 0x00, 0x0f, 0x00, 0x70, 0x00, 0x07, 0x01, 0xe0, 0x00, 0x02, 0x07, 0xc0, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00 }; // 'play_pause-512', 128x32px const unsigned char playpause_icon [] PROGMEM = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38, 0x00, 0x3c, 0x3c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3e, 0x00, 0x3c, 0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x3c, 0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xc0, 0x3c, 0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xf0, 0x3c, 0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xfc, 0x3c, 0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xfe, 0x3c, 0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xfe, 0x3c, 0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xfc, 0x3c, 0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xf0, 0x3c, 0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xc0, 0x3c, 0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x3c, 0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3e, 0x00, 0x3c, 0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38, 0x00, 0x3c, 0x3c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; // 'bitmap', 128x32px const unsigned char fastforward_icon [] PROGMEM = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x01, 0x00, 0x00, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x01, 0xc0, 0x00, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xc0, 0x01, 0xf0, 0x00, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xe0, 0x01, 0xf8, 0x00, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xf8, 0x01, 0xfe, 0x00, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xfe, 0x01, 0xff, 0x80, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xff, 0x81, 0xff, 0xe0, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xff, 0xc1, 0xff, 0xf0, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xff, 0xf1, 0xff, 0xfc, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xff, 0xfd, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xff, 0xfd, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xff, 0xf1, 0xff, 0xfc, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xff, 0xc1, 0xff, 0xf0, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xff, 0x81, 0xff, 0xe0, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xfe, 0x01, 0xff, 0x80, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xf8, 0x01, 0xfe, 0x00, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xe0, 0x01, 0xf8, 0x00, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xc0, 0x01, 0xf0, 0x00, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x01, 0xc0, 0x00, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x01, 0x00, 0x00, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; bool flag_next = false; bool flag_pause = false; bool flag_volup = false; bool flag_voldn = false; bool inhibit = false; Ticker inhibit_ticker; void reset_inhibit() { inhibit = false; } void set_flag_pause() { if (!inhibit) { flag_pause = true; inhibit = true; inhibit_ticker.once_ms(1000, reset_inhibit); } } void set_flag_next() { if (!inhibit) { flag_next = true; inhibit = true; inhibit_ticker.once_ms(1000, reset_inhibit); } } void set_flag_volup() { if (!inhibit) { flag_volup = true; inhibit = true; inhibit_ticker.once_ms(1000, reset_inhibit); } } void set_flag_voldn() { if (!inhibit) { flag_voldn = true; inhibit = true; inhibit_ticker.once_ms(1000, reset_inhibit); } } void setup() { Serial.begin(9600); // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x32 Serial.println(F("SSD1306 allocation failed")); for(;;); // Don't proceed, loop forever } // Clear the buffer display.clearDisplay(); display.display(); setup_wifi(); delay(1000); pinMode(BUTTON_NEXT, INPUT_PULLUP); pinMode(BUTTON_PAUSE, INPUT_PULLUP); pinMode(BUTTON_VOLUP, INPUT_PULLUP); pinMode(BUTTON_VOLDN, INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(BUTTON_NEXT), set_flag_next, FALLING); attachInterrupt(digitalPinToInterrupt(BUTTON_PAUSE), set_flag_pause, FALLING); attachInterrupt(digitalPinToInterrupt(BUTTON_VOLUP), set_flag_volup, FALLING); attachInterrupt(digitalPinToInterrupt(BUTTON_VOLDN), set_flag_voldn, FALLING); display_song_info(); } void loop() { for (int i = 0; i<10; i++) { delay(100); if (flag_next) { next_song(); } else if (flag_pause) { toggle_pause(); } else if (flag_volup) { display_volume(); volume_up(); delay(10); display_volume(); delay(500); while (!digitalRead(BUTTON_VOLUP)) { volume_up(); delay(10); display_volume(); delay(250); } } else if (flag_voldn) { display_volume(); delay(10); volume_down(); display_volume(); delay(500); while (!digitalRead(BUTTON_VOLDN)) { volume_down(); delay(10); display_volume(); delay(250); } } flag_next = false; flag_pause = false; flag_volup = false; flag_voldn = false; } display_song_info(); } void display_error(char* error) { display.clearDisplay(); display.setCursor(0, 0); display.println("Error:"); display.println(error); display.display(); } void send_command(String command) { WiFiClient client; client.connect(MPDHOST, MPDPORT); client.println(command); while (client.available()) { Serial.write(client.read()); } client.stop(); } void next_song() { display.clearDisplay(); display.drawBitmap(0, 0, fastforward_icon, 128 ,32, WHITE); display.display(); send_command("next"); delay(250); } void toggle_pause() { display.clearDisplay(); display.drawBitmap(0, 0, playpause_icon, 128 ,32, WHITE); display.display(); send_command("pause"); delay(250); } void volume_up() { send_command("volume 1"); } void volume_down() { send_command("volume -1"); } int get_volume() { WiFiClient client; String vol_str; if (client.connect(MPDHOST, MPDPORT)) { Serial.println("connected"); client.print("status\n"); while (client.connected() || client.available()) { if (client.available()) { String line = client.readStringUntil('\n'); if (line.startsWith("volume: ")) { vol_str = line.substring(8); } else if (line.equals("OK")) { client.stop(); } } } client.stop(); Serial.println("connection done"); } else { display_error("mpd connect failed"); client.stop(); return; } return atoi(vol_str); } void display_volume() { WiFiClient client; String volume; if (client.connect(MPDHOST, MPDPORT)) { Serial.println("connected"); client.print("status\n"); while (client.connected() || client.available()) { if (client.available()) { String line = client.readStringUntil('\n'); if (line.startsWith("volume: ")) { volume = line.substring(8); } else if (line.equals("OK")) { client.stop(); } } } client.stop(); Serial.println("connection done"); } else { display_error("mpd connect failed"); client.stop(); return; } display.clearDisplay(); display.drawBitmap(0, 0, volume_icon, 32, 32, WHITE); display.setTextSize(2); // Draw 2X-scale text display.setTextColor(WHITE); display.setCursor(40, 8); display.setTextWrap(false); display.println(volume); display.display(); // Show initial text } void display_song_info() { WiFiClient client; String title; String artist; if (client.connect(MPDHOST, MPDPORT)) { Serial.println("connected"); client.print("currentsong\n"); while (client.connected() || client.available()) { if (client.available()) { String line = client.readStringUntil('\n'); if (line.startsWith("Title: ")) { title = line.substring(7); } else if (line.startsWith("Artist: ")) { artist = line.substring(8); } else if (line.equals("OK")) { client.stop(); } } } client.stop(); Serial.println("connection done"); } else { display_error("mpd connect failed"); client.stop(); return; } display.clearDisplay(); display.setTextSize(2); // Draw 2X-scale text display.setTextColor(WHITE); display.setCursor(0, 0); display.setTextWrap(false); display.println(title); display.println(artist); display.display(); // Show initial text } void setup_wifi() { display.clearDisplay(); display.setTextColor(WHITE); display.setTextSize(1); display.setCursor(0, 0); display.setTextWrap(true); delay(10); // We start by connecting to a WiFi network display.print("Network: "); display.print(ssid); display.display(); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); display.print("."); display.display(); } display.println(""); display.println("WiFi connected"); display.print("IP: "); display.println(WiFi.localIP()); display.display(); }