In this ESP8266 tutorial we are going to learn how to use your ESP8266 NodeMCU as a WiFi Radio. Yes, we can add multiple radio channels all over the world. ESP8266 will stream live radio, we are also connecting a speaker to ESP8266. As a result you can see how powerful that tiny ESP8266 chip is. So let’s get started.

Components Required:
  1. NodeMCU ESP8266 12E,
  2. Transistor (TIP 122) or NPN 2N3904 transistor,  
  3. 1k ohms resister,
  4. Speaker (3 watt, 4 ohms),
  5. 4 AAA 1.5V Battery Case,
  6. AAA 1.5V Battery,
  7. Jumper Wires,
  8. Breadboard.
Required Library:

You will need to download ESP8266 Audio library for making WiFi Radio,

Open Arduino IDE, go to

Sketches → Include Libraries → Add .Zip File → add downloaded .zip file

Library added successfully , you can see confirmation at the bottom of the Arduino IDE. To know more about how to add library click here.

Supplying power to Speaker:

As ESP8266 only provide 3.3V we can’t give that power to speaker. It may work if you run speaker with ESP8266’s 3.3V. But you may end up damaging your ESP8266. So better solution is provide separate power supply and use transistor. I am using TIP122, you can also use 2N3904.

Setup for Arduino IDE & Radio:
  1. If you haven’t setup your Arduino IDE for ESP8266, First you need to setup Arduino IDE for ESP8266.
  2. Open Arduino IDE, go to tools → Board → NodeMCU 1.0
  3. Go to tools → change CPU frequency to 160 MHz.

Change ESP8266 CPU frequency to 160 MHz

Finding Streaming Link for Radio:

This is very important step, you can find radio streaming link for your country and insert that link into the main code. You can find Radio streaming link from NDR. Currently it contains 11 radio station. You can select anyone, go and play online. Then download m3u file.

Download m3u file for streaming

Open that downloaded m3u file in VLC player. Click on left side VLC player icon, now you can see media information. Copy location address.

Copy streaming link from VLC playerPaste this streaming link into our main code.

const char *URL="http://ndr-edge-206c.fra-lg.cdn.addradio.net/ndr/njoy/live/mp3/128/stream.mp3"; //'N-JOY vom NDR - www.n-joy.de'

Note: Do not use https:// in streaming link. use http:// even if it contain https:// link. otherwise it will not work.

Circuit diagram for Wi-Fi Radio with ESP8266.

ESP8266 Wifi radio stream

Note: Remove Rx Pin before uploading the code, if it is connected, Arduino IDE will show you error.

Code for WiFi Radio with ESP8266:
#include <Arduino.h>

#ifdef ESP32
#include <WiFi.h>
#else
#include <ESP8266WiFi.h>
#endif
#include "AudioFileSourceICYStream.h"
#include "AudioFileSourceBuffer.h"
#include "AudioGeneratorMP3.h"
#include "AudioOutputI2SNoDAC.h"

// To run, set your ESP8266 build to 160MHz, update the SSID info, and upload.

// Enter your WiFi setup here:
const char *SSID = "Wi-Fi";
const char *PASSWORD = "Password";

// Uncomment one link (I have added 6 radio streaming link, you can check each)

//flawlessly working radio streaming link
const char *URL="http://ndr-edge-206c.fra-lg.cdn.addradio.net/ndr/njoy/live/mp3/128/stream.mp3"; //'N-JOY vom NDR - www.n-joy.de'
//const char *URL="http://ndr-edge-10ad-fra-dtag-cdn.cast.addradio.de/ndr/ndr1niedersachsen/hannover/mp3/128/stream.mp3";
//const char *URL="http://jazz.streamr.ru/jazz-64.mp3";

// It will work but buffer alot
//const char *URL="http://stream.ca.morow.com:8003/morow_med.mp3";
//const char *URL= "http://ndr-ndr1radiomv-schwerin.sslcast.addradio.de/ndr/ndr1radiomv/schwerin/mp3/128/stream.mp3";
//const char *URL="http://mms.hoerradar.de:8000/rst128k";//Radio RST(German)


AudioGeneratorMP3 *mp3;
AudioFileSourceICYStream *file;
AudioFileSourceBuffer *buff;
AudioOutputI2SNoDAC *out;

