ESP32 Internet Radio with button

ESP32 Internet Radio with button

To start with I followed this tutorial to find a DAC (Digital Analogue Converter) and how to connected it up.

The Parts I Used

  • Two Full Sized bread boards joined with the build in clips (as in video)
  • ESP32 WEMOS LOLIN32 V1.0.0
  • I2S PCM5102A DAC Decoder 32bit Player Module ebay.co.uk link
  • LAVA Mini Speaker (build in rechargeable battery) / Search for a “Portable Line-In Speakers”
  • Two Push to Make breadboard friendly buttons example thepihut link (one for use in future a blog)
  • Unneeded OLED 0.96-inch Display – Amazon Link (for use in future a blog)

Software Libraries used

https://github.com/schreibfaul1/ESP32-audioI2S/

My example radio station stream links

I got the UK radio streams from http://media-the.musicradio.com

Gold – http://icecast.thisisdax.com/Gold
HeartKent – http://icecast.thisisdax.com/HeartKent
SmoothKent – http://icecast.thisisdax.com/SmoothKent

My board layout

After I entered my WiFi Details and got the example stream working… I found the serial terminal in the Arduino IDE could sent messages back to the esp32. This allowed the channel to change without the need to reupload the program. I saw the station information change and come back in the terminal. The sound output also changed to that station.

At this point I had a internet radio with only one station. It can’t be changed without a computer. To solve this I added a button and modified the code.

I reused the below bold commands to stop the play back and connect to a new host link.

    if(Serial.available()){ // put streamURL in serial monitor
        audio.stopSong();
        String r=Serial.readString(); r.trim();
        if(r.length()>5) audio.connecttohost(r);
        log_i("free heap=%i", ESP.getFreeHeap());
    }

The setup part of the sketch initialises the button as a pullup input. This means the button is normally high via a internal resistor. When pressed it goes to the easiest path ground (LOW in the program).

pinMode(buttonPin, INPUT_PULLUP);

This code from the main loop looks for a predefined button pin4 and switches the station to the value of infolnc.

There is also a denounce timer with makes sure the button is pressed

  //watch for button press
  int buttonRead = digitalRead(buttonPin);
  if (buttonRead != lastButtonState){
    lastButtonState = currentMillis;
  }

  if ((currentMillis - lastPlayDebounceTime) > debouncePlayDelay)
  {
    lastPlayDebounceTime = currentMillis;
    
    if (buttonRead != buttonState) {
      buttonState = buttonRead;
      
      if (buttonRead == LOW) {        
        // only play if the button state is HIGH
        audio.stopSong();

        //switch value of station
        if(infolnc>=2)
          {
            infolnc = 0;
          }
            else
          {
            infolnc++;
          }

        if(infolnc == 0) {
          audio.connecttohost("http://icecast.thisisdax.com/Gold");
        }
        if(infolnc == 1) {
     audio.connecttohost("http://icecast.thisisdax.com/HeartKent");
        }
        if(infolnc == 2) {        audio.connecttohost("http://icecast.thisisdax.com/SmoothKent");
        }
  
        Serial.println(infolnc);
      }
    }