Created
February 3, 2021 23:59
-
-
Save DJSundog/924df46af93b813f581b075c53a51fc7 to your computer and use it in GitHub Desktop.
now playing in the toot-lab on an M5Paper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <M5EPD.h> | |
#include <WiFi.h> | |
#include <WiFiClientSecure.h> | |
#include <ArduinoJson.h> | |
#include <Regexp.h> | |
#include <MD5Builder.h> | |
M5EPD_Canvas canvas(&M5.EPD); | |
WiFiClientSecure client; | |
rtc_time_t RTCtime; | |
rtc_date_t RTCDate; | |
char timeStrbuff[64]; | |
// landscape | |
int screenWidth = 960; | |
int screenHeight = 540; | |
char ssid[] = "YOUR_WIFI_SSID"; | |
char psk[] = "YOUR_WIFI_PRESHARED_KEY"; | |
char lastPlaying[1000] = ""; // store nowplaying data to minimize screen updates | |
char nowPlaying[1000] = ""; | |
const int checkInterval = 10000; // check nowplaying every 10 seconds | |
int currentMillis = 0; | |
int lastCheckMillis = 0; | |
StaticJsonDocument<64> filter; | |
bool hasUpdate = false; | |
void flushTime(){ | |
M5.RTC.getTime(&RTCtime); | |
M5.RTC.getDate(&RTCDate); | |
sprintf(timeStrbuff,"%d/%02d/%02d %02d:%02d:%02d", | |
RTCDate.year,RTCDate.mon,RTCDate.day, | |
RTCtime.hour,RTCtime.min,RTCtime.sec); | |
if (hasUpdate) { | |
canvas.pushCanvas(200, 100, UPDATE_MODE_DU4); | |
canvas.deleteCanvas(); | |
hasUpdate = false; | |
} | |
canvas.createCanvas(400, 40); | |
canvas.setTextDatum(TL_DATUM); | |
canvas.setTextSize(3); | |
canvas.drawString(timeStrbuff, 0, 0); | |
canvas.pushCanvas(screenWidth - 390, screenHeight - 40, UPDATE_MODE_DU4); | |
canvas.deleteCanvas(); | |
if (currentMillis - checkInterval > lastCheckMillis) | |
updateNowPlaying(); | |
} | |
void updateNowPlaying() { | |
lastCheckMillis = currentMillis; | |
client.connect("toot-lab.reclaim.technology", 443); | |
if (!client.connected()) { | |
Serial.println(F("Could not connect!")); | |
} else { | |
client.println(F("GET /users/nowplaying/outbox.json?page=true HTTP/1.0")); | |
client.println(F("Host: toot-lab.reclaim.technology")); | |
client.println(F("Connection: close")); | |
if (client.println() == 0) { | |
Serial.println(F("Failed to send request")); | |
client.stop(); | |
return; | |
} | |
while (client.connected()) { | |
String line = client.readStringUntil('\n'); | |
if (line == "\r") { | |
Serial.println("headers received"); | |
break; | |
} | |
} | |
// Parse JSON object | |
DynamicJsonDocument doc(102400); | |
DeserializationError error = deserializeJson(doc, client, DeserializationOption::Filter(filter)); | |
if (error) { | |
Serial.print(F("deserializeJson() failed: ")); | |
Serial.println(error.f_str()); | |
client.stop(); | |
return; | |
} | |
client.stop(); | |
// Extract content | |
strcpy(nowPlaying, (char *) doc["orderedItems"][0]["object"]["content"].as<char*>()); | |
if (strcmp(nowPlaying, lastPlaying) != 0) { | |
Serial.println("New nowplaying info!"); | |
hasUpdate = true; | |
strcpy(lastPlaying, nowPlaying); | |
char songMatches[1200]; | |
char songTitle[200]; | |
char songArtists[500]; | |
char songAlbum[500]; | |
MatchState ms (nowPlaying); | |
ms.Match("<p>"(.*)" by (.*) from the album "(.*)""); | |
ms.GetMatch(songMatches); | |
Serial.println("Song title:"); | |
Serial.println(ms.GetCapture(songTitle, 0)); | |
Serial.println("Song artists:"); | |
Serial.println(ms.GetCapture(songArtists, 1)); | |
Serial.println("Song album:"); | |
Serial.println(ms.GetCapture(songAlbum, 2)); | |
// update the display | |
canvas.createCanvas(760, 390); | |
canvas.setTextDatum(TL_DATUM); | |
canvas.setTextSize(4); | |
canvas.setTextArea(0, 0, 760, 390); | |
canvas.setTextWrap(true, false); | |
canvas.setTextLineSpace(5); | |
canvas.setCursor(0, 30); | |
canvas.print(songTitle); | |
canvas.setCursor(0, 180); | |
canvas.print(songArtists); | |
canvas.setTextSize(3); | |
canvas.setCursor(0, 330); | |
canvas.print(songAlbum); | |
} | |
doc.clear(); | |
} | |
} | |
void setup() | |
{ | |
M5.begin(); | |
M5.EPD.SetRotation(0); | |
M5.EPD.Clear(true); | |
WiFi.begin(ssid, psk); | |
while(!WiFi.isConnected()) { | |
Serial.println("Connecting to wifi..."); | |
delay(500); | |
} | |
// initialize filter for arduinojson to limit memory use | |
filter["orderedItems"][0]["object"]["content"] = true; | |
// set up static text displays | |
canvas.createCanvas(960, 540); | |
canvas.setTextDatum(TL_DATUM); | |
canvas.setTextSize(5); | |
canvas.drawString("NOW PLAYING IN THE TOOT LAB", 5, 5); | |
canvas.setTextSize(4); | |
canvas.drawString("Title:", 10, 130); | |
canvas.drawString("Artist:", 10, 280); | |
canvas.setTextSize(3); | |
canvas.drawString("Album:", 10, 430); | |
canvas.pushCanvas(0, 0, UPDATE_MODE_GC16); | |
canvas.deleteCanvas(); | |
Serial.println("Initialized."); | |
} | |
void loop() | |
{ | |
currentMillis = millis(); | |
flushTime(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[env:m5stack-fire] | |
platform = espressif32 | |
board = m5stack-fire | |
framework = arduino | |
upload_speed = 2000000 | |
monitor_speed = 115200 | |
build_flags = | |
-DCORE_DEBUG_LEVEL=4 | |
-DBOARD_HAS_PSRAM | |
-mfix-esp32-psram-cache-issue | |
lib_deps = | |
m5stack/M5EPD | |
bblanchon/ArduinoJson | |
nickgammon/Regexp |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment