/**
Build by CJP 2021-03-02
from example in VS1053 library https://github.com/baldram/ESP_VS1053_Library
A simple stream handler to play web radio stations using ESP32 dev board
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 | D32 XCS
| DCS | D0 | D33 XDCS
| DREQ | D3 | D35 |
| 5V | 5V | 5V |
| GND | GND | GND |
--------------------------------
|----vs1053 board--------------per above ------
| --------------
| |5v | 5v | VIN VIN
| |dgnd | miso | GND D19
| |mosi | sck | == D23 D18
| |dreq | xrst | D35 EN
| |xcs | xdcs | D32 D33
| --------------
| Jack
| X
| X
|----------------------X------------
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 https://github.com/baldram/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
IDE Settings (Tools):
-IwIP Variant: v1.4 Higher Bandwidth
-CPU Frequency: 160Hz
*/
#include "VS1053.h" //from https://github.com/baldram/ESP_VS1053_Library
// uncomment static_asserts to see what board is used - throws compile time error - so recomment after test
#ifdef ARDUINO_ARCH_ESP8266
//static_assert(-5 / 2 != -2, "ARDUINO_ARCH_ESP8266");
#include <ESP8266WiFi.h>
#define VS1053_CS D1
#define VS1053_DCS D0
#define VS1053_DREQ D3
#endif
#ifdef ARDUINO_ARCH_ESP32
//static_assert(-5 / 2 != -2, "ARDUINO_ARCH_ESP32");
#include <WiFi.h>
#define VS1053_CS 5
#define VS1053_DCS 16
#define VS1053_DREQ 4
#endif
//CJP I have a different ESP32 pinout - overwrite above
#include <WiFi.h>
#define VS1053_CS 32 //XCS label on VS1053
#define VS1053_DCS 33 //XDCS label on VS1053
#define VS1053_DREQ 35
// Default volume
#define VOLUME 99
VS1053 player(VS1053_CS, VS1053_DCS, VS1053_DREQ);
WiFiClient client;
// WiFi settings example, substitute your own
const char *ssid = "EVANOFF24G";
const char *password = "wpawpa987321";
struct station
{
char *stationName;
char *host;
char *path;
int httpPort;
};
// Few Radio Stations
struct station stations[]={
"BBC 6 Music", "bbcmedia.ic.llnwd.net", "/stream/bbcmedia_6music_mf_p", 80, //link works in browser-not here
"Cornucopia Broadcasting", "8.38.78.173", "/stream", 8264, //works
"Country KX94.7", "player.listenlive.co/34661", "", 80, //need to push play at player.listenlive.co/34661:80/
"Country KX94.7", "192.173.28.140", "/34661", 80,
"BBC Radio 1", "bbcmedia.ic.llnwd.net", "/stream/bbcmedia_radio1_mf_p", 80,
"Comet radio", "http://comet.shoutca.st", "/1", 8563, //broken - needs login
"BBC Radio 2", "bbcmedia.ic.llnwd.net", "/stream/bbcmedia_radio2_mf_p", 80, //this stops cornucopia playing
"I love Rio Radio", "eu10.fastcast4u.com", "/iloverio", 80, //nice
"BBC Radio 4", "bbcmedia.ic.llnwd.net", "/stream/bbcmedia_radio4fm_mf_p", 80,
"BBC World Service", "bbcwssc.ic.llnwd.net", "/stream/bbcwssc_mp1_ws-eieuk", 80,
"Classic FM", "media-ice.musicradio.com", "/ClassicFMMP3", 80 //working (resets if array size wrong)
};
int numberOfStations = sizeof(stations)/sizeof(stations[0]);
//CJP suggestions
//https://www.cbc.ca/listen/live-radio?radio_one=toronto&cbc_music=toronto
//for scanning
unsigned long startMillis;
unsigned long currentMillis;
const unsigned long period = 60000; //1,000= sec 60,000=min
// The buffer size 64 seems to be optimal. At 32 and 128 the sound might be brassy.
uint8_t mp3buff[64];
int stationNumber =-1; //first step is increment to 0
void setup() {
Serial.begin(115200);
// Wait for VS1053 and PAM8403 to power up
// otherwise the system might not start up correctly
delay(5000);
// 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");
//startup VS1053
SPI.begin();
player.begin();
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.print("IP address: ");
Serial.println(WiFi.localIP());
//let loop() connect to wifi - no duplicate code sections
Serial.print("Number of stations: ");
Serial.println(numberOfStations);
startMillis = millis(); //initial start time
}
void loop() {
char str3[111]="";
int rtn=0;
//if no connection try next station OR for testing if 1 minute is up -note connected still plays
currentMillis = millis();
if (!client.connected() || currentMillis - startMillis >= period)
{
//if no connection scroll to next one
stationNumber++;
if(stationNumber >= numberOfStations) stationNumber=0;
Serial.println();
Serial.print("Requesting stream: ");
Serial.println(stations[stationNumber].stationName);
//connect to station else close
rtn=client.connect(stations[stationNumber].host, stations[stationNumber].httpPort);
if(rtn==1)
{
Serial.println("SUCCESS");
//send path to website - finish up selection
client.print(String("GET ") + stations[stationNumber].path + " \r\n\r\n");
}
else if(rtn==-1)Serial.println("TIMED_OUT");
else if(rtn==-2)Serial.println("INVALID_SERVER");
else if(rtn==-3)Serial.println("TRUNCATED");
else if(rtn==-4)Serial.println("INVALID_RESPONSE");
else
{
Serial.print("UNKNOWN - Error code: ");
Serial.println(rtn);
}
//scroll through all stations for testing
if (currentMillis - startMillis >= period)
{
rtn=-99; //new station
Serial.println("1 minute");
startMillis = millis(); //re-initial start time
}
//handle connection errors - close
if(!rtn)
{
sprintf (str3, "GET %s HTTP/1.1\r\nHost:%s \r\nConnection: close\r\n\r\n",
stations[stationNumber].stationName,
stations[stationNumber].host
);
client.print(str3);
Serial.print(str3);
}
//this is what is sent by connection
strcpy(str3,stations[stationNumber].host);
strcat(str3,":");
char* ptr = str3 + strlen(str3);
ultoa(stations[stationNumber].httpPort, ptr, 10);
strcat(str3,stations[stationNumber].path);
Serial.print("Trying: ");
Serial.println(str3);
}
//if connected then play
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);
}
} //end loop()