Advertisement
Cleox76

WebRadioCleo

May 23rd, 2021
468
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. #include <Arduino.h>
  2.  
  3. #ifdef ESP32
  4. #include <WiFi.h>
  5. #else
  6. #include <ESP8266WiFi.h>
  7. #endif
  8. #include "AudioFileSourceICYStream.h"
  9. #include "AudioFileSourceBuffer.h"
  10. #include "AudioGeneratorMP3.h"
  11. #include "AudioOutputI2SNoDAC.h"
  12.  
  13. // To run, set your ESP8266 build to 160MHz, update the SSID info, and upload.
  14.  
  15. // Enter your WiFi setup here:
  16. const char *SSID = "jdfkajekjwak";
  17. const char *PASSWORD = "fewuiawfuiau";
  18.  
  19. const char *URL="http://nr5.newradio.it:8728/stream";// Funziona...
  20. //const char *URL="http://radiom2o-lh.akamaihd.net/i/RadioM2o_Live_1@42518/master.m3u8";// M2O non funziona
  21.  
  22. AudioGeneratorMP3 *mp3;
  23. AudioFileSourceICYStream *file;
  24. AudioFileSourceBuffer *buff;
  25. AudioOutputI2SNoDAC *out;
  26.  
  27. // Called when a metadata event occurs (i.e. an ID3 tag, an ICY block, etc.
  28. void MDCallback(void *cbData, const char *type, bool isUnicode, const char *string)
  29. {
  30. const char *ptr = reinterpret_cast<const char *>(cbData);
  31. (void) isUnicode; // Punt this ball for now
  32. // Note that the type and string may be in PROGMEM, so copy them to RAM for printf
  33. char s1[32], s2[64];
  34. strncpy_P(s1, type, sizeof(s1));
  35. s1[sizeof(s1)-1]=0;
  36. strncpy_P(s2, string, sizeof(s2));
  37. s2[sizeof(s2)-1]=0;
  38. Serial.printf("METADATA(%s) '%s' = '%s'\n", ptr, s1, s2);
  39. Serial.flush();
  40. }
  41.  
  42. // Called when there's a warning or error (like a buffer underflow or decode hiccup)
  43. void StatusCallback(void *cbData, int code, const char *string)
  44. {
  45. const char *ptr = reinterpret_cast<const char *>(cbData);
  46. // Note that the string may be in PROGMEM, so copy it to RAM for printf
  47. char s1[64];
  48. strncpy_P(s1, string, sizeof(s1));
  49. s1[sizeof(s1)-1]=0;
  50. Serial.printf("STATUS(%s) '%d' = '%s'\n", ptr, code, s1);
  51. Serial.flush();
  52. }
  53.  
  54.  
  55. void setup()
  56. {
  57. Serial.begin(115200);
  58. delay(1000);
  59. Serial.println("Connecting to WiFi");
  60.  
  61. WiFi.disconnect();
  62. WiFi.softAPdisconnect(true);
  63. WiFi.mode(WIFI_STA);
  64.  
  65. WiFi.begin(SSID, PASSWORD);
  66.  
  67. // Try forever
  68. while (WiFi.status() != WL_CONNECTED) {
  69. Serial.println("...Connecting to WiFi");
  70. delay(1000);
  71. }
  72. Serial.println("Connected");
  73.  
  74. // audioLogger = &Serial;
  75. file = new AudioFileSourceICYStream(URL);
  76. file->RegisterMetadataCB(MDCallback, (void*)"ICY");
  77. buff = new AudioFileSourceBuffer(file, 2048);
  78. buff->RegisterStatusCB(StatusCallback, (void*)"buffer");
  79. out = new AudioOutputI2SNoDAC();
  80. mp3 = new AudioGeneratorMP3();
  81. mp3->RegisterStatusCB(StatusCallback, (void*)"mp3");
  82. mp3->begin(buff, out);
  83. }
  84.  
  85.  
  86. void loop()
  87. {
  88. static int lastms = 0;
  89.  
  90. if (mp3->isRunning()) {
  91. if (millis()-lastms > 1000) {
  92. lastms = millis();
  93. Serial.printf("Running for %d ms...\n", lastms);
  94. Serial.flush();
  95. }
  96. if (!mp3->loop()) mp3->stop();
  97. } else {
  98. Serial.printf("MP3 done\n");
  99. delay(1000);
  100. }
  101. }
  102.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement