forked from Edzelf/Esp-radio
-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathWebRadioDemo.ino
154 lines (123 loc) · 3.91 KB
/
WebRadioDemo.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/**
A simple stream handler to play web radio stations using ESP8266
Copyright (C) 2018 Vince Gellár (github.com/vincegellar)
Licensed under GNU GPL v3
Wiring:
--------------------------------
| VS1053 | ESP8266 | ESP32 |
--------------------------------
| SCK | D5 | IO18 |
| MISO | D6 | IO19 |
| MOSI | D7 | IO23 |
| XRST | RST | EN |
| CS | D1 | IO5 |
| DCS | D0 | IO16 |
| DREQ | D3 | IO4 |
| 5V | 5V | 5V |
| GND | GND | GND |
--------------------------------
Dependencies:
-VS1053 library by baldram (https://github.com/baldram/ESP_VS1053_Library)
-ESP8266Wifi/WiFi
To run this example define the platformio.ini as below.
[env:nodemcuv2]
platform = espressif8266
board = nodemcuv2
framework = arduino
build_flags = -D PIO_FRAMEWORK_ARDUINO_LWIP2_HIGHER_BANDWIDTH
lib_deps =
ESP_VS1053_Library
[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
lib_deps =
ESP_VS1053_Library
Instructions:
-Build the hardware
(please find an additional description and Fritzing's schematic here:
https://github.com/vincegellar/Simple-Radio-Node#wiring)
-Set the station in this file
-Upload the program
IDE Settings (Tools):
-IwIP Variant: v1.4 Higher Bandwidth
-CPU Frequency: 160Hz
*/
#include <VS1053.h>
#ifdef ARDUINO_ARCH_ESP8266
#include <ESP8266WiFi.h>
#define VS1053_CS D1
#define VS1053_DCS D0
#define VS1053_DREQ D3
#endif
#ifdef ARDUINO_ARCH_ESP32
#include <WiFi.h>
#define VS1053_CS 5
#define VS1053_DCS 16
#define VS1053_DREQ 4
#endif
// Default volume
#define VOLUME 80
VS1053 player(VS1053_CS, VS1053_DCS, VS1053_DREQ);
WiFiClient client;
// WiFi settings example, substitute your own
const char *ssid = "TP-Link";
const char *password = "xxxxxxxx";
// http://comet.shoutca.st:8563/1
const char *host = "comet.shoutca.st";
const char *path = "/1";
int httpPort = 8563;
// The buffer size 64 seems to be optimal. At 32 and 128 the sound might be brassy.
uint8_t mp3buff[64];
void setup() {
Serial.begin(115200);
// Wait for VS1053 and PAM8403 to power up
// otherwise the system might not start up correctly
delay(3000);
// This can be set in the IDE no need for ext library
// system_update_cpu_freq(160);
Serial.println("\n\nSimple Radio Node WiFi Radio");
SPI.begin();
player.begin();
if (player.getChipVersion() == 4) { // Only perform an update if we really are using a VS1053, not. eg. VS1003
player.loadDefaultVs1053Patches();
}
player.switchToMp3Mode();
player.setVolume(VOLUME);
Serial.print("Connecting to SSID ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Serial.print("connecting to ");
Serial.println(host);
if (!client.connect(host, httpPort)) {
Serial.println("Connection failed");
return;
}
Serial.print("Requesting stream: ");
Serial.println(path);
client.print(String("GET ") + path + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
}
void loop() {
if (!client.connected()) {
Serial.println("Reconnecting...");
if (client.connect(host, httpPort)) {
client.print(String("GET ") + path + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
}
}
if (client.available() > 0) {
// The buffer size 64 seems to be optimal. At 32 and 128 the sound might be brassy.
uint8_t bytesread = client.read(mp3buff, 64);
player.playChunk(mp3buff, bytesread);
}
}