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