use persistent connection for polling
This commit is contained in:
@@ -22,7 +22,7 @@ Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
|
|||||||
const char* ssid = "hiddennetworkforchromecast";
|
const char* ssid = "hiddennetworkforchromecast";
|
||||||
const char* password = "raisingelephantsissoutterlyboring";
|
const char* password = "raisingelephantsissoutterlyboring";
|
||||||
|
|
||||||
WiFiClient espClient;
|
WiFiClient songinfoClient;
|
||||||
|
|
||||||
struct song_info {
|
struct song_info {
|
||||||
String title;
|
String title;
|
||||||
@@ -266,37 +266,34 @@ int get_player_info(player_info* player_info) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int get_song_info(song_info* song_info) {
|
int get_song_info(song_info* song_info) {
|
||||||
WiFiClient client;
|
if (!songinfoClient.connected()) {
|
||||||
|
Serial.println("reconnect");
|
||||||
|
if (!songinfoClient.connect(MPDHOST, MPDPORT)) {
|
||||||
|
display_error("mpd connect failed");
|
||||||
|
Serial.println("mpd connect failed");
|
||||||
|
songinfoClient.stop();
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (client.connect(MPDHOST, MPDPORT)) {
|
songinfoClient.print("currentsong\n");
|
||||||
Serial.println("connected");
|
|
||||||
client.print("currentsong\n");
|
|
||||||
|
|
||||||
while (client.connected() || client.available())
|
while (songinfoClient.connected() || songinfoClient.available())
|
||||||
{
|
{
|
||||||
if (client.available())
|
if (songinfoClient.available())
|
||||||
{
|
{
|
||||||
String line = client.readStringUntil('\n');
|
String line = songinfoClient.readStringUntil('\n');
|
||||||
if (line.startsWith("Title: ")) {
|
if (line.startsWith("Title: ")) {
|
||||||
song_info->title = line.substring(7);
|
song_info->title = line.substring(7);
|
||||||
}
|
|
||||||
else if (line.startsWith("Artist: ")) {
|
|
||||||
song_info->artist = line.substring(8);
|
|
||||||
}
|
|
||||||
else if (line.equals("OK")) {
|
|
||||||
client.stop();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
else if (line.startsWith("Artist: ")) {
|
||||||
client.stop();
|
song_info->artist = line.substring(8);
|
||||||
Serial.println("connection done");
|
}
|
||||||
}
|
else if (line.equals("OK")) {
|
||||||
else {
|
break;
|
||||||
display_error("mpd connect failed");
|
}
|
||||||
client.stop();
|
}
|
||||||
return 1;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user