├── MPD_Client_Toggle ├── README.md └── MPD_Client_Toggle.ino ├── MPD_Client_Keep_Connect ├── README.md └── MPD_Client_Keep_Connect.ino ├── MPD_Client_Play_Forever ├── README.md └── MPD_Client_Play_Forever.ino ├── MPD_Client_List_Playlists ├── README.md └── MPD_Client_List_Playlists.ino ├── MPD_Client_Load_Playlist ├── README.md └── MPD_Client_Load_Playlist.ino ├── MPD_Client_1602LCD_Display ├── README.md └── MPD_Client_1602LCD_Display.ino ├── MPD_Client_Remote ├── README.md └── MPD_Client_Remote.ino ├── MPD_Client_Remote_1602LCD_Display ├── README.md └── MPD_Client_Remote_1602LCD_Display.ino ├── MPD_Client_DotMatrix ├── README.md └── MPD_Client_DotMatrix.ino └── README.md /MPD_Client_Toggle/README.md: -------------------------------------------------------------------------------- 1 | You have to change these environment. 2 | 3 | ``` 4 | char* ssid = "Your AP's SSID"; 5 | char* password = "Your AP's PASSWORD"; 6 | uint16_t port = 6600; 7 | char * host = "192.168.10.45"; // ip or dns 8 | ``` 9 | -------------------------------------------------------------------------------- /MPD_Client_Keep_Connect/README.md: -------------------------------------------------------------------------------- 1 | You have to change these environment. 2 | 3 | ``` 4 | char* ssid = "Your AP's SSID"; 5 | char* password = "Your AP's PASSWORD"; 6 | uint16_t port = 6600; 7 | char * host = "192.168.10.45"; // ip or dns 8 | ``` 9 | -------------------------------------------------------------------------------- /MPD_Client_Play_Forever/README.md: -------------------------------------------------------------------------------- 1 | You have to change these environment. 2 | 3 | ``` 4 | char* ssid = "Your AP's SSID"; 5 | char* password = "Your AP's PASSWORD"; 6 | uint16_t port = 6600; 7 | char * host = "192.168.10.45"; // ip or dns 8 | ``` 9 | -------------------------------------------------------------------------------- /MPD_Client_List_Playlists/README.md: -------------------------------------------------------------------------------- 1 | You have to change these environment. 2 | 3 | ``` 4 | char* ssid = "Your AP's SSID"; 5 | char* password = "Your AP's PASSWORD"; 6 | uint16_t port = 6600; 7 | char * host = "192.168.10.45"; // ip or dns 8 | ``` 9 | -------------------------------------------------------------------------------- /MPD_Client_Load_Playlist/README.md: -------------------------------------------------------------------------------- 1 | You have to change these environment. 2 | 3 | ``` 4 | char* ssid = "Your AP's SSID"; 5 | char* password = "Your AP's PASSWORD"; 6 | uint16_t port = 6600; 7 | char * host = "192.168.10.45"; // ip or dns 8 | ``` 9 | -------------------------------------------------------------------------------- /MPD_Client_1602LCD_Display/README.md: -------------------------------------------------------------------------------- 1 | # You have to change these environment. 2 | 3 | ``` 4 | char* ssid = "Your AP's SSID"; 5 | char* password = "Your AP's PASSWORD"; 6 | uint16_t port = 6600; 7 | char * host = "192.168.10.45"; // ip or dns 8 | ``` 9 | 10 | # It needs to match your LCD type. 11 | 12 | ``` 13 | #define lcdRows 2 14 | //#define lcdRows 4 15 | ``` 16 | 17 | # Circuit diagram 18 | 19 | ![WeMos-1602LCD-GPIO-BackLight](https://user-images.githubusercontent.com/6020549/68948062-6b33d980-07fa-11ea-8b4b-56a763fb121a.jpg) 20 | 21 | -------------------------------------------------------------------------------- /MPD_Client_Remote/README.md: -------------------------------------------------------------------------------- 1 | # You have to change these environment. 2 | 3 | ``` 4 | char* ssid = "Your AP's SSID"; 5 | char* password = "Your AP's PASSWORD"; 6 | uint16_t port = 6600; 7 | char * host = "192.168.10.45"; // ip or dns 8 | ``` 9 | 10 | # It needs to match your remote control. 11 | 12 | ``` 13 | #define PLAY 0x00FF18E7 14 | #define STOP 0x00FF38C7 15 | #define PREV 0x00FF10EF 16 | #define NEXT 0x00FF5AA5 17 | #define VOLU 0x00FF52AD 18 | #define VOLD 0x00FF42BD 19 | ``` 20 | 21 | # Circuit diagram 22 | 23 | ![WeMos-IRrecv](https://user-images.githubusercontent.com/6020549/68649297-aff31280-0565-11ea-925f-f6302046c92b.jpg) 24 | 25 | # Required library 26 | https://github.com/crankyoldgit/IRremoteESP8266 27 | -------------------------------------------------------------------------------- /MPD_Client_Remote_1602LCD_Display/README.md: -------------------------------------------------------------------------------- 1 | # You have to change these environment. 2 | 3 | ``` 4 | char* ssid = "Your AP's SSID"; 5 | char* password = "Your AP's PASSWORD"; 6 | uint16_t port = 6600; 7 | char * host = "192.168.10.45"; // ip or dns 8 | ``` 9 | 10 | # It needs to match your remote control. 11 | 12 | ``` 13 | #define PLAY 0x00FF18E7 14 | #define STOP 0x00FF38C7 15 | #define PREV 0x00FF10EF 16 | #define NEXT 0x00FF5AA5 17 | #define VOLU 0x00FF52AD 18 | #define VOLD 0x00FF42BD 19 | ``` 20 | 21 | # It needs to match your LCD type. 22 | 23 | ``` 24 | #define lcdRows 2 25 | //#define lcdRows 4 26 | ``` 27 | 28 | # Circuit diagram 29 | 30 | ![WeMos-1602LCD-GPIO-BackLight-Remote](https://user-images.githubusercontent.com/6020549/68948369-1349a280-07fb-11ea-952d-1b2a1f5c13df.jpg) 31 | 32 | # Required library 33 | https://github.com/crankyoldgit/IRremoteESP8266 34 | -------------------------------------------------------------------------------- /MPD_Client_DotMatrix/README.md: -------------------------------------------------------------------------------- 1 | # You have to change these environment. 2 | 3 | ``` 4 | char* ssid = "Your AP's SSID"; 5 | char* password = "Your AP's PASSWORD"; 6 | uint16_t port = 6600; 7 | char * host = "192.168.10.45"; // ip or dns 8 | ``` 9 | 10 | # It needs to match your Matrix LED. 11 | 12 | ``` 13 | int pinCS = 15; // Attach CS to this pin, DIN to MOSI and CLK to SCK (cf http://arduino.cc/en/Reference/SPI ) 14 | int numberOfHorizontalDisplays = 4; 15 | int numberOfVerticalDisplays = 1; 16 | ``` 17 | 18 | # Wirering 19 | 20 | |DotMatrix||ESP8266| 21 | |:-:|:-:|:-:| 22 | |VCC|--|3.3V| 23 | |GND|--|GND| 24 | |DIN|--|IO13(D7)| 25 | |CS|--|IO15(D8)| 26 | |CLK|--|IO14(D5)| 27 | 28 | # Wire cable length 29 | 30 | Wire cable length should be as short as possible. 31 | 32 | 33 | ![LedMatrix-1](https://user-images.githubusercontent.com/6020549/68864658-d87d3700-0734-11ea-9c62-08c02b5a8e18.JPG) 34 | 35 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # esp8266-mpd-client 2 | MPD(MusicPlayerDaemon) client example for ESP8266/ESP32. 3 | MPD protocol is [here](https://github.com/MusicPlayerDaemon/MPD/blob/master/doc/protocol.rst). 4 | 5 | --- 6 | 7 | # MPD_Client_Toggle 8 | Judge current status and execute PLYA or STOP. 9 | 10 | # MPD_Client_Play_Forever 11 | Play current song forever. 12 | 13 | # MPD_Client_Remote 14 | Control using IR remote. 15 | 16 | # MPD_Client_1602LCD_Display 17 | Display current artist & title on LCD. 18 | 19 | # MPD_Client_Remote_1602LCD_Display 20 | Control using IR remote. 21 | Display current artist & title on LCD. 22 | 23 | # MPD_Client_DotMatrix 24 | Display current artist & title on 8x8 DotMatrix LED. 25 | 26 | # MPD_Client_List_PlayLists 27 | Show current playlists. 28 | 29 | # MPD_Client_Load_Playlist 30 | Load playlist forever. 31 | 32 | # MPD_Client_Keep_Connect 33 | If there is no communication for a certain period of time, the connection with the MPD server will be disconnected. 34 | Keep to connect to the MPD server using the STATUS command. 35 | -------------------------------------------------------------------------------- /MPD_Client_Keep_Connect/MPD_Client_Keep_Connect.ino: -------------------------------------------------------------------------------- 1 | /* 2 | * MPD Client Example for ESP8266/ESP32 3 | */ 4 | 5 | #if defined(ESP8266) 6 | #include 7 | #else 8 | #include 9 | #endif 10 | 11 | char* ssid = "Your AP's SSID"; 12 | char* password = "Your AP's PASSWORD"; 13 | uint16_t port = 6600; 14 | char * host = "192.168.10.45"; // ip or dns 15 | unsigned long lastMillis = 0; 16 | 17 | // Use WiFiClient class to create TCP connections 18 | WiFiClient client; 19 | 20 | int mpc_connect(char * host, int port) { 21 | char smsg[40]; 22 | char rmsg[40]; 23 | 24 | if (!client.connect(host, port)) { 25 | Serial.println("connection failed"); 26 | return 0; 27 | } 28 | 29 | String line; 30 | client.setTimeout(1000); 31 | line = client.readStringUntil('\0'); 32 | //Serial.print("["); 33 | //Serial.print(line); 34 | //Serial.println("]"); 35 | //Serial.println("length()=" + String(line.length())); 36 | line.toCharArray(rmsg, line.length()+1); 37 | //Serial.println("strlen()=" + String(strlen(rmsg))); 38 | rmsg[line.length()-1] = 0; 39 | Serial.println("rmsg=[" + String(rmsg) + "]"); 40 | if (strncmp(rmsg,"OK",2) == 0) return 1; 41 | return 0; 42 | } 43 | 44 | int mpc_command(char * buf) { 45 | char smsg[40]; 46 | char rmsg[40]; 47 | sprintf(smsg,"%s\n",buf); 48 | client.print(smsg); 49 | Serial.println("smsg=[" + String(buf) + "]"); 50 | 51 | String line; 52 | client.setTimeout(1000); 53 | line = client.readStringUntil('\0'); 54 | //Serial.print("["); 55 | //Serial.print(line); 56 | //Serial.println("]"); 57 | //Serial.println("length()=" + String(line.length())); 58 | line.toCharArray(rmsg, line.length()+1); 59 | //Serial.println("strlen()=" + String(strlen(rmsg))); 60 | rmsg[line.length()-1] = 0; 61 | Serial.println("rmsg=[" + String(rmsg) + "]"); 62 | if (strcmp(rmsg,"OK") == 0) return 1; 63 | return 0; 64 | } 65 | 66 | void mpc_error(char * buf) { 67 | Serial.print("mpc command error:"); 68 | Serial.println(buf); 69 | while(1) {} 70 | } 71 | 72 | void setup() { 73 | Serial.begin(115200); 74 | 75 | // We start by connecting to a WiFi network 76 | Serial.println(); 77 | Serial.println(); 78 | Serial.print("Wait for WiFi..."); 79 | WiFi.begin(ssid, password); 80 | 81 | int cnt = 0; 82 | while (WiFi.status() != WL_CONNECTED) { 83 | delay(500); 84 | Serial.print("."); 85 | cnt++; 86 | if ((cnt % 60) == 0) Serial.println(); 87 | } 88 | 89 | Serial.println(""); 90 | Serial.println("WiFi connected"); 91 | Serial.print("IP address: "); 92 | Serial.println(WiFi.localIP()); 93 | 94 | Serial.print("connecting to "); 95 | Serial.println(host); 96 | 97 | if (mpc_connect(host, port) == 0) mpc_error("connect"); 98 | 99 | lastMillis = millis(); 100 | } 101 | 102 | void loop() { 103 | // Check WiFi Status 104 | if (WiFi.status() != WL_CONNECTED) { 105 | ESP.reset(); 106 | } 107 | 108 | // Query MPD status for keep connection 109 | // Maintain the connection by periodically querying STATUS. 110 | long now = millis(); 111 | if (now < lastMillis) lastMillis = now; // millis is overflow 112 | if (now - lastMillis > 10000) { 113 | lastMillis = now; 114 | client.print("status\n"); 115 | client.setTimeout(1000); 116 | String line = client.readStringUntil('\0'); 117 | Serial.println("line.length=" + String(line.length())); 118 | //Serial.println("status=[" + line + "]"); 119 | if (line.length() == 0) ESP.reset(); 120 | } 121 | } 122 | -------------------------------------------------------------------------------- /MPD_Client_List_Playlists/MPD_Client_List_Playlists.ino: -------------------------------------------------------------------------------- 1 | /* 2 | * MPD Client Example for ESP8266/ESP32 3 | */ 4 | 5 | #if defined(ESP8266) 6 | #include 7 | #else 8 | #include 9 | #endif 10 | 11 | char* ssid = "Your AP's SSID"; 12 | char* password = "Your AP's PASSWORD"; 13 | uint16_t port = 6600; 14 | char * host = "192.168.10.45"; // ip or dns 15 | 16 | // Use WiFiClient class to create TCP connections 17 | WiFiClient client; 18 | 19 | int mpc_connect(char * host, int port) { 20 | char smsg[40]; 21 | char rmsg[40]; 22 | 23 | if (!client.connect(host, port)) { 24 | Serial.println("connection failed"); 25 | return 0; 26 | } 27 | 28 | String line; 29 | client.setTimeout(1000); 30 | line = client.readStringUntil('\0'); 31 | //Serial.print("["); 32 | //Serial.print(line); 33 | //Serial.println("]"); 34 | //Serial.println("length()=" + String(line.length())); 35 | line.toCharArray(rmsg, line.length()+1); 36 | //Serial.println("strlen()=" + String(strlen(rmsg))); 37 | rmsg[line.length()-1] = 0; 38 | Serial.println("rmsg=[" + String(rmsg) + "]"); 39 | if (strncmp(rmsg,"OK",2) == 0) return 1; 40 | return 0; 41 | } 42 | 43 | int mpc_command(char * buf) { 44 | char smsg[40]; 45 | char rmsg[40]; 46 | sprintf(smsg,"%s\n",buf); 47 | client.print(smsg); 48 | Serial.println("smsg=[" + String(buf) + "]"); 49 | 50 | String line; 51 | client.setTimeout(1000); 52 | line = client.readStringUntil('\0'); 53 | //Serial.print("["); 54 | //Serial.print(line); 55 | //Serial.println("]"); 56 | //Serial.println("length()=" + String(line.length())); 57 | line.toCharArray(rmsg, line.length()+1); 58 | //Serial.println("strlen()=" + String(strlen(rmsg))); 59 | rmsg[line.length()-1] = 0; 60 | Serial.println("rmsg=[" + String(rmsg) + "]"); 61 | if (strcmp(rmsg,"OK") == 0) return 1; 62 | return 0; 63 | } 64 | 65 | void mpc_error(char * buf) { 66 | Serial.print("mpc command error:"); 67 | Serial.println(buf); 68 | while(1) {} 69 | } 70 | 71 | int getItems(String line, int start, char * item, char * value, int len) { 72 | int pos1,pos2,pos3; 73 | Serial.println("item=[" + String(item) + "]"); 74 | pos1=line.indexOf(item, start); 75 | //Serial.println("pos1=" + String(pos1)); 76 | if (pos1 < 0) return(pos1); 77 | 78 | String line2; 79 | line2 = line.substring(pos1); 80 | pos2=line2.indexOf(":"); 81 | pos3=line2.indexOf(0x0a); 82 | //Serial.println("pos2=" + String(pos2)); 83 | //Serial.println("pos3=" + String(pos3)); 84 | String line3; 85 | line3 = line2.substring(pos2+1,pos3); 86 | //Serial.println("line3=[" + line3 + "]"); 87 | string2char(line3, value, len); 88 | Serial.println("value=[" + String(value) + "]"); 89 | //return(strlen(value)); 90 | return(pos1+pos3+1); 91 | } 92 | 93 | void string2char(String line, char * cstr4, int len) { 94 | char cstr3[256]; 95 | line.toCharArray(cstr3, line.length()+1); 96 | //Serial.println("cstr3=[" + String(cstr3) + "]"); 97 | int pos4 = 0; 98 | for (int i=0;i 11 | #else 12 | #include 13 | #endif 14 | 15 | #include 16 | #include 17 | #include 18 | 19 | // It needs to match your remote control. 20 | #define PLAY 0x00FF18E7 21 | #define STOP 0x00FF38C7 22 | #define PREV 0x00FF10EF 23 | #define NEXT 0x00FF5AA5 24 | #define VOLU 0x00FF52AD 25 | #define VOLD 0x00FF42BD 26 | 27 | // an IR detector/demodulator is connected to GPIO pin 2 28 | const uint16_t RECV_PIN = 2; 29 | 30 | IRrecv irrecv(RECV_PIN); 31 | 32 | decode_results results; 33 | 34 | char* ssid = "Your AP's SSID"; 35 | char* password = "Your AP's PASSWORD"; 36 | uint16_t port = 6600; 37 | char * host = "192.168.10.109"; // ip or dns 38 | 39 | // Use WiFiClient class to create TCP connections 40 | WiFiClient client; 41 | 42 | long lastMillis = 0; 43 | 44 | int mpc_connect(char * host, int port) { 45 | char smsg[40]; 46 | char rmsg[40]; 47 | 48 | if (!client.connect(host, port)) { 49 | Serial.println("connection failed"); 50 | return 0; 51 | } 52 | 53 | String line; 54 | client.setTimeout(1000); 55 | line = client.readStringUntil('\0'); 56 | //Serial.print("["); 57 | //Serial.print(line); 58 | //Serial.println("]"); 59 | //Serial.println("length()=" + String(line.length())); 60 | line.toCharArray(rmsg, line.length()+1); 61 | //Serial.println("strlen()=" + String(strlen(rmsg))); 62 | rmsg[line.length()-1] = 0; 63 | Serial.println("rmsg=[" + String(rmsg) + "]"); 64 | if (strncmp(rmsg,"OK",2) == 0) return 1; 65 | return 0; 66 | } 67 | 68 | int mpc_command(char * buf) { 69 | char smsg[40]; 70 | char rmsg[40]; 71 | sprintf(smsg,"%s\n",buf); 72 | client.print(smsg); 73 | Serial.println("smsg=[" + String(buf) + "]"); 74 | 75 | String line; 76 | client.setTimeout(1000); 77 | line = client.readStringUntil('\0'); 78 | //Serial.print("["); 79 | //Serial.print(line); 80 | //Serial.println("]"); 81 | //Serial.println("length()=" + String(line.length())); 82 | line.toCharArray(rmsg, line.length()+1); 83 | //Serial.println("strlen()=" + String(strlen(rmsg))); 84 | rmsg[line.length()-1] = 0; 85 | Serial.println("rmsg=[" + String(rmsg) + "]"); 86 | if (strcmp(rmsg,"OK") == 0) return 1; 87 | return 0; 88 | } 89 | 90 | void mpc_error(char * buf) { 91 | Serial.print("mpc command error:"); 92 | Serial.println(buf); 93 | while(1) {} 94 | } 95 | 96 | void setup() { 97 | Serial.begin(115200); 98 | 99 | // We start by connecting to a WiFi network 100 | Serial.println(); 101 | Serial.println(); 102 | Serial.print("Wait for WiFi..."); 103 | WiFi.begin(ssid, password); 104 | 105 | int cnt = 0; 106 | while (WiFi.status() != WL_CONNECTED) { 107 | delay(500); 108 | Serial.print("."); 109 | cnt++; 110 | if ((cnt % 60) == 0) Serial.println(); 111 | } 112 | 113 | Serial.println(""); 114 | Serial.println("WiFi connected"); 115 | Serial.print("IP address: "); 116 | Serial.println(WiFi.localIP()); 117 | 118 | while(1) { 119 | Serial.print("connecting to "); 120 | Serial.println(host); 121 | //if (mpc_connect(host, port) == 0) mpc_error("connect"); 122 | if (mpc_connect(host, port) == 1) break; 123 | delay(10*1000); 124 | } 125 | 126 | irrecv.enableIRIn(); // Start IR receiver 127 | Serial.print("IRrecvDemo is now running and waiting for IR message on Pin "); 128 | Serial.println(RECV_PIN); 129 | } 130 | 131 | 132 | void loop() { 133 | 134 | if (!client.connected()) { 135 | Serial.println("server disconencted"); 136 | delay(10*1000); 137 | ESP.restart(); 138 | } 139 | 140 | 141 | if (irrecv.decode(&results)) { 142 | serialPrintUint64(results.value, HEX); 143 | Serial.println(""); 144 | if (results.value == PLAY) { 145 | if (mpc_command("play") == 0) mpc_error("play"); 146 | } else if (results.value == STOP) { 147 | if (mpc_command("stop") == 0) mpc_error("stop"); 148 | } else if (results.value == NEXT) { 149 | if (mpc_command("play") == 0) mpc_error("play"); // Start if stopped 150 | if (mpc_command("next") == 0) mpc_error("next"); 151 | if (mpc_command("play") == 0) mpc_error("play"); 152 | } else if (results.value == PREV) { 153 | if (mpc_command("play") == 0) mpc_error("play"); // Start if stopped 154 | if (mpc_command("previous") == 0) mpc_error("previous"); 155 | if (mpc_command("play") == 0) mpc_error("play"); 156 | } else if (results.value == VOLU) { 157 | if (mpc_command("volume +10") == 0) mpc_error("volume"); 158 | } else if (results.value == VOLD) { 159 | if (mpc_command("volume -10") == 0) mpc_error("volume"); 160 | } 161 | irrecv.resume(); // Receive the next value 162 | } 163 | 164 | // Disconnected after 60 seconds of no communication 165 | long now = millis(); 166 | if (now < lastMillis) lastMillis = now; // millis is overflow 167 | if (now - lastMillis > 10000) { 168 | lastMillis = now; 169 | if (mpc_command("ping") == 0) mpc_error("ping"); 170 | } 171 | } 172 | -------------------------------------------------------------------------------- /MPD_Client_Play_Forever/MPD_Client_Play_Forever.ino: -------------------------------------------------------------------------------- 1 | /* 2 | * MPD Client Example for ESP8266/ESP32 3 | */ 4 | 5 | #if defined(ESP8266) 6 | #include 7 | #else 8 | #include 9 | #endif 10 | 11 | char* ssid = "Your AP's SSID"; 12 | char* password = "Your AP's PASSWORD"; 13 | uint16_t port = 6600; 14 | char * host = "192.168.10.45"; // ip or dns 15 | 16 | // Use WiFiClient class to create TCP connections 17 | WiFiClient client; 18 | 19 | long lastMillis = 0; 20 | 21 | int mpc_connect(char * host, int port) { 22 | char smsg[40]; 23 | char rmsg[40]; 24 | 25 | if (!client.connect(host, port)) { 26 | Serial.println("connection failed"); 27 | return 0; 28 | } 29 | 30 | String line; 31 | client.setTimeout(1000); 32 | line = client.readStringUntil('\0'); 33 | //Serial.print("["); 34 | //Serial.print(line); 35 | //Serial.println("]"); 36 | //Serial.println("length()=" + String(line.length())); 37 | line.toCharArray(rmsg, line.length()+1); 38 | //Serial.println("strlen()=" + String(strlen(rmsg))); 39 | rmsg[line.length()-1] = 0; 40 | Serial.println("rmsg=[" + String(rmsg) + "]"); 41 | if (strncmp(rmsg,"OK",2) == 0) return 1; 42 | return 0; 43 | } 44 | 45 | int mpc_command(char * buf) { 46 | char smsg[40]; 47 | char rmsg[40]; 48 | sprintf(smsg,"%s\n",buf); 49 | client.print(smsg); 50 | Serial.println("smsg=[" + String(buf) + "]"); 51 | 52 | String line; 53 | client.setTimeout(1000); 54 | line = client.readStringUntil('\0'); 55 | //Serial.print("["); 56 | //Serial.print(line); 57 | //Serial.println("]"); 58 | //Serial.println("length()=" + String(line.length())); 59 | line.toCharArray(rmsg, line.length()+1); 60 | //Serial.println("strlen()=" + String(strlen(rmsg))); 61 | rmsg[line.length()-1] = 0; 62 | Serial.println("rmsg=[" + String(rmsg) + "]"); 63 | if (strcmp(rmsg,"OK") == 0) return 1; 64 | return 0; 65 | } 66 | 67 | void mpc_error(char * buf) { 68 | Serial.print("mpc command error:"); 69 | Serial.println(buf); 70 | while(1) {} 71 | } 72 | 73 | 74 | int getItem(String line, char * item, char * value, int len) { 75 | int pos1,pos2,pos3; 76 | Serial.println("item=[" + String(item) + "]"); 77 | pos1=line.indexOf(item); 78 | //Serial.println("pos1=" + String(pos1)); 79 | String line2; 80 | line2 = line.substring(pos1); 81 | pos2=line2.indexOf(":"); 82 | pos3=line2.indexOf(0x0a); 83 | //Serial.println("pos2=" + String(pos2)); 84 | //Serial.println("pos3=" + String(pos3)); 85 | String line3; 86 | line3 = line2.substring(pos2+1,pos3); 87 | //Serial.println("line3=[" + line3 + "]"); 88 | string2char(line3, value, len); 89 | Serial.println("value=[" + String(value) + "]"); 90 | return(strlen(value)); 91 | } 92 | 93 | void string2char(String line, char * cstr4, int len) { 94 | char cstr3[40]; 95 | line.toCharArray(cstr3, line.length()+1); 96 | //Serial.println("cstr3=[" + String(cstr3) + "]"); 97 | int pos4 = 0; 98 | for (int i=0;i 1000) { 174 | lastMillis = now; 175 | sprintf(smsg,"status\n"); 176 | client.print(smsg); 177 | Serial.println("status"); 178 | 179 | //read back one line from server 180 | client.setTimeout(1000); 181 | line = client.readStringUntil('\0'); 182 | //Serial.println("status=[" + line + "]"); 183 | Serial.println("state=" + String(getItem(line, "state:", citem, sizeof(citem))) ); 184 | 185 | if (strcmp(citem,"play") == 0) { 186 | sprintf(smsg,"currentsong\n"); 187 | client.print(smsg); 188 | //read back one line from server 189 | client.setTimeout(1000); 190 | line = client.readStringUntil('\0'); 191 | Serial.println("currentsong=[" + line + "]"); 192 | 193 | sprintf(smsg,"playlist\n"); 194 | client.print(smsg); 195 | //read back one line from server 196 | client.setTimeout(1000); 197 | line = client.readStringUntil('\0'); 198 | Serial.println("playlist=[" + line + "]"); 199 | 200 | #if 0 201 | //This command is supported since MPD Version 0.20 202 | sprintf(smsg,"duration\n"); 203 | client.print(smsg); 204 | //read back one line from server 205 | client.setTimeout(1000); 206 | line = client.readStringUntil('\0'); 207 | Serial.println("duration=[" + line + "]"); 208 | #endif 209 | } else { 210 | if (mpc_command("play") == 0) mpc_error("play"); 211 | } 212 | } 213 | } 214 | -------------------------------------------------------------------------------- /MPD_Client_Load_Playlist/MPD_Client_Load_Playlist.ino: -------------------------------------------------------------------------------- 1 | /* 2 | * MPD Client Example for ESP8266/ESP32 3 | * 4 | */ 5 | 6 | #if defined(ESP8266) 7 | #include 8 | #else 9 | #include 10 | #endif 11 | 12 | 13 | char* ssid = "Your AP's SSID"; 14 | char* password = "Your AP's PASSWORD"; 15 | uint16_t port = 6600; 16 | char * host = "192.168.10.45"; // ip or dns 17 | 18 | // Use WiFiClient class to create TCP connections 19 | WiFiClient client; 20 | 21 | long lastMillis = 0; 22 | 23 | // playlists 24 | #define MAX_PLAYLIST 10 25 | char playlist[MAX_PLAYLIST][128]; 26 | int nplaylist = 0; 27 | 28 | 29 | int mpc_connect(char * host, int port) { 30 | char smsg[40]; 31 | char rmsg[40]; 32 | 33 | if (!client.connect(host, port)) { 34 | Serial.println("connection failed"); 35 | return 0; 36 | } 37 | 38 | String line; 39 | client.setTimeout(1000); 40 | line = client.readStringUntil('\0'); 41 | //Serial.print("["); 42 | //Serial.print(line); 43 | //Serial.println("]"); 44 | //Serial.println("length()=" + String(line.length())); 45 | line.toCharArray(rmsg, line.length()+1); 46 | //Serial.println("strlen()=" + String(strlen(rmsg))); 47 | rmsg[line.length()-1] = 0; 48 | Serial.println("rmsg=[" + String(rmsg) + "]"); 49 | if (strncmp(rmsg,"OK",2) == 0) return 1; 50 | return 0; 51 | } 52 | 53 | int mpc_command(char * buf) { 54 | char smsg[40]; 55 | char rmsg[40]; 56 | sprintf(smsg,"%s\n",buf); 57 | client.print(smsg); 58 | Serial.println("smsg=[" + String(buf) + "]"); 59 | 60 | String line; 61 | client.setTimeout(1000); 62 | line = client.readStringUntil('\0'); 63 | //Serial.print("["); 64 | //Serial.print(line); 65 | //Serial.println("]"); 66 | //Serial.println("length()=" + String(line.length())); 67 | line.toCharArray(rmsg, line.length()+1); 68 | //Serial.println("strlen()=" + String(strlen(rmsg))); 69 | rmsg[line.length()-1] = 0; 70 | Serial.println("rmsg=[" + String(rmsg) + "]"); 71 | if (strcmp(rmsg,"OK") == 0) return 1; 72 | return 0; 73 | } 74 | 75 | void mpc_error(char * buf) { 76 | Serial.print("mpc command error:"); 77 | Serial.println(buf); 78 | while(1) {} 79 | } 80 | 81 | 82 | int getItem(String line, char * item, char * value, int len) { 83 | int pos1,pos2,pos3; 84 | Serial.println("item=[" + String(item) + "]"); 85 | pos1=line.indexOf(item); 86 | //Serial.println("pos1=" + String(pos1)); 87 | String line2; 88 | line2 = line.substring(pos1); 89 | pos2=line2.indexOf(":"); 90 | pos3=line2.indexOf(0x0a); 91 | //Serial.println("pos2=" + String(pos2)); 92 | //Serial.println("pos3=" + String(pos3)); 93 | String line3; 94 | line3 = line2.substring(pos2+1,pos3); 95 | //Serial.println("line3=[" + line3 + "]"); 96 | string2char(line3, value, len); 97 | Serial.println("value=[" + String(value) + "]"); 98 | return(strlen(value)); 99 | } 100 | 101 | int getItems(String line, int start, char * item, char * value, int len) { 102 | int pos1,pos2,pos3; 103 | Serial.println("item=[" + String(item) + "]"); 104 | pos1=line.indexOf(item, start); 105 | //Serial.println("pos1=" + String(pos1)); 106 | if (pos1 < 0) return(pos1); 107 | 108 | String line2; 109 | line2 = line.substring(pos1); 110 | pos2=line2.indexOf(":"); 111 | pos3=line2.indexOf(0x0a); 112 | //Serial.println("pos2=" + String(pos2)); 113 | //Serial.println("pos3=" + String(pos3)); 114 | String line3; 115 | line3 = line2.substring(pos2+1,pos3); 116 | //Serial.println("line3=[" + line3 + "]"); 117 | string2char(line3, value, len); 118 | Serial.println("value=[" + String(value) + "]"); 119 | //return(strlen(value)); 120 | return(pos1+pos3+1); 121 | } 122 | 123 | void string2char(String line, char * cstr4, int len) { 124 | char cstr3[256]; 125 | line.toCharArray(cstr3, line.length()+1); 126 | //Serial.println("cstr3=[" + String(cstr3) + "]"); 127 | int pos4 = 0; 128 | for (int i=0;i 1000) { 223 | lastMillis = now; 224 | counter++; 225 | if (counter > 10) { 226 | playlistIndex++; 227 | if (playlistIndex >= nplaylist) playlistIndex = 0; 228 | strcpy(smsg, "load \""); 229 | strcat(smsg, playlist[playlistIndex]); 230 | strcat(smsg, "\""); 231 | Serial.println(); 232 | Serial.print("smsg="); 233 | Serial.println(smsg); 234 | if (mpc_command("stop") == 0) mpc_error("stop"); 235 | if (mpc_command("clear") == 0) mpc_error("clear"); 236 | if (mpc_command(smsg) == 0) mpc_error("load"); 237 | if (mpc_command("play") == 0) mpc_error("play"); 238 | counter = 0; 239 | } else { 240 | Serial.print("."); 241 | } 242 | } 243 | } 244 | -------------------------------------------------------------------------------- /MPD_Client_DotMatrix/MPD_Client_DotMatrix.ino: -------------------------------------------------------------------------------- 1 | /* 2 | * MPD Client Example for ESP8266/ESP32 3 | * 4 | * LED Matrix Pin -> ESP8266 Pin 5 | * Vcc -> 3v 6 | * Gnd -> Gnd 7 | * DIN -> D7 8 | * CS -> D8 9 | * CLK -> D5 10 | */ 11 | #if defined(ESP8266) 12 | #include 13 | #else 14 | #include 15 | #endif 16 | 17 | #include 18 | #include // Install from Library Manager 19 | #include // https://github.com/markruys/arduino-Max72xxPanel 20 | 21 | int pinCS = 15; // Attach CS to this pin, DIN to MOSI and CLK to SCK (cf http://arduino.cc/en/Reference/SPI ) 22 | int numberOfHorizontalDisplays = 4; 23 | int numberOfVerticalDisplays = 1; 24 | 25 | Max72xxPanel matrix = Max72xxPanel(pinCS, numberOfHorizontalDisplays, numberOfVerticalDisplays); 26 | 27 | int wait = 100; // In milliseconds 28 | int spacer = 1; 29 | int width = 5 + spacer; // The font width is 5 pixels 30 | 31 | char* ssid = "Your AP's SSID"; 32 | char* password = "Your AP's PASSWORD"; 33 | uint16_t port = 6600; 34 | char * host = "192.168.10.12"; // ip or dns 35 | 36 | // Use WiFiClient class to create TCP connections 37 | WiFiClient client; 38 | 39 | long lastMillis = 0; 40 | int interval = 0; 41 | 42 | int mpc_connect(char * host, int port) { 43 | char smsg[40]; 44 | char rmsg[40]; 45 | 46 | if (!client.connect(host, port)) { 47 | Serial.println("connection failed"); 48 | return 0; 49 | } 50 | 51 | String line; 52 | client.setTimeout(1000); 53 | line = client.readStringUntil('\0'); 54 | //Serial.print("["); 55 | //Serial.print(line); 56 | //Serial.println("]"); 57 | //Serial.println("length()=" + String(line.length())); 58 | line.toCharArray(rmsg, line.length()+1); 59 | //Serial.println("strlen()=" + String(strlen(rmsg))); 60 | rmsg[line.length()-1] = 0; 61 | Serial.println("rmsg=[" + String(rmsg) + "]"); 62 | if (strncmp(rmsg,"OK",2) == 0) return 1; 63 | return 0; 64 | } 65 | 66 | int mpc_command(char * buf) { 67 | char smsg[40]; 68 | char rmsg[40]; 69 | sprintf(smsg,"%s\n",buf); 70 | client.print(smsg); 71 | Serial.println("smsg=[" + String(buf) + "]"); 72 | 73 | String line; 74 | client.setTimeout(1000); 75 | line = client.readStringUntil('\0'); 76 | //Serial.print("["); 77 | //Serial.print(line); 78 | //Serial.println("]"); 79 | //Serial.println("length()=" + String(line.length())); 80 | line.toCharArray(rmsg, line.length()+1); 81 | //Serial.println("strlen()=" + String(strlen(rmsg))); 82 | rmsg[line.length()-1] = 0; 83 | Serial.println("rmsg=[" + String(rmsg) + "]"); 84 | if (strcmp(rmsg,"OK") == 0) return 1; 85 | return 0; 86 | } 87 | 88 | void mpc_error(char * buf) { 89 | Serial.print("mpc command error:"); 90 | Serial.println(buf); 91 | while(1) {} 92 | } 93 | 94 | 95 | int getItem(String line, char * item, char * value, int len) { 96 | int pos1,pos2,pos3; 97 | Serial.println("item=[" + String(item) + "]"); 98 | pos1=line.indexOf(item); 99 | //Serial.println("pos1=" + String(pos1)); 100 | String line2; 101 | line2 = line.substring(pos1); 102 | pos2=line2.indexOf(":"); 103 | pos3=line2.indexOf(0x0a); 104 | //Serial.println("pos2=" + String(pos2)); 105 | //Serial.println("pos3=" + String(pos3)); 106 | String line3; 107 | line3 = line2.substring(pos2+1,pos3); 108 | //Serial.println("line3=[" + line3 + "]"); 109 | string2char(line3, value, len); 110 | Serial.println("value=[" + String(value) + "]"); 111 | return(strlen(value)); 112 | } 113 | 114 | void string2char(String line, char * cstr4, int len) { 115 | char cstr3[40]; 116 | line.toCharArray(cstr3, line.length()+1); 117 | //Serial.println("cstr3=[" + String(cstr3) + "]"); 118 | int pos4 = 0; 119 | for (int i=0;i 188 | matrix.setPosition( 1, 1, 0) ; // The second display is at <1, 0> 189 | matrix.setPosition( 2, 2, 0) ; // The third display is in <2, 0> 190 | matrix.setPosition( 3, 3, 0) ; // The fourth display is at <3, 0> 191 | 192 | matrix.setRotation(0, 1); // The first display is position upside down 193 | matrix.setRotation(1, 1); // The first display is position upside down 194 | matrix.setRotation(2, 1); // The first display is position upside down 195 | matrix.setRotation(3, 1); // The first display is position upside down 196 | 197 | matrix.fillScreen(LOW); 198 | } 199 | 200 | void loop() { 201 | static int counter = 0; 202 | String line; 203 | char state[40]; 204 | char smsg[40]; 205 | char lcdbuf[80] = {0}; 206 | static char oldbuf[80] = {0}; 207 | static int offset = 0; 208 | 209 | if (!client.connected()) { 210 | Serial.println("server disconencted"); 211 | delay(10*1000); 212 | ESP.restart(); 213 | } 214 | 215 | long now = millis(); 216 | if (now < lastMillis) lastMillis = now; // millis is overflow 217 | if (now - lastMillis > 1000) { 218 | lastMillis = now; 219 | counter++; 220 | if (counter > interval) { 221 | sprintf(smsg,"status\n"); 222 | client.print(smsg); 223 | Serial.println("status"); 224 | 225 | //read back one line from server 226 | client.setTimeout(1000); 227 | line = client.readStringUntil('\0'); 228 | //Serial.println("status=[" + line + "]"); 229 | Serial.println("state=" + String(getItem(line, "state:", state, sizeof(state))) ); 230 | 231 | if (strcmp(state,"play") == 0) { 232 | sprintf(smsg,"currentsong\n"); 233 | client.print(smsg); 234 | //read back one line from server 235 | client.setTimeout(1000); 236 | line = client.readStringUntil('\0'); 237 | Serial.println("currentsong=[" + line + "]"); 238 | 239 | char artist[40]; 240 | char title[40]; 241 | int artistLen; 242 | int titleLen; 243 | artistLen = getItem(line, "Artist:", artist, sizeof(artist)); 244 | Serial.println("Artist=" + String(artistLen)); 245 | titleLen = getItem(line, "Title:", title, sizeof(title)); 246 | Serial.println("Title=" + String(titleLen)); 247 | memset(lcdbuf, 0, sizeof(lcdbuf)); 248 | if (artistLen > 0 && titleLen > 0) { 249 | strcpy(lcdbuf, artist); 250 | strcat(lcdbuf, " - "); 251 | strcat(lcdbuf, title); 252 | } else if (artistLen > 0 && titleLen == 0) { 253 | strcpy(lcdbuf, artist); 254 | } else if (artistLen == 0 && titleLen > 0) { 255 | strcpy(lcdbuf, title); 256 | } 257 | 258 | Serial.println("lcdbuf=[" + String(lcdbuf) + "]"); 259 | if (strlen(lcdbuf) > 0) { 260 | matrixDisplay(lcdbuf, strlen(lcdbuf)); 261 | strcpy(oldbuf, lcdbuf); 262 | } 263 | interval = 0; 264 | 265 | } else { // state = stop 266 | matrix.fillScreen(LOW); 267 | memset(oldbuf, 0, sizeof(oldbuf)); 268 | interval = 1; 269 | } 270 | counter=0; 271 | } 272 | } 273 | } 274 | -------------------------------------------------------------------------------- /MPD_Client_1602LCD_Display/MPD_Client_1602LCD_Display.ino: -------------------------------------------------------------------------------- 1 | /* 2 | * MPD Client Example for ESP8266/ESP32 3 | * 4 | * 1602LCD ---- WeMos 5 | * GND ---- GND 6 | * VCC ---- 5V 7 | * VE ---- Potentiometer Resistor 8 | * RS ---- D1(GPIO5) 9 | * R/W ---- GND 10 | * Enable ---- D2(GPIO4) 11 | * DB0 ---- N.C 12 | * DB1 ---- N.C 13 | * DB2 ---- N.C 14 | * DB3 ---- N.C 15 | * DB4 ---- D0(GPIO16) 16 | * DB5 ---- D5(GPIO14) 17 | * DB6 ---- D6(GPIO12) 18 | * DB7 ---- D7(GPIO13) 19 | * LED(+) ---- 5V 20 | * ---- Transistor(2SC1815) 21 | * LED(-) ---- ---- Collector 22 | * GND ---- Emitter 23 | * D8(GPIO15) ---- Base 24 | * 25 | */ 26 | #if defined(ESP8266) 27 | #include 28 | #else 29 | #include 30 | #endif 31 | 32 | #include 33 | /* LiquidCrystal lcd(rs, enable, d4, d5, d6, d7) */ 34 | LiquidCrystal lcd(5, 4, 16, 14, 12, 13); 35 | /* the number of columns that the display has */ 36 | #define lcdCols 16 37 | /* the number of rows that the display has */ 38 | #define lcdRows 2 39 | //#define lcdRows 4 40 | /* LDC BackLight Control GPIO */ 41 | #define BackLight 15 42 | 43 | 44 | char* ssid = "Your AP's SSID"; 45 | char* password = "Your AP's PASSWORD"; 46 | uint16_t port = 6600; 47 | char * host = "192.168.10.109"; // ip or dns 48 | 49 | // Use WiFiClient class to create TCP connections 50 | WiFiClient client; 51 | 52 | long lastMillis = 0; 53 | int interval = 0; 54 | 55 | int mpc_connect(char * host, int port) { 56 | char smsg[40]; 57 | char rmsg[40]; 58 | 59 | if (!client.connect(host, port)) { 60 | Serial.println("connection failed"); 61 | return 0; 62 | } 63 | 64 | String line; 65 | client.setTimeout(1000); 66 | line = client.readStringUntil('\0'); 67 | //Serial.print("["); 68 | //Serial.print(line); 69 | //Serial.println("]"); 70 | //Serial.println("length()=" + String(line.length())); 71 | line.toCharArray(rmsg, line.length()+1); 72 | //Serial.println("strlen()=" + String(strlen(rmsg))); 73 | rmsg[line.length()-1] = 0; 74 | Serial.println("rmsg=[" + String(rmsg) + "]"); 75 | if (strncmp(rmsg,"OK",2) == 0) return 1; 76 | return 0; 77 | } 78 | 79 | int mpc_command(char * buf) { 80 | char smsg[40]; 81 | char rmsg[40]; 82 | sprintf(smsg,"%s\n",buf); 83 | client.print(smsg); 84 | Serial.println("smsg=[" + String(buf) + "]"); 85 | 86 | String line; 87 | client.setTimeout(1000); 88 | line = client.readStringUntil('\0'); 89 | //Serial.print("["); 90 | //Serial.print(line); 91 | //Serial.println("]"); 92 | //Serial.println("length()=" + String(line.length())); 93 | line.toCharArray(rmsg, line.length()+1); 94 | //Serial.println("strlen()=" + String(strlen(rmsg))); 95 | rmsg[line.length()-1] = 0; 96 | Serial.println("rmsg=[" + String(rmsg) + "]"); 97 | if (strcmp(rmsg,"OK") == 0) return 1; 98 | return 0; 99 | } 100 | 101 | void mpc_error(char * buf) { 102 | Serial.print("mpc command error:"); 103 | Serial.println(buf); 104 | while(1) {} 105 | } 106 | 107 | 108 | int getItem(String line, char * item, char * value, int len) { 109 | int pos1,pos2,pos3; 110 | Serial.println("item=[" + String(item) + "]"); 111 | pos1=line.indexOf(item); 112 | //Serial.println("pos1=" + String(pos1)); 113 | String line2; 114 | line2 = line.substring(pos1); 115 | pos2=line2.indexOf(":"); 116 | pos3=line2.indexOf(0x0a); 117 | //Serial.println("pos2=" + String(pos2)); 118 | //Serial.println("pos3=" + String(pos3)); 119 | String line3; 120 | line3 = line2.substring(pos2+1,pos3); 121 | //Serial.println("line3=[" + line3 + "]"); 122 | string2char(line3, value, len); 123 | Serial.println("value=[" + String(value) + "]"); 124 | return(strlen(value)); 125 | } 126 | 127 | void string2char(String line, char * cstr4, int len) { 128 | char cstr3[40]; 129 | line.toCharArray(cstr3, line.length()+1); 130 | //Serial.println("cstr3=[" + String(cstr3) + "]"); 131 | int pos4 = 0; 132 | for (int i=0;i 16) { 160 | strncpy(line, &lcdbuf[16], 16); 161 | } else { 162 | strcpy(line, " "); 163 | } 164 | fillBuffer(line, 16); 165 | Serial.println("line2=[" + String(line) + "]"); 166 | lcd.setCursor(0, 1); 167 | lcd.print(line); 168 | 169 | if (rows == 2) return; 170 | if (strlen(lcdbuf) > 32) { 171 | strncpy(line, &lcdbuf[32], 16); 172 | } else { 173 | strcpy(line, " "); 174 | } 175 | fillBuffer(line, 16); 176 | Serial.println("line3=[" + String(line) + "]"); 177 | lcd.setCursor(0, 2); 178 | lcd.print(line); 179 | 180 | if (strlen(lcdbuf) > 48) { 181 | strncpy(line, &lcdbuf[48], 16); 182 | } else { 183 | strcpy(line, " "); 184 | } 185 | fillBuffer(line, 16); 186 | Serial.println("line4=[" + String(line) + "]"); 187 | lcd.setCursor(0, 3); 188 | lcd.print(line); 189 | } 190 | 191 | 192 | void setup() { 193 | Serial.begin(115200); 194 | 195 | // We start by connecting to a WiFi network 196 | Serial.println(); 197 | Serial.println(); 198 | Serial.print("Wait for WiFi..."); 199 | WiFi.begin(ssid, password); 200 | 201 | int cnt = 0; 202 | while (WiFi.status() != WL_CONNECTED) { 203 | delay(500); 204 | Serial.print("."); 205 | cnt++; 206 | if ((cnt % 60) == 0) Serial.println(); 207 | } 208 | 209 | Serial.println(""); 210 | Serial.println("WiFi connected"); 211 | Serial.print("IP address: "); 212 | Serial.println(WiFi.localIP()); 213 | 214 | while(1) { 215 | Serial.print("connecting to "); 216 | Serial.println(host); 217 | //if (mpc_connect(host, port) == 0) mpc_error("connect"); 218 | if (mpc_connect(host, port) == 1) break; 219 | delay(10*1000); 220 | } 221 | 222 | // set up the LCD's number of columns and rows: 223 | lcd.begin(lcdCols, lcdRows); 224 | pinMode(BackLight, OUTPUT); 225 | } 226 | 227 | 228 | void loop() { 229 | static int counter = 0; 230 | String line; 231 | char state[40]; 232 | char smsg[40]; 233 | char lcdbuf[80] = {0}; 234 | static char oldbuf[80] = {0}; 235 | 236 | if (!client.connected()) { 237 | Serial.println("server disconencted"); 238 | lcd.clear(); 239 | digitalWrite(BackLight, LOW); 240 | delay(10*1000); 241 | ESP.restart(); 242 | } 243 | 244 | long now = millis(); 245 | if (now < lastMillis) lastMillis = now; // millis is overflow 246 | if (now - lastMillis > 1000) { 247 | lastMillis = now; 248 | counter++; 249 | if (counter > interval) { 250 | sprintf(smsg,"status\n"); 251 | client.print(smsg); 252 | Serial.println("status"); 253 | 254 | //read back one line from server 255 | client.setTimeout(1000); 256 | line = client.readStringUntil('\0'); 257 | //Serial.println("status=[" + line + "]"); 258 | Serial.println("state=" + String(getItem(line, "state:", state, sizeof(state))) ); 259 | 260 | if (strcmp(state,"play") == 0) { 261 | sprintf(smsg,"currentsong\n"); 262 | client.print(smsg); 263 | //read back one line from server 264 | client.setTimeout(1000); 265 | line = client.readStringUntil('\0'); 266 | Serial.println("currentsong=[" + line + "]"); 267 | 268 | char artist[40]; 269 | char title[40]; 270 | int artistLen; 271 | int titleLen; 272 | artistLen = getItem(line, "Artist:", artist, sizeof(artist)); 273 | Serial.println("Artist=" + String(artistLen)); 274 | titleLen = getItem(line, "Title:", title, sizeof(title)); 275 | Serial.println("Title=" + String(titleLen)); 276 | memset(lcdbuf, 0, sizeof(lcdbuf)); 277 | if (artistLen > 0 && titleLen > 0) { 278 | strcpy(lcdbuf, artist); 279 | strcat(lcdbuf, " - "); 280 | strcat(lcdbuf, title); 281 | } else if (artistLen > 0 && titleLen == 0) { 282 | strcpy(lcdbuf, artist); 283 | } else if (artistLen == 0 && titleLen > 0) { 284 | strcpy(lcdbuf, title); 285 | } 286 | 287 | Serial.println("lcdbuf=[" + String(lcdbuf) + "]"); 288 | if (strlen(lcdbuf) > 0) { 289 | if (strcmp(lcdbuf, oldbuf) != 0) { 290 | digitalWrite(BackLight, HIGH); 291 | lcdDisplay(lcdbuf, lcdRows); 292 | strcpy(oldbuf, lcdbuf); 293 | } 294 | } 295 | interval = 1; 296 | 297 | } else { // state = stop 298 | lcd.clear(); 299 | digitalWrite(BackLight, LOW); 300 | memset(oldbuf, 0, sizeof(oldbuf)); 301 | interval = 1; 302 | } 303 | counter=0; 304 | } 305 | } 306 | } 307 | -------------------------------------------------------------------------------- /MPD_Client_Remote_1602LCD_Display/MPD_Client_Remote_1602LCD_Display.ino: -------------------------------------------------------------------------------- 1 | /* 2 | * MPD Client Example for ESP8266/ESP32 3 | * 4 | * Required library 5 | * https://github.com/crankyoldgit/IRremoteESP8266 6 | * 7 | * 1602LCD ---- WeMos 8 | * GND ---- GND 9 | * VCC ---- 5V 10 | * VE ---- Potentiometer Resistor 11 | * RS ---- D1(GPIO5) 12 | * R/W ---- GND 13 | * Enable ---- D2(GPIO4) 14 | * DB0 ---- N.C 15 | * DB1 ---- N.C 16 | * DB2 ---- N.C 17 | * DB3 ---- N.C 18 | * DB4 ---- D0(GPIO16) 19 | * DB5 ---- D5(GPIO14) 20 | * DB6 ---- D6(GPIO12) 21 | * DB7 ---- D7(GPIO13) 22 | * LED(+) ---- 5V 23 | * 24 | * ---- Transistor(2SC1815) 25 | * LED(-) ---- ---- Collector 26 | * GND ---- Emitter 27 | * D8(GPIO15) ---- Base 28 | * 29 | */ 30 | #if defined(ESP8266) 31 | #include 32 | #else 33 | #include 34 | #endif 35 | 36 | #include 37 | 38 | #include 39 | #include 40 | #include 41 | 42 | /* LiquidCrystal lcd(rs, enable, d4, d5, d6, d7) */ 43 | LiquidCrystal lcd(5, 4, 16, 14, 12, 13); 44 | /* the number of columns that the display has */ 45 | #define lcdCols 16 46 | /* the number of rows that the display has */ 47 | #define lcdRows 2 48 | //#define lcdRows 4 49 | /* LDC BackLight Control */ 50 | #define BackLight 15 51 | 52 | // It needs to match your remote control. 53 | #define PLAY 0x00FF18E7 54 | #define STOP 0x00FF38C7 55 | #define PREV 0x00FF10EF 56 | #define NEXT 0x00FF5AA5 57 | #define VOLU 0x00FF52AD 58 | #define VOLD 0x00FF42BD 59 | 60 | // an IR detector/demodulator is connected to GPIO pin 2 61 | const uint16_t RECV_PIN = 2; 62 | 63 | IRrecv irrecv(RECV_PIN); 64 | 65 | decode_results results; 66 | 67 | char* ssid = "Your AP's SSID"; 68 | char* password = "Your AP's PASSWORD"; 69 | uint16_t port = 6600; 70 | char * host = "192.168.10.109"; // ip or dns 71 | 72 | // Use WiFiClient class to create TCP connections 73 | WiFiClient client; 74 | 75 | long lastMillis = 0; 76 | int interval = 0; 77 | 78 | int mpc_connect(char * host, int port) { 79 | char smsg[40]; 80 | char rmsg[40]; 81 | 82 | if (!client.connect(host, port)) { 83 | Serial.println("connection failed"); 84 | return 0; 85 | } 86 | 87 | String line; 88 | client.setTimeout(1000); 89 | line = client.readStringUntil('\0'); 90 | //Serial.print("["); 91 | //Serial.print(line); 92 | //Serial.println("]"); 93 | //Serial.println("length()=" + String(line.length())); 94 | line.toCharArray(rmsg, line.length()+1); 95 | //Serial.println("strlen()=" + String(strlen(rmsg))); 96 | rmsg[line.length()-1] = 0; 97 | Serial.println("rmsg=[" + String(rmsg) + "]"); 98 | if (strncmp(rmsg,"OK",2) == 0) return 1; 99 | return 0; 100 | } 101 | 102 | int mpc_command(char * buf) { 103 | char smsg[40]; 104 | char rmsg[40]; 105 | sprintf(smsg,"%s\n",buf); 106 | client.print(smsg); 107 | Serial.println("smsg=[" + String(buf) + "]"); 108 | 109 | String line; 110 | client.setTimeout(1000); 111 | line = client.readStringUntil('\0'); 112 | //Serial.print("["); 113 | //Serial.print(line); 114 | //Serial.println("]"); 115 | //Serial.println("length()=" + String(line.length())); 116 | line.toCharArray(rmsg, line.length()+1); 117 | //Serial.println("strlen()=" + String(strlen(rmsg))); 118 | rmsg[line.length()-1] = 0; 119 | Serial.println("rmsg=[" + String(rmsg) + "]"); 120 | if (strcmp(rmsg,"OK") == 0) return 1; 121 | return 0; 122 | } 123 | 124 | void mpc_error(char * buf) { 125 | Serial.print("mpc command error:"); 126 | Serial.println(buf); 127 | while(1) {} 128 | } 129 | 130 | 131 | int getItem(String line, char * item, char * value, int len) { 132 | int pos1,pos2,pos3; 133 | Serial.println("item=[" + String(item) + "]"); 134 | pos1=line.indexOf(item); 135 | //Serial.println("pos1=" + String(pos1)); 136 | String line2; 137 | line2 = line.substring(pos1); 138 | pos2=line2.indexOf(":"); 139 | pos3=line2.indexOf(0x0a); 140 | //Serial.println("pos2=" + String(pos2)); 141 | //Serial.println("pos3=" + String(pos3)); 142 | String line3; 143 | line3 = line2.substring(pos2+1,pos3); 144 | //Serial.println("line3=[" + line3 + "]"); 145 | string2char(line3, value, len); 146 | Serial.println("value=[" + String(value) + "]"); 147 | return(strlen(value)); 148 | } 149 | 150 | void string2char(String line, char * cstr4, int len) { 151 | char cstr3[40]; 152 | line.toCharArray(cstr3, line.length()+1); 153 | //Serial.println("cstr3=[" + String(cstr3) + "]"); 154 | int pos4 = 0; 155 | for (int i=0;i 16) { 183 | strncpy(line, &lcdbuf[16], 16); 184 | } else { 185 | strcpy(line, " "); 186 | } 187 | fillBuffer(line, 16); 188 | Serial.println("line2=[" + String(line) + "]"); 189 | lcd.setCursor(0, 1); 190 | lcd.print(line); 191 | 192 | if (rows == 2) return; 193 | if (strlen(lcdbuf) > 32) { 194 | strncpy(line, &lcdbuf[32], 16); 195 | } else { 196 | strcpy(line, " "); 197 | } 198 | fillBuffer(line, 16); 199 | Serial.println("line3=[" + String(line) + "]"); 200 | lcd.setCursor(0, 2); 201 | lcd.print(line); 202 | 203 | if (strlen(lcdbuf) > 48) { 204 | strncpy(line, &lcdbuf[48], 16); 205 | } else { 206 | strcpy(line, " "); 207 | } 208 | fillBuffer(line, 16); 209 | Serial.println("line4=[" + String(line) + "]"); 210 | lcd.setCursor(0, 3); 211 | lcd.print(line); 212 | } 213 | 214 | 215 | void setup() { 216 | Serial.begin(115200); 217 | 218 | // We start by connecting to a WiFi network 219 | Serial.println(); 220 | Serial.println(); 221 | Serial.print("Wait for WiFi..."); 222 | WiFi.begin(ssid, password); 223 | 224 | int cnt = 0; 225 | while (WiFi.status() != WL_CONNECTED) { 226 | delay(500); 227 | Serial.print("."); 228 | cnt++; 229 | if ((cnt % 60) == 0) Serial.println(); 230 | } 231 | 232 | Serial.println(""); 233 | Serial.println("WiFi connected"); 234 | Serial.print("IP address: "); 235 | Serial.println(WiFi.localIP()); 236 | 237 | while(1) { 238 | Serial.print("connecting to "); 239 | Serial.println(host); 240 | //if (mpc_connect(host, port) == 0) mpc_error("connect"); 241 | if (mpc_connect(host, port) == 1) break; 242 | delay(10*1000); 243 | } 244 | 245 | // set up the LCD's number of columns and rows: 246 | lcd.begin(lcdCols, lcdRows); 247 | pinMode(BackLight, OUTPUT); 248 | 249 | Serial.println(); 250 | irrecv.enableIRIn(); // Start the receiver 251 | Serial.print("IRrecvDemo is now running and waiting for IR message on Pin "); 252 | Serial.println(RECV_PIN); 253 | 254 | } 255 | 256 | 257 | void loop() { 258 | static int counter = 0; 259 | String line; 260 | char state[40]; 261 | char smsg[40]; 262 | char lcdbuf[80] = {0}; 263 | static char oldbuf[80] = {0}; 264 | 265 | if (!client.connected()) { 266 | Serial.println("server disconencted"); 267 | lcd.clear(); 268 | digitalWrite(BackLight, LOW); 269 | delay(10*1000); 270 | ESP.restart(); 271 | } 272 | 273 | if (irrecv.decode(&results)) { 274 | serialPrintUint64(results.value, HEX); 275 | Serial.println(""); 276 | if (results.value == PLAY) { 277 | if (mpc_command("play") == 0) mpc_error("play"); 278 | interval = lastMillis = 0; 279 | } else if (results.value == STOP) { 280 | if (mpc_command("stop") == 0) mpc_error("stop"); 281 | interval = lastMillis = 0; 282 | } else if (results.value == NEXT) { 283 | if (mpc_command("play") == 0) mpc_error("play"); // Start if stopped 284 | if (mpc_command("next") == 0) mpc_error("next"); 285 | if (mpc_command("play") == 0) mpc_error("play"); 286 | interval = lastMillis = 0; 287 | } else if (results.value == PREV) { 288 | if (mpc_command("play") == 0) mpc_error("play"); // Start if stopped 289 | if (mpc_command("previous") == 0) mpc_error("previous"); 290 | if (mpc_command("play") == 0) mpc_error("play"); 291 | interval = lastMillis = 0; 292 | } else if (results.value == VOLU) { 293 | if (mpc_command("volume +10") == 0) mpc_error("volume"); 294 | } else if (results.value == VOLD) { 295 | if (mpc_command("volume -10") == 0) mpc_error("volume"); 296 | } 297 | irrecv.resume(); // Receive the next value 298 | } 299 | 300 | 301 | 302 | 303 | 304 | long now = millis(); 305 | if (now < lastMillis) lastMillis = now; // millis is overflow 306 | if (now - lastMillis > 1000) { 307 | lastMillis = now; 308 | counter++; 309 | if (counter > interval) { 310 | sprintf(smsg,"status\n"); 311 | client.print(smsg); 312 | Serial.println("status"); 313 | 314 | //read back one line from server 315 | client.setTimeout(1000); 316 | line = client.readStringUntil('\0'); 317 | //Serial.println("status=[" + line + "]"); 318 | Serial.println("state=" + String(getItem(line, "state:", state, sizeof(state))) ); 319 | 320 | if (strcmp(state,"play") == 0) { 321 | sprintf(smsg,"currentsong\n"); 322 | client.print(smsg); 323 | //read back one line from server 324 | client.setTimeout(1000); 325 | line = client.readStringUntil('\0'); 326 | Serial.println("currentsong=[" + line + "]"); 327 | 328 | char artist[40]; 329 | char title[40]; 330 | int artistLen; 331 | int titleLen; 332 | artistLen = getItem(line, "Artist:", artist, sizeof(artist)); 333 | Serial.println("Artist=" + String(artistLen)); 334 | titleLen = getItem(line, "Title:", title, sizeof(title)); 335 | Serial.println("Title=" + String(titleLen)); 336 | memset(lcdbuf, 0, sizeof(lcdbuf)); 337 | if (artistLen > 0 && titleLen > 0) { 338 | strcpy(lcdbuf, artist); 339 | strcat(lcdbuf, " - "); 340 | strcat(lcdbuf, title); 341 | } else if (artistLen > 0 && titleLen == 0) { 342 | strcpy(lcdbuf, artist); 343 | } else if (artistLen == 0 && titleLen > 0) { 344 | strcpy(lcdbuf, title); 345 | } 346 | 347 | Serial.println("lcdbuf=[" + String(lcdbuf) + "]"); 348 | if (strlen(lcdbuf) > 0) { 349 | if (strcmp(lcdbuf, oldbuf) != 0) { 350 | digitalWrite(BackLight, HIGH); 351 | lcdDisplay(lcdbuf, lcdRows); 352 | strcpy(oldbuf, lcdbuf); 353 | } 354 | } 355 | interval = 1; 356 | 357 | } else { // state = stop 358 | lcd.clear(); 359 | digitalWrite(BackLight, LOW); 360 | memset(oldbuf, 0, sizeof(oldbuf)); 361 | interval = 10; 362 | } 363 | counter=0; 364 | } 365 | } 366 | } 367 | --------------------------------------------------------------------------------