From aa21417af363aedda7aa7b3fdf2f3083dfd218dd Mon Sep 17 00:00:00 2001 From: ladyada Date: Sat, 16 Mar 2019 03:37:38 -0400 Subject: [PATCH] this version requires a VS1053 shield or something - its a good wifi test! --- .../streaming_mp3_player/arduino_secrets.h | 2 + .../streaming_mp3_player.ino | 116 ++++++++++++++++++ 2 files changed, 118 insertions(+) create mode 100644 Adafruit_ESP32_Arduino_Demos/streaming_mp3_player/arduino_secrets.h create mode 100644 Adafruit_ESP32_Arduino_Demos/streaming_mp3_player/streaming_mp3_player.ino diff --git a/Adafruit_ESP32_Arduino_Demos/streaming_mp3_player/arduino_secrets.h b/Adafruit_ESP32_Arduino_Demos/streaming_mp3_player/arduino_secrets.h new file mode 100644 index 000000000..09cee67b3 --- /dev/null +++ b/Adafruit_ESP32_Arduino_Demos/streaming_mp3_player/arduino_secrets.h @@ -0,0 +1,2 @@ +#define SECRET_SSID "ssid" +#define SECRET_PASS "password" diff --git a/Adafruit_ESP32_Arduino_Demos/streaming_mp3_player/streaming_mp3_player.ino b/Adafruit_ESP32_Arduino_Demos/streaming_mp3_player/streaming_mp3_player.ino new file mode 100644 index 000000000..7408e5ba0 --- /dev/null +++ b/Adafruit_ESP32_Arduino_Demos/streaming_mp3_player/streaming_mp3_player.ino @@ -0,0 +1,116 @@ +// Stream MP3s over WiFi on Metro M4 Express and play via music maker shield + +#include +#include +#include +#include +#include "arduino_secrets.h" + +///////please enter your sensitive data in the Secret tab/arduino_secrets.h +char ssid[] = SECRET_SSID; // your network SSID (name) +char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP) + + +// http://ice1.somafm.com/u80s-128-mp3 +const char *host = "ice1.somafm.com"; +const char *path = "/u80s-128-mp3"; +//const char *path = "/doomed-128-mp3"; +int httpPort = 80; + +// These are the pins used +#define VS1053_RESET -1 // VS1053 reset pin (not used!) +#define VS1053_CS 7 // VS1053 chip select pin (output) +#define VS1053_DCS 6 // VS1053 Data/command select pin (output) +#define VS1053_DREQ 3 // VS1053 Data request, ideally an Interrupt pin + +Adafruit_VS1053 musicPlayer = Adafruit_VS1053(VS1053_RESET, VS1053_CS, VS1053_DCS, VS1053_DREQ); + +// Use WiFiClient class to create HTTP/TCP connection +WiFiClient client; + +int lastvol = 30; + +#define BUFFER_SIZE 1500 +CircularBuffer buffer; + +void setup() { + Serial.begin(115200); + while (!Serial); + delay(100); + + Serial.println("\n\nAdafruit VS1053 Feather WiFi Radio"); + + /************************* INITIALIZE MP3 Shield */ + if (! musicPlayer.begin()) { // initialise the music player + Serial.println(F("Couldn't find VS1053, do you have the right pins defined?")); + while (1) delay(10); + } + + Serial.println(F("VS1053 found")); + //musicPlayer.sineTest(0x44, 10); // Make a tone to indicate VS1053 is working + + // Set volume for left, right channels. lower numbers == louder volume! + musicPlayer.setVolume(lastvol, lastvol); + + // don't use an IRQ, we'll hand-feed + + /************************* INITIALIZE WIFI */ + Serial.print("Connecting to SSID "); Serial.println(ssid); + WiFi.begin(ssid, pass); + + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + Serial.println("WiFi connected"); + Serial.println("IP address: "); Serial.println(WiFi.localIP()); + + /************************* INITIALIZE STREAM */ + Serial.print("Connecting to "); Serial.println(host); + + if (!client.connect(host, httpPort)) { + Serial.println("Connection failed"); + while (1); + } + + // We now create a URI for the request + Serial.print("Requesting URL: "); Serial.println(path); + + // This will send the request to the server + client.print(String("GET ") + path + " HTTP/1.1\r\n" + + "Host: " + host + "\r\n" + + "Connection: close\r\n\r\n"); +} + + +void loop() { + Serial.print("Client Avail: "); Serial.print(client.available()); + Serial.print("\tBuffer Avail: "); Serial.println(buffer.available()); + + // Prioritize reading data from the ESP32 into the buffer (it sometimes stalls) + while (client.available() && buffer.available()) { + uint8_t minibuff[BUFFER_SIZE]; + + int bytesread = client.read(minibuff, buffer.available()); + Serial.print("Client read: "); Serial.println(bytesread); + + for (int i=0; i 0)) { + //wants more data! check we have something available from the stream + uint8_t mp3buff[32]; // vs1053 likes 32 bytes at a time + + int byteswrite = min(32, buffer.size()); + Serial.print("MP3 write: "); Serial.println(byteswrite); + + // push to mp3 + for (int i=0; i