Internet-Radio mit dem ESP32 - UPDATE - AZ-Delivery

Update: The reader Andreas Schröder has changed the project a little and supplemented some functions. You can find the text and sketch at the end of the post.

Many radio stations can be heard as a MP3 stream over the Internet. Since the microcontroller ESP32 has WLAN skills on the one hand and, on the other hand, can convert the digital data stream into an analog signal with two built-in digital/analog converters, it offers itself as an ideal solution. In addition, a battery power supply, an audio amplifier, two speakers, a display for the transmitter display and an input device for the transmitter setting is required. The whole thing is rounded off with a housing from the 3D printer.

Required hardware

Number Component annotation

1

ESP32 Development Board

 

1

Audio amplifier 3W

 

1

speaker

 

2

Resistance 4.7 KOHM

 

2

Resistance 22 KOHM

 

1

Resistance 10 kohm

 

1

Elko 1000UF / 10V

 

1

3.7V battery 2000mAh

 

1

Loader

 

1

DC-DC Step Up Converter

 

1

LCD display with I2C interface

 

1

Distributor Encoder

 

1

Lochraster plate 50x70

 

2

Spring strips 19-pin

 

1

Park bar 3-pin

 

1

 Park bar 4-pin

 

1

Pin bar 5-pin

 

2

Turntons for 6mm axis

 

Several

Jumper cable female too female

 

1

Housing front part from the 3D printer

 

1

Housing back from the 3D printer

 

1

Lid for battery protection from the 3D printer

 

various

Fastening screws 2.2mm

 

 

circuit

 

Important!
If the Rotary Encoder is not used, the PIN 34 of the ESP32 must still be connected to the 10 co-cock resistance with 3.3V.

The ESP32 is built up on a 50x70 mm hole grid plate with the resistances and pen strips for the periphery.


The figure shows the assembly and wiring on the underside

wiring

First of all, the loader's battery connection is connected to the input of the DC-DC Step Up converter. Note the polarity! The battery is also soldered via a suitable connector with the battery input of the loader. Now the output voltage of the converter with the blue potentiometer should be set to approx. 5.2 V. To do this, either a battery must be connected or the loader's USB input must be connected to a USB network.

If the voltage is set, the output of the converter can be connected to the supply receipt of the audio amplifier. There are two soldering points on the back of the amplifier, the plus connection is switched over the switch of the volume potentiometer.

 amplifier underside

This connection is used to supply the ESP32 and the display so that the device can be switched off completely via the potentiometer.
Now the connections to the control board can be made on the hole grid plate. It is best to use jumper wires with two female plugs. You need a 3-pin connection from the audio output to the amplifier, a 4-pin to the display and a 5-pin to the Rotary Encoder.

Important NOTE!
The loudspeakers should not be attached or staked out when switched on, since inductive tension peaks could destroy the amplifier outputs.

If the housing presented in the blog post is used, battery, loader, DC/DC converter and the control board come to the baking tarpaulin. The speakers, the amplifier, the Rotary Encoder and the display come to the front tarpaulin. The lid is used to secure the battery.

 baking tarpaulinassembly

software

In order for the sketch to be compiled, the Arduino IDE must be prepared accordingly. By default, Arduino IDE supports a large number of boards with different microcontrollers, but not the ESP32. So that you can create and upload programs for these controllers, a software package for the support must therefore be installed.

First you have to communicate the Arduino IDE where you can find the additional data required. To do this, open the Preferences in the File menu. In the preset window there is the input field called "Additional board administrator URLS". If you click on the icon to the right of the input field, a window in which you can open the URL https://dl.espressif.com/dl/package_esp32_index.json for the ESP32 can enter. Version 2.0.0 or higher of the ESP32 package should not be used for this project, since these versions have problems related to the ESP8266audio library!

Now choose the board management in the Arduino IDE under tool → board.

A window opens in which all available packages are listed. In order to narrow down the list, you enter "ESP32" in the search field. Then you only get an entry in the list. Install the "ESP32" package.

ESP32 Lib
For the display you need a library that can be installed via the Arduino library management. This is the library "Liquidcrystal I2C".

Liquid Crystal Lib

Another library is required for the Rotary Encoder. Her name is "Aiesp32rotary Coder".

Rotary Encoder Lib 

The core of this project is the library "ESP8266audio".

 audio lib

This library enables various digital input currents to be read, decoding and reproducing them using various output channels. As an entrance, the program memory, the internal RAM can be used by an SD card, an HTTP stream or an ICY stream. The icy stream is typically used by internet radios.
WAV, MOD, MIDI, Flac, AAC and MP3 files can be decoded. MP3 is required for the web radio. The output can finally be made in memory, files or I2S. There is a special feature for the ESP32. The I2S Output can be output on the internal digital analog converter. An analog stereo signal is then available at the output pins of the DAW (PIN 25 and PIN 26). This feature is used in the present project.

If all libraries are installed, the sketch can be compiled and uploaded to the hardware.

The sketch

#include <Wifi.H> 
// Includes from ESP8266audio
#include "AudioFileesourceiSstream.h" // input stream
#include "AudioFileesourcebuffer.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 "AiesP32rotaryScoder.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 Behavior
//#define rotary_encoder_steps 1
//#define rotary_encoder_steps 2
#define Rotary_encoder_steps 4

// Structure for Station List
type struct {
  char * url;  // stream url
  char * Surname; // station name
} station;

#define Ward 24 // Number of Stations in Tzhe List

// Station List Can Easily Be Modified to Support Other Stations
station wardlist[Ward] Progmem = {
{"http://icecast.ndr.de/ndr/ndr2/niedersachsen/mp3/128/stream.mp3","NDR2 Lower Saxony"},
{"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/franken/mp3/low","Bayern1"},
{"http://dispatcher.rndfnk.com/br3/live/mp3/low","Bayern 3"},
{"http://dispatcher.rndfnk.com/hr/hr3/live/mp3/48/stream.mp3","Hessen3"},
{"http://stream.antentne.de/antne","Antenne Bayern"},
{"http://stream.1a-webradio.de/saw-deutsch/","Radio 1a German hits"},
{"http://stream.1a-weibradio.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","Bavaria Classic"},
{"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/antnebrandenburg/live/mp3/mid","Antenne Brandenburg"},
{"http://wdr3-live.iceCasttssl.wdr.de/wdr/wdr3/live/mp3/128/stream.mp3","WDR3"},
{"http://wdr2-aachenundregion.iceCasttssl.wdr.de/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 90s"},
{"http://mp3.hitradiort1.c.nmdn.net/rt1rockwl/livestream.mp3","RT1 Rock"}};

// Buffer size for stream buffering
const intimately Preallocatebuffersize = 80*1024;
const intimately PreallocateCodecsize = 29192;         // MP3 Codec Max Mem Needed
// pointer to preallocated memory
void *Preallocatebuffer = ZERO;
void *PreallocateCodec = ZERO;

// Instance of Preferences
Preferences Pref;
// Instance for Rotary Encoder
AISP32Rotary code rotary = AISP32Rotary code(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(0x27,16,2);  // Set the lcd address to 0x27 for a 16 chars and 2 line display
// Instance for Audio Components
Audio *decoder = ZERO;
Audio filesourceistream *file = ZERO;
Audio filesource buffer *buff = ZERO;
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 curing = 0;   // Index for current selected station in station list
uint8_t act = 0;   // Index for current station in station list used for streaming 
uint32_t liability = 0;  // Time of Last Selection Change

// Callback function wants to 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 instantences
void stop playing() {
  IF (decoder)  {
    decoder->Stop();
    delete decoder;
    decoder = ZERO;
  }
  IF (buff)  {
    buff->close();
    delete buff;
    buff = ZERO;
  }
  IF (file)  {
    file->close();
    delete file;
    file = ZERO;
  }
}

//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.init();
  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")) curing = Pref.gut("station");
  IF (curing >= Ward) curing = 0;
  // Set Active Station to Current Station 
  // Show on display and start streaming
  act = curing;
  show station();
  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.remaining start();
  }
  // Read events from Rotary Encoder
  rotary_loop();

} 

Sketch to download

Before compiling, the SSID and the password must be set for the WLAN. At the beginning of the sketch is a list of 24 German radio stations. You can edit or expand them as you like to hear your desired program. A maximum of 100 stations can be defined.

After uploading, the program can be started. The channel list can be scrolled with the Rotary Encoder. If you press the button of the Rotary Encoder, the station that has just been displayed is set as active. This selection is saved in the flash, so that after a power break, the program is started again with the selected transmitter. The station that has just been reproduced is displayed on the display by a preceding loudspeaker symbol.

Finished device

Have fun with the internet radio

Post as a PDF

Update from our reader Andreas Schröder

(thanks at this point)

With a cut black film on white housing, it looks really good.

Because this is not tingling and screwing up, I expanded the code to include the following:

