Guest User

Untitled

a guest
Nov 12th, 2023
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 4.15 KB | Source Code | 0 0
  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 |  ESP32   |
  10.   --------------------------------
  11.   |   SCK   |   D5    |   IO18   |
  12.   |   MISO  |   D6    |   IO19   |
  13.   |   MOSI  |   D7    |   IO23   |
  14.   |   XRST  |   RST   |   EN     |
  15.   |   CS    |   D1    |   IO5    |
  16.   |   DCS   |   D0    |   IO16   |
  17.   |   DREQ  |   D3    |   IO4    |
  18.   |   5V    |   5V    |   5V     |
  19.   |   GND   |   GND   |   GND    |
  20.   --------------------------------
  21.  
  22.   Dependencies:
  23.   -VS1053 library by baldram (https://github.com/baldram/ESP_VS1053_Library)
  24.   -ESP8266Wifi/WiFi
  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.   lib_deps =
  34.     ESP_VS1053_Library
  35.  
  36.   [env:esp32dev]
  37.   platform = espressif32
  38.   board = esp32dev
  39.   framework = arduino
  40.   lib_deps =
  41.     ESP_VS1053_Library
  42.  
  43.   Instructions:
  44.   -Build the hardware
  45.     (please find an additional description and Fritzing's schematic here:
  46.      https://github.com/vincegellar/Simple-Radio-Node#wiring)
  47.   -Set the station in this file
  48.   -Upload the program
  49.  
  50.   IDE Settings (Tools):
  51.   -IwIP Variant: v1.4 Higher Bandwidth
  52.   -CPU Frequency: 160Hz
  53. */
  54.  
  55. #include <VS1053.h>
  56.  
  57. #ifdef ARDUINO_ARCH_ESP8266
  58. #include <ESP8266WiFi.h>
  59. #define VS1053_CS     5//D1
  60. #define VS1053_DCS    0//D0
  61. #define VS1053_DREQ   3//D3
  62. #endif
  63.  
  64. #ifdef ARDUINO_ARCH_ESP32
  65. #include <WiFi.h>
  66. #define VS1053_CS     5
  67. #define VS1053_DCS    16
  68. #define VS1053_DREQ   4
  69. #endif
  70.  
  71. // Default volume
  72. #define VOLUME  90
  73.  
  74. VS1053 player(VS1053_CS, VS1053_DCS, VS1053_DREQ);
  75. WiFiClient client;
  76.  
  77. // WiFi settings example, substitute your own
  78. const char *ssid = "Akacfa32";
  79. const char *password = "xxxxxx";
  80.  
  81. //  http://comet.shoutca.st:8563/1
  82. const char *host = "icast.connectmedia.hu"; //edge-bauerall-01-gos2.sharp-stream.com/absolute10s.mp3
  83. const char *path = "/5001/live.mp3"; //     /stream
  84. int httpPort = 80;
  85.  
  86. // The buffer size 64 seems to be optimal. At 32 and 128 the sound might be brassy.
  87. uint8_t mp3buff[64];
  88.  
  89. void setup() {
  90.     Serial.begin(115200);
  91.  
  92.     // Wait for VS1053 and PAM8403 to power up
  93.     // otherwise the system might not start up correctly
  94.     delay(3000);
  95.  
  96.     // This can be set in the IDE no need for ext library
  97.     // system_update_cpu_freq(160);
  98.  
  99.     Serial.println("\n\nSimple Radio Node WiFi Radio");
  100.  
  101.     SPI.begin();
  102.  
  103.     player.begin();
  104.     if (player.getChipVersion() == 4) { // Only perform an update if we really are using a VS1053, not. eg. VS1003
  105.         player.loadDefaultVs1053Patches();
  106.     }
  107.     player.switchToMp3Mode();
  108.     player.setVolume(VOLUME);
  109.  
  110.     Serial.print("Connecting to SSID ");
  111.     Serial.println(ssid);
  112.     WiFi.begin(ssid, password);
  113.  
  114.     while (WiFi.status() != WL_CONNECTED) {
  115.         delay(500);
  116.         Serial.print(".");
  117.     }
  118.  
  119.     Serial.println("WiFi connected");
  120.     Serial.println("IP address: ");
  121.     Serial.println(WiFi.localIP());
  122.  
  123.     Serial.print("connecting to ");
  124.     Serial.println(host);
  125.  
  126.     if (!client.connect(host, httpPort)) {
  127.         Serial.println("Connection failed");
  128.         return;
  129.     }
  130.  
  131.     Serial.print("Requesting stream: ");
  132.     Serial.println(path);
  133.  
  134.     client.print(String("GET ") + path + " HTTP/1.1\r\n" +
  135.                  "Host: " + host + "\r\n" +
  136.                  "Connection: close\r\n\r\n");
  137. }
  138.  
  139. void loop() {
  140.     if (!client.connected()) {
  141.         Serial.println("Reconnecting...");
  142.         if (client.connect(host, httpPort)) {
  143.             client.print(String("GET ") + path + " HTTP/1.1\r\n" +
  144.                          "Host: " + host + "\r\n" +
  145.                          "Connection: close\r\n\r\n");
  146.         }
  147.     }
  148.  
  149.     if (client.available() > 0) {
  150.         // The buffer size 64 seems to be optimal. At 32 and 128 the sound might be brassy.
  151.         uint8_t bytesread = client.read(mp3buff, 64);
  152.         player.playChunk(mp3buff, bytesread);
  153.     }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment