Skip to content

Commit 3f940e3

Browse files
Update README.md
Split up the example code
1 parent 9629085 commit 3f940e3

File tree

1 file changed

+107
-59
lines changed

1 file changed

+107
-59
lines changed

README.md

Lines changed: 107 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@ Take care to install the master branch of the VS1053 library or at least a versi
1818

1919
Use the [2.0.17 Arduino ESP32 core version](https://github.com/espressif/arduino-esp32/releases/tag/2.0.17).
2020

21-
## Example code
2221

22+
23+
## Example: play a stream
2324
```c++
2425
#include <Arduino.h>
25-
#include <SD.h>
26-
#include <VS1053.h> /* https://github.com/baldram/ESP_VS1053_Library */
26+
#include <VS1053.h> // https://github.com/baldram/ESP_VS1053_Library
2727
#include <ESP32_VS1053_Stream.h>
28+
#include <WiFi.h>
2829

2930
#define SPI_CLK_PIN 18
3031
#define SPI_MISO_PIN 19
@@ -34,106 +35,153 @@ Use the [2.0.17 Arduino ESP32 core version](https://github.com/espressif/arduino
3435
#define VS1053_DCS 21
3536
#define VS1053_DREQ 22
3637

37-
#define SDREADER_CS 26
38-
3938
ESP32_VS1053_Stream stream;
4039

4140
const char* SSID = "xxx";
4241
const char* PSK = "xxx";
4342

44-
bool mountSDcard()
45-
{
46-
if (!SD.begin(SDREADER_CS))
47-
{
48-
Serial.println("Card Mount Failed");
43+
void setup() {
44+
Serial.begin(115200);
45+
Serial.println("\n\nVS1053 Radio Streaming Example\n");
46+
47+
// Connect to Wi-Fi
48+
Serial.printf("Connecting to WiFi network: %s\n", SSID);
49+
WiFi.begin(SSID, PSK);
50+
WiFi.setSleep(false); // Important to disable sleep to ensure stable connection
51+
52+
while (!WiFi.isConnected()) {
53+
delay(10);
54+
}
55+
Serial.println("WiFi connected");
56+
57+
// Start SPI bus
58+
SPI.setHwCs(true);
59+
SPI.begin(SPI_CLK_PIN, SPI_MISO_PIN, SPI_MOSI_PIN);
60+
61+
// Initialize the VS1053 decoder
62+
if (!stream.startDecoder(VS1053_CS, VS1053_DCS, VS1053_DREQ) || !stream.isChipConnected()) {
63+
Serial.println("Decoder not running - system halted");
64+
while (1) delay(100);
65+
}
66+
Serial.println("VS1053 running - starting radio stream");
67+
68+
// Connect to the radio stream
69+
stream.connecttohost("http://icecast.omroep.nl/radio6-bb-mp3");
70+
71+
if (!stream.isRunning()) {
72+
Serial.println("Stream not running - system halted");
73+
while (1) delay(100);
74+
}
75+
76+
Serial.print("Codec: ");
77+
Serial.println(stream.currentCodec());
78+
79+
Serial.print("Bitrate: ");
80+
Serial.print(stream.bitrate());
81+
Serial.println(" kbps");
82+
}
83+
84+
void loop() {
85+
stream.loop();
86+
delay(5);
87+
}
88+
89+
void audio_showstation(const char* info) {
90+
Serial.printf("Station: %s\n", info);
91+
}
92+
93+
void audio_showstreamtitle(const char* info) {
94+
Serial.printf("Stream title: %s\n", info);
95+
}
96+
97+
void audio_eof_stream(const char* info) {
98+
Serial.printf("End of stream: %s\n", info);
99+
}
100+
```
101+
102+
## Example: play from SD card
103+
```c++
104+
#include <Arduino.h>
105+
#include <SD.h>
106+
#include <VS1053.h> // https://github.com/baldram/ESP_VS1053_Library
107+
#include <ESP32_VS1053_Stream.h>
108+
109+
#define SPI_CLK_PIN 18
110+
#define SPI_MISO_PIN 19
111+
#define SPI_MOSI_PIN 23
112+
113+
#define VS1053_CS 5
114+
#define VS1053_DCS 21
115+
#define VS1053_DREQ 22
116+
#define SDREADER_CS 26
117+
118+
ESP32_VS1053_Stream stream;
119+
120+
bool mountSDcard() {
121+
if (!SD.begin(SDREADER_CS)) {
122+
Serial.println("Card mount failed");
49123
return false;
50124
}
51-
uint8_t cardType = SD.cardType();
52125
53-
if (cardType == CARD_NONE)
54-
{
126+
uint8_t cardType = SD.cardType();
127+
if (cardType == CARD_NONE) {
55128
Serial.println("No SD card attached");
56129
return false;
57130
}
58131
59132
uint64_t cardSize = SD.cardSize() / (1024 * 1024);
60-
Serial.println("SD Card Size: %lluMB\n", cardSize);
133+
Serial.printf("SD Card Size: %lluMB\n", cardSize);
61134
return true;
62135
}
63136
64137
void setup() {
65138
Serial.begin(115200);
66-
Serial.println("\n\nsimple vs1053 streaming example.\n");
67-
68-
Serial.printf("connecting to wifi network %s\n", SSID);
69-
70-
WiFi.begin(SSID, PSK);
71-
WiFi.setSleep(false); /* important to set this right! See issue #15 */
72-
73-
while (!WiFi.isConnected())
74-
delay(10);
75-
76-
Serial.println("wifi connected - starting spi bus");
139+
Serial.println("\n\nVS1053 SD Card Playback Example\n");
77140
141+
// Start SPI bus
78142
SPI.setHwCs(true);
79-
SPI.begin(SPI_CLK_PIN, SPI_MISO_PIN, SPI_MOSI_PIN); /* start SPI before starting decoder or sdcard*/
143+
SPI.begin(SPI_CLK_PIN, SPI_MISO_PIN, SPI_MOSI_PIN);
80144
81-
Serial.println("spi running - mounting sd card");
82-
83-
if (!mountSDcard())
84-
{
85-
Serial.println("sdcard not mounted - system halted");
86-
while (1)
87-
delay(100);
145+
// Mount SD card
146+
if (!mountSDcard()) {
147+
Serial.println("SD card not mounted - system halted");
148+
while (1) delay(100);
88149
}
89150
90-
Serial.println("card mounted - starting vs1053");
91-
92-
if (!stream.startDecoder(VS1053_CS, VS1053_DCS, VS1053_DREQ) || !stream.isChipConnected())
93-
{
151+
// Initialize the VS1053 decoder
152+
if (!stream.startDecoder(VS1053_CS, VS1053_DCS, VS1053_DREQ) || !stream.isChipConnected()) {
94153
Serial.println("Decoder not running - system halted");
95-
while (1)
96-
delay(100);
154+
while (1) delay(100);
97155
}
98156
99-
Serial.println("vs1053 running - starting playback");
157+
Serial.println("VS1053 running - starting SD playback");
100158
101-
//stream.connecttohost("http://icecast.omroep.nl/radio6-bb-mp3");
159+
// Start playback from an SD file
102160
stream.connecttofile(SD, "/test.mp3");
103161
104-
if (!stream.isRunning())
105-
{
106-
Serial.println("no stream running - system halted");
107-
while (1)
108-
delay(100);
162+
if (!stream.isRunning()) {
163+
Serial.println("No file running - system halted");
164+
while (1) delay(100);
109165
}
110166
111-
Serial.print("codec: ");
167+
Serial.print("Codec: ");
112168
Serial.println(stream.currentCodec());
113169
114-
Serial.print("bitrate: ");
170+
Serial.print("Bitrate: ");
115171
Serial.print(stream.bitrate());
116-
Serial.println("kbps");
172+
Serial.println(" kbps");
117173
}
118174
119175
void loop() {
120176
stream.loop();
121-
//Serial.printf("Buffer status: %s\n", stream.bufferStatus());
122177
delay(5);
123178
}
124179
125-
void audio_showstation(const char* info) {
126-
Serial.printf("showstation: %s\n", info);
127-
}
128-
129-
void audio_showstreamtitle(const char* info) {
130-
Serial.printf("streamtitle: %s\n", info);
131-
}
132-
133180
void audio_eof_stream(const char* info) {
134-
Serial.printf("eof: %s\n", info);
181+
Serial.printf("End of file: %s\n", info);
135182
}
136183
```
184+
137185
## Known issues
138186
Ogg files can not be started with an offset without first playing a couple of seconds from the start of the file.
139187

0 commit comments

Comments
 (0)