Skip to content

Instantly share code, notes, and snippets.

@maxpromer
Last active October 26, 2023 23:02
Show Gist options
  • Save maxpromer/c65b82392c617e24c8839fb3c4ae13ab to your computer and use it in GitHub Desktop.
Save maxpromer/c65b82392c617e24c8839fb3c4ae13ab to your computer and use it in GitHub Desktop.
/* Create by IOXhop : www.ioxhop.com */
#include <ESPVS1003.h>
#include <SPI.h>
#include <WiFi.h>
#include <HTTPClient.h>
/* * Wiring map * * *
VS1003 <-> ESP32
XDCS - 23
DREQ - 25
XRES - 26
XCS - 27
SCLK - 14
MOSI - 13
MISO - 12
*/
// WiFi Config
#define WIFI_SSID "YOUR WIFI NAME"
#define WIFI_PASSWORD "YOU WIFI PASSWORD"
// Radio station list
int stationLen = 15;
String stationName[] = {
"www.fungfungfung.com",
"AM 1143 MHz",
"AM 1494 MHz",
"FM 100.5 MHz",
"FM 107 MHz",
"FM 107 MHz",
"FM 95 MHz",
"FM 96.5 MHz",
"FM 97.5 MHz",
"FM 99MHz",
"FM 102 MHz",
"GET FM 102.5",
"103 Like FM",
"103.5 FM One",
"FM 104.5 MHz",
};
String stationUrl[] = {
"http://onair2.onair.network:8064/;", // www.fungfungfung.com
"http://rstream.mcot.net:8000/am1143", // AM 1143 MHz
"http://rstream.mcot.net:8000/am1494", // AM 1494 MHz
"http://rstream.mcot.net:8000/fm1005", // FM 100.5 MHz
"http://rstream.mcot.net:8000/fm107", // FM 107 MHz
"http://rstream.mcot.net:8000/fm95", // FM 95 MHz
"http://rstream.mcot.net:8000/fm965", // FM 96.5 MHz
"http://rstream.mcot.net:8000/fm975", // FM 97.5 MHz
"http://rstream.mcot.net:8000/fm99", // FM 99MHz
"http://media.login.in.th:1100/;", // FM 102 MHz
"http://get1025.plathong.net:7004/;", // GET FM 102.5
"http://radio.livecache.org/radio/likefm/icecast.audio", // 103 Like FM
"http://fmone.plathong.net:7010/;", // 103.5 FM One
"http://radio12.plathong.net:8648/;", // FM 106 MHz
"http://radio10.plathong.net:7110/;", // FM 104.5 MHz
};
// VS1003 config
#define BUFFER_SIZE 128
uint8_t buff[BUFFER_SIZE] = { 0 };
VS1003 player(27, 23, 25, 26); // cs_pin, dcs_pin, dreq_pin, reset_pin
int playStation = -1, nowPlayStation = -1;
void playerTask(void *p) {
while (1) {
if (playStation >= 0) {
HTTPClient http;
http.begin(stationUrl[playStation]); // Connect to server
Serial.println("Connect to server...");
int httpCode = http.GET(); // Get sound from server
if (httpCode > 0) {
if (httpCode == HTTP_CODE_OK) {
nowPlayStation = playStation;
// Setup play
player.startSong();
Serial.print("Playing: ");
Serial.println(stationName[nowPlayStation]);
WiFiClient * stream = http.getStreamPtr();
while (http.connected() && nowPlayStation == playStation) {
int size = stream->available();
if (size > 0) {
int c = stream->readBytes(buff, size > sizeof(buff) ? sizeof(buff) : size);
player.playChunk(buff, c);
}
delay(0);
}
Serial.println("Stop !");
} else {
Serial.printf("Server failed, http code: %d\n", httpCode);
}
} else {
Serial.printf("Connect failed, error: %s\n", http.errorToString(httpCode).c_str());
}
// Serial.println("Wait to disconnect");
http.end();
// Serial.println("Disconnect");
}
delay(100);
}
}
void setup() {
SPI.begin(14, 12, 13); //SCK, MISO, MOSI
Serial.begin(115200);
// Setup VS1003
player.begin();
player.setVolume(0x0); // Set volume to max
// Connect to WiFi
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print('.');
}
Serial.println(" Connected.");
// Create new task
xTaskCreate(&playerTask, "player", 2048, NULL, 10, NULL);
Serial.println();
Serial.println("Select station (enter -1 to stop)");
Serial.println("***********************");
for (int i=0;i<stationLen;i++) {
Serial.print(" ");
Serial.print(i);
Serial.print(": ");
Serial.print(stationName[i]);
Serial.println();
}
Serial.println("***********************");
}
void loop() {
if (Serial.available()) {
playStation = Serial.parseInt();
Serial.flush(); // Clears the buffer
}
delay(10);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment