1.5.2 Online Audio Streaming
Stream internet radio and online audio directly to the speaker via Wi-Fi.
Overview
This example builds on the audio playback from the previous lesson — but instead of reading MP3 files from the SD card, it streams audio directly from the internet. You’ll connect to Wi-Fi and play a live internet radio station through the I2S speaker.
What You’ll Learn
Streaming audio over HTTP with the
AudiolibraryCombining Wi-Fi connectivity with I2S audio output
Switching audio sources at runtime via Serial input
Before You Begin
Complete the setup steps in Preparation before continuing.
Note
Use the recommended Arduino IDE settings in Recommended Board Settings unless this lesson says otherwise.
Open the example sketch from the code package:
LAFVIN-ESP32-S3-Multimedia-Kit-main/1.Arduino/5.2_Play_Online/5.2_Play_Online.ino
Important
Edit the ssid and password variables in the sketch to match your Wi-Fi network before uploading.
Signal |
ESP32-S3 |
|---|---|
I2S BCLK |
GPIO 42 |
I2S LRC |
GPIO 14 |
I2S DOUT |
GPIO 41 |
Upload and Test
Set your Wi-Fi SSID and password in the sketch.
Connect the board with a USB Type-C data cable.
Open
5.2_Play_Online.inoin Arduino IDE.In the Tools menu, select
ESP32S3 Dev Moduleas the board and choose the correct serial port.Click Upload.
Open Serial Monitor at
115200baud. The board connects to Wi-Fi and begins streaming internet radio:Connecting to WiFi ........ WiFi connected IP address: 192.168.1.100
You should hear audio through the onboard speaker.
Paste a different MP3 stream URL into the Serial Monitor and press Enter to switch to a different station.
How the Code Works
Wi-Fi Connection
#include <WiFi.h>
#include "Audio.h"
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Stream Online Audio
Audio audio;
audio.setPinout(42, 14, 41); // BCLK, LRC, DOUT
audio.setVolume(10);
// Default internet radio stream
audio.connecttohost("http://icecast.omroep.nl/radio4-bb-mp3");
Runtime URL Switching
The sketch reads from Serial in the main loop. If you send any text, it is treated as a new stream URL:
void loop() {
audio.loop();
if (Serial.available()) {
String url = Serial.readString();
audio.connecttohost(url.c_str());
}
}
In the provided sketch:
The default stream points to a public icecast MP3 radio station
Volume is set to 10 to be conservative
Audio metadata (stream title, etc.) is printed to the Serial Monitor when available
Try This Next
Find other free internet radio station URLs and use the Serial Monitor to switch between them
Set up a local audio stream with VLC or another media server on your computer
Combine with 1.5.1 MP3 Playback from SD Card to build a player that switches between local and online sources