Internet Radio Using An ESP32: Instructables

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

instructables

Internet Radio Using an ESP32

by educ8s

Dear friends welcome to another Instructable! Today Athens and by using these buttons we can change
we are going to build an Internet Radio device the Radio Station we are listening to. I have saved
with a big 3.5” display using an inexpensive my favorite radio stations to the memory of the
ESP32 board. Believe it or not, we can now build an ESP32 so I can access them easily. With this
Internet Radio in less than 10 minutes and with less potentiometer, I can change the volume of the
than 30$. There is a lot to cover so, let’s get started! speaker. I display the Name of the Radio Station we
are listening to on a big 3.5” display with a retro User
A few months ago, I completed an Arduino FM Radio Interface. The project works fine and it is very easy to
project which works great and looks even better in my build.
opinion. If you want to see how I built this project you
can read the Instructable here. The problem is that, You can build the same project in less than 10
although this radio looks cool it is not practical minutes but you need to have some experience. If
because I live in a small town in southern Greece and this is your first project, consider building a simpler
the big Greek radio stations I prefer to listen to, do not one first, to gain some experience. Check my
have transmitters around here. So, I listen to my Instructables for simple project ideas and when you
favorite radios online on my laptop or tablet pc which are more comfortable with the Arduino the electronics
is also not so practical. So, today I am going to build come back to build this cool project. Let’s now start
an Internet radio device in order to be able to listen to building our own Internet Radio.
my favorite radio stations from all over the world!
UPDATE 6/6/2019
As you can see, a first version of the project is ready
on a breadboard. Let’s power it up. As you can see The noise issue has been solved by adding an
the project connects to the Internet and then streams isolator transformer. Check out the updated shematic
music from predefined Radio Stations. diagram. Thanks!

I have tuned in to the Real FM radio station from

Internet Radio Using an ESP32: Page 1


https://www.youtube.com/watch?v=eFs2ePMQz8c

https://www.youtube.com/watch?v=4bm22sN5hWE

Step 1: Get All the Parts

We are going to need the following parts:

ESP32 http://educ8s.tv/part/ESP32
MP3 decoder http://educ8s.tv/part/MP3Decoder
Isolation Transformer http://educ8s.tv/part/AudioTransformer
Amplifier http://educ8s.tv/part/PAM8403
3W speaker http://educ8s.tv/part/3WSpeaker
3.5" Nextion Display http://educ8s.tv/part/Nextion35
Push Buttons http://educ8s.tv/part/Buttons
Breadboard http://educ8s.tv/part/LargeBreadboard
Wires http://educ8s.tv/part/Wires

The total cost of the project is around 40$ but if you don’t use a display the cost of the project is around 20$.
Amazing stuff. We can build our own Internet radio with just 20$!

Internet Radio Using an ESP32: Page 2


Step 2: ESP32 Board

The heart of the project is, of course, the powerful Instructable. It will help understand why this chip will
ESP32 board. If you are not familiar with it, the change the way we make things forever! One of the
ESP32 chip is the successor of the popular ESP8266 most exciting things about the ESP32 is that even
chip we have used many times in the past. The though it is so powerful, it offers a deep-sleep mode
ESP32 is a beast! It offers two 32 bit processing which requires only 10 s of current. This makes
cores which operate at 160MHz, a massive the ESP32 the ideal chip for low power applications.
amount of memory, WiFi, Bluetooth and many
other features with a cost of around 7$! Amazing In this project, the ESP32 board connects to the
stuff! Internet and then it receives MP3 data from the radio
station we are listening to, and it sends some
Please watch the detailed review I have prepared for commands to the display.
this board. I have attached the video on this

https://www.youtube.com/watch?v=Mq1YhgS5VkY

Internet Radio Using an ESP32: Page 3


Step 3: MP3 Decoder

The MP3 data is then sent to the MP3 decoder amplified.) That’s why I am using an Isolation
module using the SPI interface. This module uses the transformer to clear the audio from the noise and a
VS1053 IC. This IC is a dedicated hardware MP3 PAM8403 audio amplifier to amplify the audio signal
decoder. It gets the MP3 data from the ESP32 and and then send it to a speaker. I have also connected
converts it really fast into an audio signal. two buttons to the ESP32 just to change the MP3
Stream we are getting data from and a Nextion
The audio signal that it outputs at this audio jack is display to display the Radio station we are listening
weak and noisy, so we need to clear it from the noise to.
and amplify it. (If you are using headphones, the
signal does not need to be cleared from noise or

Step 4: Nextion Display

I chose to use a Nextion display for this project graphical user interface. So, we can use them with
since it is very easy to use. We only need to any microcontroller and achieve spectacular results. I
connect one wire to control it. have prepared a detailed review of this Nextion
display which explains in depth how they work, how
The Nextion displays are new kind of displays. They to use them and their drawbacks. You can read it her
have their own ARM processor at the back which is e , or watch the attached video.
responsible for driving the display and creating the

https://www.youtube.com/watch?v=mVuy33FK9L0

Internet Radio Using an ESP32: Page 4


Step 5: Connecting All the Parts

All we have to do now is to connect all the parts To send data to the display, we only need to connect
together according to this schematic diagram. You one wire to the TX0 pin of the ESP32. After
can find the schematic diagram attached here. The connecting the parts, we have to load the code to the
connection is straightforward. ESP32, and we have to load the GUI to the Nextion
display.
There are two things to note though. The MP3
decoder module outputs a Stereo Signal but I am To load the GUI to the Nextion display, copy the
using only one audio channel in this project. In order InternetRadio.tft file I am going to share with you to
to get the audio signal, I connected an audio cable to an empty SD card. Put the SD card into the SD card
the audio jack of the module, and cut it to reveal four slot at the back of the display. Then power up the
wires inside. I connected two of the wires. One of display, and the GUI will be loaded. Then remove the
them is the GND and the other one is the audio signal SD card and connect the power again.
of one of the two audio channels. If you wish you can
connect both channels to the amplifier module and After successfully loading the code, let’s power up the
drive two speakers. project. It displays the text “Connecting…” for a few
seconds on the display. After connecting to the
Each audio channel must go through the isolation internet the project connects to a predefined radio
transformer to clear any noise present before station. The hardware is working as expected but now
connecting to the amplifier. let’s see the software side of the project.

Internet Radio Using an ESP32: Page 5


Step 6: The Code of the Project

First of all, let me show you something. The code of the project is less than 140 lines of code. Think about it,
we can build an Internet Radio with a 3.5” Display with 140 lines of code, this is amazing. We can achieve all
this using various libraries of course which contain thousands of lines of code. This is the power of Arduino and the
Open source community. It makes things easy for makers.

In this project, I am using the VS1053 library for the ESP32 board.

At first, we have to define the SSID and the Password of the Wi-Fi network. Next, we have to save some Radio
Stations here. We need the host URL, the path where the stream is located and the port we need to use. We save
all this info into these variables.

char ssid[] = "yourSSID"; // your network SSID (name) <br>char pass[] = "yourWifiPassword"; // your network password</p><p>// Few Radio Stations
char *host[4] = {"149.255.59.162","radiostreaming.ert.gr","realfm.live24.gr", "secure1.live24.gr"};
char *path[4] = {"/1","/ert-kosmos","/realfm","/skai1003"};
int port[4] = {8062,80,80,80};

I have included 4 radio stations in this example.

In the setup function we attach interrupts to the buttons, we initialize the MP3 decoder module and we connect to
the Wi-Fi.

Internet Radio Using an ESP32: Page 6


void setup () {

Serial.begin(9600);
delay(500);
SPI.begin();</p><p> pinMode(previousButton, INPUT_PULLUP);
pinMode(nextButton, INPUT_PULLUP);</p><p> attachInterrupt(digitalPinToInterrupt(previousButton), previousButtonInterrupt, FALLING);
attachInterrupt(digitalPinToInterrupt(nextButton), nextButtonInterrupt, FALLING);
initMP3Decoder();
connectToWIFI();
}

In the loop function, first of all, we check if the user has selected a different radio station than the one we are
getting data from. If so, we connect to the new radio station else we get data from the stream and send them to the
MP3 Decoder module.

<p>void loop() {<br>


if(radioStation!=previousRadioStation)
{
station_connect(radioStation);
previousRadioStation = radioStation;
}

if (client.available() > 0)
{
uint8_t bytesread = client.read(mp3buff, 32);
player.playChunk(mp3buff, bytesread);
}
}</p>

That’s all! When the user presses a button, an interrupt happens, and changes the value of a variable which tells
which stream to connect to.

<p>void IRAM_ATTR previousButtonInterrupt() {</p><p> static unsigned long last_interrupt_time = 0;


unsigned long interrupt_time = millis();

if (interrupt_time - last_interrupt_time > 200)


{
if(radioStation>0)
radioStation--;
else
radioStation = 3;
}
last_interrupt_time = interrupt_time;
}</p>

To update the display, we simply send some commands to the serial port.

<p>void drawRadioStationName(int id)<br>{


String command;
switch (id)
{
case 0: command = "p1.pic=2"; Serial.print(command); endNextionCommand(); break; //1940 UK Radio
case 1: command = "p1.pic=3"; Serial.print(command); endNextionCommand(); break; //KOSMOS GREEK
case 2: command = "p1.pic=4"; Serial.print(command); endNextionCommand(); break; //REAL FM GREEK
case 3: command = "p1.pic=5"; Serial.print(command); endNextionCommand(); break; //SKAI 100.3 GREEK
}
}</p>

Now let’s take a look at the Nextion Display GUI. The Nextion GUI consists of a background picture and a picture
which displays the name of the Radio Station. The ESP32 board sends commands to change the name of the
radio station from the embedded images. It is very easy. Please watch the Nextion display tutorial I have prepared
some time ago for more information. You can quickly design your own GUI if you wish and display more things on
it.

Internet Radio Using an ESP32: Page 7


As always you can find the code of the project attached in this Instructable.

Download
https://www.instructables.com/ORIG/FCX/PBTR/JX8T6NF8/FCXPBTRJX8T6NF8.zip

Step 7: Final Thoughts & Improvements

This project is very simple. I wanted a simple an enclosure around this spectacular Art Deco radio.
Internet Radio project skeleton to work with. Now What do you think, do you like the looks of this radio
that a first version of the project is ready we can add or do you prefer something more modern? Do you
many features to it to improve it. First of all, I need to have any other enclosure ideas? Also, do you like
design an enclosure to house all the electronics. this Internet Radio project and what features do think
we need to add to it to make it more useful? I would
In this book about the Most Beautiful Radios ever love to read your thoughts and Ideas so, please post
made there are very cool radios to choose from as an them in the comments section below.
enclosure for this project. I think I am going to build

Internet Radio Using an ESP32: Page 8


I have my ESP32 radio based on this instructable working decently. Built into a wooden cigar box,
it can hang on the wall. Has battery, charging board, boost converter, and small 2 or 3 watt (?) 5v
amplifier board
I only have 3 stations programmed in and am not using a display (yet). The up and down buttons
are somewhat ambiguous. Sometime a change is made with a press of a button sometime not.
Sometime I think I can hear it jump over a station either going up or down. When a change of
station is actually made, there is a loud squawk from the speakers as it connects. Please, if
anyone has experienced the same sqwak or noise, and have a solution, please post.
As someone mentioned, finding the port is somewhat allusive.
My next mod will be an OLED as suggested by Klaus-JürgenE, or some other means of indicating
the station playing.
This 5 month, on & off, project has been a source of fun and education.Thank you and all the
people making comments and suggestions.

Hello my friend! Great build. Thanks for sharing the photos. Also, check the update video I
published a few days ago:

//www.youtube.com/embed/4bm22sN5hWE

It wil lprobaply solve your noise issues. ;-) Cheers!

Great project! It made so much fun. ...and worked. Made some modifications:
- used a cheap OLED I2C Display
- changed to stereo
Please find attached some pictures of my 3D printed enclosure.

Hi. Do you think is possible to use this one: https://www.amazon.de/diymore-Wireless-Bluetooth-


Battery-Development/dp/B07GFMWDZD

If I don’t need a fancy monitor and I need a battery (for portability)


Thanks
I'm sure this one works.
I used a normal ESP32 plus this one: https://de.banggood.com/0_96-Inch-4Pin-White-IIC-I2C-
OLED-Display-Module-12864-LED-For-Arduino-p-958196.html?
gmcCountry=DE&currency=EUR&createTmp=1&utm_source=googleshopping&utm_medium=cpc_bgcs&utm_content=
deg-ele-pc&gclid=Cj0KCQjw7YblBRDFARIsAKkK-
dKL2rqjiBSgA3Tp1YGnnC6sL_5u6V3jrsyyGBs1B8ySMP2FE_ts6Z4aAk_yEALw_wcB&cur_warehouse=CN

I think this will be somewhat cheeper in total.

Internet Radio Using an ESP32: Page 9


Nice work on keeping the cost down with the OLED. could you share the part of the code that
writes to the OLED/ Especially clearing the display each time the source changes. Tnx.
Thank you very much for the nice words. The noise issue can be solved by either using an Isolator
Transformer or a BA3121 isolation amplifier. I just ordered the parts to try them. I will keep you
updated. More info here: https://github.com/Edzelf/ESP32-Radio/issues/66
What do you mean by "noise"? I hear a very garbled audio, what can I do about it? It's like it's
played in small chunks separated one from the other.
Is it possible to install the VS1053 library in Arduino IDE without Platform IO?

Very nice project...I have assembled it and...all is working fine (I have changed the radio's
channels, changed the Wifi connection to be on the Eudoram network). But there is a very
annoying problem: after more or less one hour, the device bug and stop of working...have any idea
about that (buffer...)? Thanks in advance
...I added a software watchdog and I tried an another ESP WROOM32 card...same problem, after
1 hour it stop to work!!!
I have the same problem.

I have to check it out. Thanks for reporting! Cheers!

solutions?
Did anybody try adding some debug output and keep the radio alive and connected to PC for
debugging for some hours/days?
Can this sketch download and play MP3 files from any server or just from "official" web radios?
I mean, can I connect it to my own server? Or to an MP3 on dropbox, onedrive, whatelse...?
Great Project! Added the channel selector to the Nextion screen and upgraded the amplifier.

Hi... you say "not all streams use the same formats". So... which format do we need to search for?
Can you recommend specific websites where you can choose stations in the right format? Or is it
trial and error? Thanks.
It was a great experience

Great project and easy to build. Thanks a lot for sharing your work. The only thing I can't do is
change the list of stations. I was able to change the one with the IP address but not much more.
Not being the smartest cookie in the jar I tried to figure it out but me and ESP32 are new found
friends and I am not quite sure of how it does things.:) . I have built one with a pi and it had a
playlist you can edit. If I could know what the items in the list mean it would be a great help.
Again, Thanks and good luck. It will go into an old Grundig case.
this is great project ... i just cant find esp_wifi.h and HTTPClient.h ... is it possible to post these
libraries or give us download link, pls ... thanks
Where do I find the library: esp_wifi.h? I've been searching the internet, but can't seem to find
anything.
It's alive! It's alive! Just in the infant stage but it does work --- somewhat. Only change to the code

Internet Radio Using an ESP32: Page 10


was entering my LAN info. Your music station is OK with me. I'll have to learn how to find and
enter my personal preferences. Plenty of hints in the comments. I'm hoping to use a small OLED to
just display the station call sign. Down side so far is the audio is broken up in spurts, for the loss of
a better word. Like I said in my first comment. Thanks for an internet radio that I can actually build.
The cabinet comes later.

Great project. Well designed and documented.


I used a different, simpler, display. SSD1306_128x32 OLED
I didn't need an amplifier, as I have one at my desk, where I am using the radio.
I mounted it all in a simple plastic box, interconnecting it all with jumpers I had "in inventory."
I did have to solder one multi-point jumper together for the 4 ground connections, but all other
connections are point to point, so no PC Board is required. The display runs brightly on 3.3V and
the MP3 decoder connects to Vin, so even those connections are point to point.
I use the USB cable with a newish wall wart for power, which causes no hum. (An older wall wart
did put hum in the audio.) For station or other code changes, I can easily unplug from the wall wart,
and into the PC.
I made one significant code change, which was to make interrupts active on RISING, which
provides more reliable results with push buttons. I also doubled the number of stations in the list to
8, which was straight forward.

Comment: Many of the internet feeds are ".pls" which I have been told means that there are
several URLs associated with switching between them to reduce the intermittency of internet
streaming. I couldn't figure out how to make those streams play.
If anyone has code or knows the syntax to make those streams play I would like to know.

I constructed the ESP32 Web Radio, but the upload of the code gives this error code: esptool.py
v2.3.1 connecting ………._________...………________...………_________ then the error occurs.
A fatal error occurred: Failed to connect to ESP32. I don't know what is wrong and how to fix it. Al
The ESP32 development module has a little switch next to the USB connector labeled "boot".
When you see the Arduino system trying to up load, push that button until the PC shows the
program loading. If that doesn't help, make sure that you are using the correct COM port from the
tools menu. Using device manager, you should see something like, "Silicon Labs CP210x USB to
UART Bridge", followed by a COM port number. Use that COM port to program your board. If
THAT doesn't help, then I am not sure what the issue would be. Hope this helps!
Finally finished. Had more trouble with the case I built from scraps and a bit of burlap than I did
with the electronics although there were some challenges there. I learned a lot about the ESP32 in
the process. Struggled with finding the right libraries to use.

The amp worked fine for me. a bit of tick in the audio but not a worry as I wanted the old time look
and sound anyway. I found I could listen to none of the stations you had as default as the stutter
was too bad. I live in Australia and the ping to your stations listed was over 300 msec. When I
found local stations it worked fine and I could even listen to overseas stations if the bit rate was no
higher than 128 kbps.
I too had trouble finding station information with mp3 bit rate and port information until I stumbled

Internet Radio Using an ESP32: Page 11


upon fmstream.org. Great site!

Thanks again for a fun project and great intro to ESP32 for me.

Built this without the display. Pin 32 for the SPI CS did not work for me. When I switched it to 5,
the default CS, it worked. The audio amplifier is a disaster. Mine did not come with the nut need to
attach it to a panel, and the thread is a really odd size I could not find. It's no doubt metric in an odd
pitch. Also the drive power to a small speaker is very poor. I'm looking for another audio amp for
this project.
Very nice project! Sadly I have the same problem as DurkV: won't compile due to error in
HTTPClient. Wich library must I use?
Great 'ible!

I just got my first ESP32, and have been trying to figure out what to do with it. I have a lot of
learning to do before I take on a project like this, but I'm looking forward to trying it one day.

I do have a couple of questions. For your project you chose to hard wire a speaker. Since the
ESP32 is Bluetooth capable, what would need to be done differently to connect to a Bluetooth
speaker? Would that clean up some of the audio issues?

Also, I bought an Open-Smart 3.5" TFT LCD Shield. Would this work for the display on a project
like this?

Thanks so much for your input. I'm already looking through your other 'ibles to find something
simple to start with!

That's actually a really good idea to use a bluetooth speaker! It would simplify the build quite a bit,
I'm just not sure if there's an easy way to do it. Looks like there's an example that may be able to
connect to and stream to a BT speaker:
https://github.com/espressif/esp-idf/tree/master/examples/bluetooth/a2dp_source
I don't know much of electronics, but i think this is a good and cheap project!
Maybe this article will help you?
https://github.com/MrBuddyCasino/ESP32_MP3_Decoder
Yeah, I could be wrong, but it looks like that project is a web radio that also doubles as a BT
speaker (receiving the audio).
Good Instructions, Thanks for sharing information about ESP32

Hi guys, you might be interested to visit Radio Garden, that's an application that allows you to
listen FM stations from around the world. I hope the stations have downloadable links, so you can
record them in your receiver. Thanks to Nick for his invention!
Very nice! Want to built it too!

What lib:
#include <HTTPClient.h>

Internet Radio Using an ESP32: Page 12


did you use?

I found one, but it produces a lot of errors during compiling.


I've got an old 1930s Philco "cathedral" radio from my grand parents. It's worth about $500 fully
restored, but would cost nearly that to restore it. I have toyed with the idea of using modern guts in
it and restoring the cabinet. Hearing a 1930s-40s radio show from it would be a blast... and this
would be a good way to do it...
Very easy to build and really nice .bravo!!!

the NEXTION DISPLAY is ***NOT*** cheap! LOL!

I bought an internet radio about 5 years ago and use it daily. I wanted to play old time radio and a
different era of tunes. There are a lot of internet channels out there for whatever interests you. I
won't be making the project because it's too advanced for me and as I said, I already have one, but
I think it's a very worthy project for someone who can do it. My internet radio from C. Crane wasn't
cheap, and I think this one will do the same thing for much less. Great instructable!
Excellent project. Your audio noise could be due to the routing of the 0v line. For audio the
suggestion is to use a single point for the 0v not a long linear bus. You could also try adding a
100uf 16v capacitor across the 5v rail with a 0.1uf ceramic capacitor in parallel.
A possible improvement to the software I would like would be the ability to input the WiFi details so
the radio can be used in several locations but I can see that would complicate things!
Excellent performance! This is something that I have long wanted to do for myself.

Fan-tastic! Keep on rocking...

Y . At last a simple, low cost internet radio even I can build. I've ordered the ESP 32
and the MPS decoder. I won't use the display and have the remaining parts. Good things come to
those willing to wait.
great project. well presented. Did you manage to clean up the audio? You mentioned you were
getting some background noise.

Internet Radio Using an ESP32: Page 13

You might also like