1. An automatic AP mode with a lack of WLAN connection, which then queries the access data via web server.

2. In normal operation, the transmitter list can be maintained via a web front end. Would like to have provided the code, screenshots and the plotter file for the film here.

WIRELESS INTERNET ACCESS

The WLAN configuration is held in the internal flash memory. This can be configured using the web interface. The configuration process is as follows:

  • Charge the saved login data

  • Attempt to make connections (display shows "WLAN")

  • If that is not possible

    • Change to AP mode and create the WLAN "WebRadio"

    • Expect the data entry under http://192.168.4.1

    • Restart with new data

==> The whole thing is repeated until a connection is possible

 

 

Transmitter list

The list of stored transmitters can above http: // be adjusted.

 

Sketches download

 

Plotter file (silhouette format)

DisplaysEsp-32Projekte für anfängerStromversorgung

308 commenti

Gerald Lechner

Gerald Lechner

Hallo Konrad, der Kondensatore kommt zwischen +5V und Masse am ESP32. Ich habe den Kondensator in der Schaltung hinzugefügt. Er ist aber nicht unbedingt notwendig.

Tom

Tom

Hallo!
DT an G23 ist wohl ein Typo – lt. Zeichnung müsste DT an G33 führen – auch lt. Code:
#define ROTARY_ENCODER_BUTTON_PIN 34

Ich hab mich auch dran schon versucht. Mangels eines ESP32 NodeMCU Borad habe ich ein Mini D1 ESP32 benutzt. Die nötigen Pins sind an dem Borad ja auch alle vorhanden.
In der Arduino IDE hab ich dazu in der Bordverwaltung auch “WEMOS D1 Mini ESP32” konfiguriert. Kompilieren und Laden auf’s Borad läuft (bis auch ein paar Warnings) tadellos durch.

Nach dem Reset steht kann ich auch erfreulicherweise diese Log-Ausgabe sehen:
Connecting to WiFi
…Connecting to WiFi
…Connecting to WiFi
…Connecting to WiFi
Connected
Active station http://dispatcher.rndfnk.com/br/br1/nbopf/aac/low
sourcebuffer created – Free mem=155840
created decoder
Decoder start…

Leider erhalte ich aber an DAC-Pins 25 bzw. 26 noch kein Audio-Signal.
Noch hab ich keine weitere Peripherie am Borad angeschlossen – weder den Drehgeber, das LCD , noch den Audio-Verstärker, da ich diese Teile noch nicht habe. Die Log-Ausgabe sieht aber so aus, als ob es schon funktionieren sollte.
Kann es an der noch fehlenden Beschaltung am DAC liegen? Mit dem Oszi kann ich an den Pins lediglich einen ca. 22us langen Rechteckpuls feststellen, der mit einer Frequenz von rund 85Hz ansteht.
Um einen Defekt des Bords möglichst auschliessen zu können, hab ich es mit einem zweiten versucht, allerdings mit identischem Ergebnis.

Gruß
Tom

T.Gelhard

T.Gelhard

Hallo
Wo bekomme ich die datei ( Preferences.h ) her?

Gerald Lechner

Gerald Lechner

Hallo oldman, der Rotary-Encoder wird wie folgt angeschlossen: GND an Masse, + an 3.3V (nicht an 5V, das würde die Eingänge des ESP32 beschädigen), SW an G34 und über einen 10kOhm Widerstand auf 3.3 V, DT an G23 und CLK an G32. Für DT und CLK sind Pullup-Widerstände auf der Platine des Rotary-Encoders vorhanden.

fred

fred

“Eine Frage: Wo in der Schaltung ist der Kondensator drin?”

Das ist im Doppeldiagramm der Lochrasterplatine erkennbar. Der Kondensator ist dort zwischen den Anschlüssen “GND” und “+5V” verbunden.

Tanterolf

Tanterolf

Also bei mir läuft der Code nicht durch.
Da hapert es am LiquidCrystal_I2C.

Beste Grüße
Tante Rolf

oldman

oldman

Rotary encoder nur +V?

Konrad

Konrad

Hallo,
Scheint ein Klasse Projekt für schlechtes Wetter zu sein. Bin schon dabei das genau mal zu prüfen.

Eine Frage: Wo in der Schaltung ist der Kondensator drin? Auf den Bildern kann ich Ihn gut erkennen, aber leider bin ich nicht Elekroniker genug, um zu wissen wie die Schaltung dafür sein muss.
Danke im Nachgang.

Konrad

Lascia un commento

Tutti i commenti vengono moderati prima della pubblicazione