Advertisement
LeventeDaradici

Easiest WIFI WebRadio Player with ESP32, ESP8266 or Wemos D1

Jul 20th, 2021
1,441
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.77 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 = "YOUR WIFI NETWORK SSID";
  17. const char *PASSWORD = "YOUR WIFI NETWORK PASSWORD";
  18.  
  19. const char *URL="http://gamershouse.hu:8000/livemega.mp3";
  20. //const char *URL="http://192.168.0.248:8000/autodj";
  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.   Serial.println("");
  59.   delay(1000);
  60.   Serial.print("Connecting to WiFi");
  61.  
  62.   WiFi.disconnect();
  63.   WiFi.softAPdisconnect(true);
  64.   WiFi.mode(WIFI_STA);
  65.  
  66.   WiFi.begin(SSID, PASSWORD);
  67.  
  68.   // Try forever
  69.   while (WiFi.status() != WL_CONNECTED) {
  70.     Serial.print(".");
  71.     delay(1000);
  72.   }
  73.   Serial.println("Connected");
  74.  
  75.   audioLogger = &Serial;
  76.   file = new AudioFileSourceICYStream(URL);
  77.   file->RegisterMetadataCB(MDCallback, (void*)"ICY");
  78.   buff = new AudioFileSourceBuffer(file, 4096);
  79.   buff->RegisterStatusCB(StatusCallback, (void*)"buffer");
  80.   out = new AudioOutputI2SNoDAC();
  81.   mp3 = new AudioGeneratorMP3();
  82.   mp3->RegisterStatusCB(StatusCallback, (void*)"mp3");
  83.   mp3->begin(buff, out);
  84. }
  85.  
  86.  
  87. void loop()
  88. {
  89.   static int lastms = 0;
  90.  
  91.   if (mp3->isRunning()) {
  92.     if (millis()-lastms > 1000) {
  93.       lastms = millis();
  94.       Serial.printf("Running for %d ms...\n", lastms);
  95.       Serial.flush();
  96.      }
  97.     if (!mp3->loop()) mp3->stop();
  98.   } else {
  99.     Serial.printf("MP3 done\n");
  100.     delay(1000);
  101.   }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement