The diagram above shows Adafruit's UDA1334 I2S DAC. Your I2S DAC may have different connection names.
Here is a full MP3 streaming example that works on the following boards:
- Adafruit Metro ESP32-S3
- Adafruit Metro ESP32-S2
- Feather ESP32 V2
For this example, configure your device settings.toml file to automatically connect to WiFi.
Make the connections to the I2S headphone adapter as shown, and plug in headphones.
- BAT to VIN
- GND to GND
- I2S DATA or DIN to D11 or D27 (depending on which CircuitPython board you are using)
- I2S WORD_SELECT, WSEL, or SCK to D12
- I2S BIT_CLOCK, BCLK, or BCK to D13
Espressif boards can use any combination of pins for I2S, but if you're adapting this to a different microcontroller, there may be specific pin requirements. Update your code.py and wiring accordingly.
Once you have uploaded the example, CircuitPython will restart and stream an MP3 from the streaming site somafm's "Drone Zone" channel.
Update the STREAMING_URL
in your code.py to change streams. The challenge can be to find the correct MP3 streaming URL.
On somafm, you can find alternate URLs by going to the channel page, looking for the "Non-SSL MP3" line, and selecting the "128k" link. This will download a "pls" (playlist) file to your computer, which is a plain-text file that contains the actual link to the stream. (You can also use the SSL MP3 links on most microcontrollers, but there's little reason to, and it might not work as reliably).
Another source of MP3s is podcasts. You can find MP3 URLs within some podcast "rss feed" documents, such as the CircuitPython Weekly Discord meeting RSS feed.
Different services have different ways to find the streaming URL which are sometimes very obfuscated and are changed by site operators with little or no notice.
# SPDX-FileCopyrightText: 2024 Jeff Epler for Adafruit Industries # # SPDX-License-Identifier: MIT # Stream MP3 audio to I2S decoder # # Tested with: # # * Adafruit Metro ESP32-S3 # * Adafruit Metro ESP32-S2 # * Adafruit Feather ESP32 V2 import time import adafruit_connection_manager import adafruit_requests import audiobusio import audiomp3 import board import wifi mp3_buffer = bytearray(16384) mp3_decoder = audiomp3.MP3Decoder("/silence.mp3", mp3_buffer) pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio) ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio) requests = adafruit_requests.Session(pool, ssl_context) STREAMING_URL = "https://ice2.somafm.com/dronezone-128-mp3" if "D27" in dir(board): # Feather ESP32 V2 has D27 instead of D11 i2s = audiobusio.I2SOut(bit_clock=board.D12, word_select=board.D13, data=board.D27) else: i2s = audiobusio.I2SOut(bit_clock=board.D12, word_select=board.D13, data=board.D11) with requests.get(STREAMING_URL, headers={"connection": "close"}, stream=True) as response: mp3_decoder.file = response.socket i2s.play(mp3_decoder) while i2s.playing: time.sleep(0.1)
Page last edited January 22, 2025
Text editor powered by tinymce.