.. _arduino_play_online: ============================== 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 ``Audio`` library * Combining Wi-Fi connectivity with I2S audio output * Switching audio sources at runtime via Serial input Before You Begin ================ Complete the setup steps in :ref:`preparation` before continuing. .. note:: Use the recommended Arduino IDE settings in :ref:`arduino_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.ino`` in Arduino IDE. #. In the **Tools** menu, select ``ESP32S3 Dev Module`` as the board and choose the correct serial port. #. Click **Upload**. #. Open **Serial Monitor** at ``115200`` baud. 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** .. code-block:: cpp #include #include "Audio.h" WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } **Stream Online Audio** .. code-block:: cpp 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: .. code-block:: cpp 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 :ref:`arduino_play_sdmp3` to build a player that switches between local and online sources