@@ -18,13 +18,14 @@ Take care to install the master branch of the VS1053 library or at least a versi
18
18
19
19
Use the [ 2.0.17 Arduino ESP32 core version] ( https://github.com/espressif/arduino-esp32/releases/tag/2.0.17 ) .
20
20
21
- ## Example code
22
21
22
+
23
+ ## Example: play a stream
23
24
``` c++
24
25
#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
27
27
#include < ESP32_VS1053_Stream.h>
28
+ #include < WiFi.h>
28
29
29
30
#define SPI_CLK_PIN 18
30
31
#define SPI_MISO_PIN 19
@@ -34,106 +35,153 @@ Use the [2.0.17 Arduino ESP32 core version](https://github.com/espressif/arduino
34
35
#define VS1053_DCS 21
35
36
#define VS1053_DREQ 22
36
37
37
- #define SDREADER_CS 26
38
-
39
38
ESP32_VS1053_Stream stream;
40
39
41
40
const char * SSID = " xxx" ;
42
41
const char * PSK = " xxx" ;
43
42
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");
49
123
return false;
50
124
}
51
- uint8_t cardType = SD.cardType();
52
125
53
- if ( cardType == CARD_NONE)
54
- {
126
+ uint8_t cardType = SD.cardType();
127
+ if (cardType == CARD_NONE) {
55
128
Serial.println("No SD card attached");
56
129
return false;
57
130
}
58
131
59
132
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);
61
134
return true;
62
135
}
63
136
64
137
void setup() {
65
138
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");
77
140
141
+ // Start SPI bus
78
142
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);
80
144
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);
88
149
}
89
150
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()) {
94
153
Serial.println("Decoder not running - system halted");
95
- while (1)
96
- delay (100);
154
+ while (1) delay(100);
97
155
}
98
156
99
- Serial.println("vs1053 running - starting playback");
157
+ Serial.println("VS1053 running - starting SD playback");
100
158
101
- //stream.connecttohost("http://icecast.omroep.nl/radio6-bb-mp3");
159
+ // Start playback from an SD file
102
160
stream.connecttofile(SD, "/test.mp3");
103
161
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);
109
165
}
110
166
111
- Serial.print("codec : ");
167
+ Serial.print("Codec : ");
112
168
Serial.println(stream.currentCodec());
113
169
114
- Serial.print("bitrate : ");
170
+ Serial.print("Bitrate : ");
115
171
Serial.print(stream.bitrate());
116
- Serial.println("kbps");
172
+ Serial.println(" kbps");
117
173
}
118
174
119
175
void loop() {
120
176
stream.loop();
121
- //Serial.printf("Buffer status: %s\n", stream.bufferStatus());
122
177
delay(5);
123
178
}
124
179
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
-
133
180
void audio_eof_stream(const char* info) {
134
- Serial.printf("eof : %s\n", info);
181
+ Serial.printf("End of file : %s\n", info);
135
182
}
136
183
```
184
+
137
185
## Known issues
138
186
Ogg files can not be started with an offset without first playing a couple of seconds from the start of the file.
139
187
0 commit comments