// Called when a metadata event occurs (i.e. an ID3 tag, an ICY block, etc.
void MDCallback(void *cbData, const char *type, bool isUnicode, const char *string)
{
const char *ptr = reinterpret_cast<const char *>(cbData);
(void) isUnicode; // Punt this ball for now
// Note that the type and string may be in PROGMEM, so copy them to RAM for printf
char s1[32], s2[64];
strncpy_P(s1, type, sizeof(s1));
s1[sizeof(s1)-1]=0;
strncpy_P(s2, string, sizeof(s2));
s2[sizeof(s2)-1]=0;
Serial.printf("METADATA(%s) '%s' = '%s'\n", ptr, s1, s2);
Serial.flush();
}

// Called when there's a warning or error (like a buffer underflow or decode hiccup)
void StatusCallback(void *cbData, int code, const char *string)
{
const char *ptr = reinterpret_cast<const char *>(cbData);
// Note that the string may be in PROGMEM, so copy it to RAM for printf
char s1[64];
strncpy_P(s1, string, sizeof(s1));
s1[sizeof(s1)-1]=0;
Serial.printf("STATUS(%s) '%d' = '%s'\n", ptr, code, s1);
Serial.flush();
}


void setup()
{
Serial.begin(115200);
delay(1000);
Serial.println("Connecting to WiFi");

WiFi.disconnect();
WiFi.softAPdisconnect(true);
WiFi.mode(WIFI_STA);

WiFi.begin(SSID, PASSWORD);

// Try forever
while (WiFi.status() != WL_CONNECTED) {
Serial.println("...Connecting to WiFi");
delay(1000);
}
Serial.println("Connected");

audioLogger = &Serial;
file = new AudioFileSourceICYStream(URL);
file->RegisterMetadataCB(MDCallback, (void*)"ICY");
buff = new AudioFileSourceBuffer(file, 8192);
buff->RegisterStatusCB(StatusCallback, (void*)"buffer");
out = new AudioOutputI2SNoDAC();
mp3 = new AudioGeneratorMP3();
mp3->RegisterStatusCB(StatusCallback, (void*)"mp3");
mp3->begin(buff, out);
}

void loop()
{
static int lastms = 0;

if (mp3->isRunning()) {
if (millis()-lastms > 1000) {
lastms = millis();
Serial.printf("Running for %d ms...\n", lastms);
Serial.flush();
}
if (!mp3->loop()) mp3->stop();
} else {
Serial.printf("MP3 done\n");
delay(1000);
}
}
Code Explanation:

Insert your wifi credential.

const char *SSID = "WiFi";
const char *PASSWORD = "Password";

I have added 6 radio streaming links, you can check each link by uncommenting each one by one. These streaming links are working flawlessly. You can add your favorite radio streaming link here.

//flawlessly working radio streaming link
const char *URL="http://ndr-edge-206c.fra-lg.cdn.addradio.net/ndr/njoy/live/mp3/128/stream.mp3"; //'N-JOY vom NDR - www.n-joy.de'
//const char *URL="http://ndr-edge-10ad-fra-dtag-cdn.cast.addradio.de/ndr/ndr1niedersachsen/hannover/mp3/128/stream.mp3";
//const char *URL="http://jazz.streamr.ru/jazz-64.mp3";

// It will work but buffer alot
//const char *URL="http://stream.ca.morow.com:8003/morow_med.mp3";
//const char *URL= "http://ndr-ndr1radiomv-schwerin.sslcast.addradio.de/ndr/ndr1radiomv/schwerin/mp3/128/stream.mp3";
//const char *URL="http://mms.hoerradar.de:8000/rst128k";//Radio RST(German)
ESP8266 radio stream 35 minutes

streaming flawlessly

If radio contentiously shows you buffering or play for few seconds and again start buffering, To solve this problem, you can increase buffer size, play with this settings, 2048, 4096, 8192. Below image is example of buffering.

buff = new AudioFileSourceBuffer(file, 8192);
Errors & Buffering while streaming ESP8266 Radio

Buffering

Radio continuously buffering:

1) This can happen, solution to this issue is increase buffer size 2048,4096, 8192. If this doesn’t solve the problem.

2) You can try a different streaming server, or if there is a lower bit rate they can stream at, try that. Because PC can buffer audio for many seconds, but the ESP only has ram for 1/4 of a second.

3) It’s probably network buffering issues between the server and your ESP8266.

Video:

If you like this kind of content please subscribe to my YouTube channel for latest updates. Till Then Keep Learning Keep Making.