icon

Simple ESP32 Internet radio on VFD Display

  Internet radio, also known as online radio or streaming radio, refers to the broadcasting of audio content over the internet. Unlike traditional radio stations that transmit over the airwaves, internet radio stations broadcast their content exclusively through the internet. Overall, internet radio has become a popular alternative to traditional radio broadcasting due to its accessibility, diversity of content, and quality. In one of my previous videos , I presented how to make an Internet radio with a TFT display and multi-format audio codec module VS1053. 

The device presented in this video represents the simplest way to make an Internet radio with a minimum number of components. The project is presented on the AZ-delivery website, and I made a small hardware modification, so instead of an LCD display, now I use a VFD display, which gives a special retro touch to the device.

    As I mentioned before, the device is very simple to make, and consists of several components:
      - ESP32 Development Board
      - VFM202 MDA1 type VFD display with 20x2 characters and I2C interface module
      - Rotary encoder
     - several resistors
     - and if we don't want to use only headphones, we also need a PAM 8403 amplifier board and a pair of speakers.

  This project is sponsored by PCBWay. They has all the services you need to create your project at the best price, whether is a scool project, or complex professional project. On PCBWay you can share your experiences, or get inspiration for your next project. They also provide completed Surface mount SMT PCB assemblY service at a best price, and ISO9001 quality control. Visit www.pcbway.com for more services
 As for the hardware, you can use a standard I2C LCD Display instead of the VFD without any code changes. In a previous project of mine (https://www.hackster.io/mircemk/diy-arduino-vfd-display-20x2-vu-volume-unit-meter-37898f) there is information on how to modify this VFD display so that you can use it in the same way as a 16x2 LCD with HD44780 chip.

 The only thing we need to pay attention to in this case is the I2C address of the display interface module, which we can determine with the "I2C Scanner" arduino sketch. We need to put the obtained value in the code in the line:
  LiquidCrystal_I2C lcd ( 0x27 , 16 , 2 ) ; // set the LCD address to the specified value.
 This time we will not dwell on the installation of the code, on the esp32 board, because it has been previously described several times. It is only important to emphasize that you must use the libraries given below, as well as esp32 Version 1.0.6 in the Arduino IDE. At the beginning of the code we need to enter the credentials of our local network, and the desired internet radio stations are entered below in the code after the line:
  Station  stationlist [ STATIONS ]  PROGMEM  =  { 
A maximum of 100 stations can be defined.

 

  And now let's see how the device works in real conditions.
Immediately after switching on, the radio connects to Wi-Fi and automatically starts broadcasting the radio station that was first entered in the code. The rotary encoder can be used to scroll through the channel list. If you press the rotary encoder button, the currently displayed station is set as active. This selection is saved in the flash so that after a power interruption the program starts again with the selected station. The station currently being played is indicated in the display by a speaker symbol in front of it. This time for obvious copyright reasons I will not broadcast the audio signal or will broadcast it briefly.
Now let's give a short conclusion. The device presented in this video represents the simplest way to make an Internet radio with a minimum number of components, and  the digital data stream is converted into an analog signal with two built-in digital/analog converters, which is an ideal solution for this project. We can listen to the audio signal directly on headphones, or through a small audio amplifier on speakers.
  And finally, the whole device is embedded in a suitable box made of PVC material with a thickness of 3mm and 5mm, and coated with colored self-adhesive wallpaper.

CODE
#include <WiFi.h> 
//Includes from ESP8266audio
#include "AudioFileSourceICYStream.h" //input stream
#include "AudioFileSourceBuffer.h"    //input buffer
#include "AudioGeneratorMP3.h"        //decoder
#include "AudioOutputI2S.h"           //output stream
//library for LCD display
#include <LiquidCrystal_I2C.h>
//library for rotary encoder
#include "AiEsp32RotaryEncoder.h"
//esp32 library to save preferences in flash
#include <Preferences.h>

//WLAN access fill with your credentials
#define SSID "******"
#define PSK "********"

//used pins for rotary encoder
#define ROTARY_ENCODER_A_PIN 33
#define ROTARY_ENCODER_B_PIN 32
#define ROTARY_ENCODER_BUTTON_PIN 34
#define ROTARY_ENCODER_VCC_PIN -1 /* 27 put -1 of Rotary encoder Vcc is connected directly to 3,3V; else you can use declared output pin for powering rotary encoder */

//depending on your encoder - try 1,2 or 4 to get expected behaviour
//#define ROTARY_ENCODER_STEPS 1
//#define ROTARY_ENCODER_STEPS 2
#define ROTARY_ENCODER_STEPS 4

//structure for station list
typedef struct {
  char * url;  //stream url
  char * name; //stations name
} Station;

#define STATIONS 18 //number of stations in tzhe list

//station list can easily be modified to support other stations
Station stationlist[STATIONS] PROGMEM = {
//{"https://radiocnd.mms.mk/proxy/player/stream", "Kanal 77"},
{"http://icecast.ndr.de/ndr/ndr2/niedersachsen/mp3/128/stream.mp3","NDR2 Niedersachsen"},
{"http://icecast.ndr.de/ndr/ndr1niedersachsen/hannover/mp3/128/stream.mp3","NDR1 Hannover"},
{"http://wdr-1live-live.icecast.wdr.de/wdr/1live/live/mp3/128/stream.mp3","WDR1"},
{"http://wdr-cosmo-live.icecast.wdr.de/wdr/cosmo/live/mp3/128/stream.mp3","WDR COSMO"},
//{"http://radiohagen.cast.addradio.de/radiohagen/simulcast/high/stream.mp3","Radio Hagen"},
{"http://st01.sslstream.dlf.de/dlf/01/128/mp3/stream.mp3","Deutschlandfunk"},
{"http://dispatcher.rndfnk.com/br/br1/nbopf/mp3/low","Bayern1"},
{"http://dispatcher.rndfnk.com/br/br3/live/mp3/low","Bayern3"},
//{"http://dispatcher.rndfnk.com/hr/hr3/live/mp3/48/stream.mp3","Hessen3"},
{"http://stream.antenne.de/antenne","Antenne Bayern"},
//{"http://stream.1a-webradio.de/saw-deutsch/","Radio 1A Deutsche Hits"},
//{"http://stream.1a-webradio.de/saw-rock/","Radio 1A Rock"},
//{"http://streams.80s80s.de/ndw/mp3-192/streams.80s80s.de/","Neue Deutsche Welle"},
{"http://dispatcher.rndfnk.com/br/brklassik/live/mp3/low","Bayern Klassik"},
{"http://mdr-284280-1.cast.mdr.de/mdr/284280/1/mp3/low/stream.mp3","MDR"},
{"http://icecast.ndr.de/ndr/njoy/live/mp3/128/stream.mp3","N-JOY"},
{"http://dispatcher.rndfnk.com/rbb/rbb888/live/mp3/mid","RBB"},
{"http://dispatcher.rndfnk.com/rbb/antennebrandenburg/live/mp3/mid","Antenne Brandenburg"},
{"http://wdr-wdr3-live.icecastssl.wdr.de/wdr/wdr3/live/mp3/128/stream.mp3","WDR3"},
{"http://wdr-wdr2-aachenundregion.icecastssl.wdr.de/wdr/wdr2/aachenundregion/mp3/128/stream.mp3","WDR 2"},
{"http://rnrw.cast.addradio.de/rnrw-0182/deinschlager/low/stream.mp3","NRW Schlagerradio"},
{"http://rnrw.cast.addradio.de/rnrw-0182/deinrock/low/stream.mp3","NRW Rockradio"},
{"http://rnrw.cast.addradio.de/rnrw-0182/dein90er/low/stream.mp3","NRW 90er"}};
//{"http://mp3.hitradiort1.c.nmdn.net/rt1rockwl/livestream.mp3","RT1 Rock"},
//{"http://sluchaj.radiorodzina.pl/RadioRodzinaWroclawLIVE.mp3","Polen"}

//buffer size for stream buffering
const int preallocateBufferSize = 80*1024;
const int preallocateCodecSize = 29192;         // MP3 codec max mem needed
//pointer to preallocated memory
void *preallocateBuffer = NULL;
void *preallocateCodec = NULL;

//instance of prefernces
Preferences pref;
//instance for rotary encoder
AiEsp32RotaryEncoder rotaryEncoder = AiEsp32RotaryEncoder(ROTARY_ENCODER_A_PIN, ROTARY_ENCODER_B_PIN, ROTARY_ENCODER_BUTTON_PIN, ROTARY_ENCODER_VCC_PIN, ROTARY_ENCODER_STEPS);
//instance for LCD display
LiquidCrystal_I2C lcd(0x3f,16,2);  // set the LCD address to 0x27 for a 16 chars and 2 line display
//instances for audio components
AudioGenerator *decoder = NULL;
AudioFileSourceICYStream *file = NULL;
AudioFileSourceBuffer *buff = NULL;
AudioOutputI2S *out;

//Special character to show a speaker icon for current station
uint8_t speaker[8]  = {0x3,0x5,0x19,0x11,0x19,0x5,0x3};
//global variables
uint8_t curStation = 0;   //index for current selected station in stationlist
uint8_t actStation = 0;   //index for current station in station list used for streaming 
uint32_t lastchange = 0;  //time of last selection change

//callback function will be called if meta data were found in input stream
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();
}

//stop playing the input stream release memory, delete instances
void stopPlaying() {
  if (decoder)  {
    decoder->stop();
    delete decoder;
    decoder = NULL;
  }
  if (buff)  {
    buff->close();
    delete buff;
    buff = NULL;
  }
  if (file)  {
    file->close();
    delete file;
    file = NULL;
  }
}

//start playing a stream from current active station
void startUrl() {
  stopPlaying();  //first close existing streams
  //open input file for selected url
  Serial.printf("Active station %s\n",stationlist[actStation].url);
  file = new AudioFileSourceICYStream(stationlist[actStation].url);
  //register callback for meta data
  file->RegisterMetadataCB(MDCallback, NULL); 
  //create a new buffer which uses the preallocated memory
  buff = new AudioFileSourceBuffer(file, preallocateBuffer, preallocateBufferSize);
  Serial.printf_P(PSTR("sourcebuffer created - Free mem=%d\n"), ESP.getFreeHeap());
  //create and start a new decoder
  decoder = (AudioGenerator*) new AudioGeneratorMP3(preallocateCodec, preallocateCodecSize);
  Serial.printf_P(PSTR("created decoder\n"));
  Serial.printf_P("Decoder start...\n");
  decoder->begin(buff, out);
}

//show name of current station on LCD display
//show the speaker symbol in front if current station = active station
void showStation() {
  lcd.clear();
  if (curStation == actStation) {
    lcd.home();
    lcd.print(char(1));
  }
  lcd.setCursor(2,0);
  String name = String(stationlist[curStation].name);
  if (name.length() < 15)
    lcd.print(name);
  else {
    uint8_t p = name.lastIndexOf(" ",15); //if name does not fit, split line on space
    lcd.print(name.substring(0,p));
    lcd.setCursor(0,1);
    lcd.print(name.substring(p+1,p+17));
  }
}

//handle events from rotary encoder
void rotary_loop()
{
  //dont do anything unless value changed
  if (rotaryEncoder.encoderChanged())
  {
    uint16_t v = rotaryEncoder.readEncoder();
    Serial.printf("Station: %i\n",v);
    //set new currtent station and show its name
    if (v < STATIONS) {
      curStation = v;
      showStation();
      lastchange = millis();
    }
  }
  //if no change happened within 10s set active station as current station
  if ((lastchange > 0) && ((millis()-lastchange) > 10000)){
    curStation = actStation;
    lastchange = 0;
    showStation();
  }
  //react on rotary encoder switch
  if (rotaryEncoder.isEncoderButtonClicked())
  {
    //set current station as active station and start streaming
    actStation = curStation;
    Serial.printf("Active station %s\n",stationlist[actStation].name);
    pref.putUShort("station",curStation);
    startUrl();
    //call show station to display the speaker symbol
    showStation();
  }
}

//interrupt handling for rotary encoder
void IRAM_ATTR readEncoderISR()
{
  rotaryEncoder.readEncoder_ISR();
}

//setup
void setup() {
  Serial.begin(115200);
  delay(1000);
  //reserve buffer für for decoder and stream
  preallocateBuffer = malloc(preallocateBufferSize);          // Stream-file-buffer
  preallocateCodec = malloc(preallocateCodecSize);            // Decoder- buffer
  if (!preallocateBuffer || !preallocateCodec)
  {
    Serial.printf_P(PSTR("FATAL ERROR:  Unable to preallocate %d bytes for app\n"), preallocateBufferSize+preallocateCodecSize);
    while(1){
      yield(); // Infinite halt
    }
  } 
  //start rotary encoder instance
  rotaryEncoder.begin();
  rotaryEncoder.setup(readEncoderISR);
  rotaryEncoder.setBoundaries(0, STATIONS, true); //minValue, maxValue, circleValues true|false (when max go to min and vice versa)
  rotaryEncoder.disableAcceleration();
  //init WiFi
  Serial.println("Connecting to WiFi");
  WiFi.disconnect();
  WiFi.softAPdisconnect(true);
  WiFi.mode(WIFI_STA);
  WiFi.begin(SSID, PSK);
  // Try forever
  while (WiFi.status() != WL_CONNECTED) {
    Serial.println("...Connecting to WiFi");
    delay(1000);
  }
  Serial.println("Connected");
  //create I2S output do use with decoder
  //the second parameter 1 means use the internal DAC
  out = new AudioOutputI2S(0,1);
  //init the LCD display
  lcd.begin();
  lcd.backlight();
  lcd.createChar(1, speaker);
  //set current station to 0
  curStation = 0;
  //start preferences instance
  pref.begin("radio", false);
  //set current station to saved value if available
  if (pref.isKey("station")) curStation = pref.getUShort("station");
  Serial.printf("Gespeicherte Station %i von %i\n",curStation,STATIONS);
  if (curStation >= STATIONS) curStation = 0;
  //set active station to current station 
  //show on display and start streaming
  //curStation = 6;
  actStation = curStation;
  showStation();
  startUrl();
}

void loop() {
  //check if stream has ended normally not on ICY streams
  if (decoder->isRunning()) {
    if (!decoder->loop()) {
      decoder->stop();
    }
  } else {
    Serial.printf("MP3 done\n");

    // Restart ESP when streaming is done or errored
    delay(10000);

    ESP.restart();
  }
  //read events from rotary encoder
  rotary_loop();

}
License
All Rights
Reserved
licensBg
0