ESP32 audio streaming with Raspberry Pi 4
ESP32 audio streaming with Raspberry Pi 4
hello, I made an mp3 player by connecting an mp3-tf-16p and a speaker to ESP32. Now I installed home assistant on Raspberry Pi and I can play the radio playing in the radio playback section in the media section of HA via my TV, but I want to play it via ESP32 instead. Can you help me?
-
- Posts: 196
- Joined: Sun Jun 23, 2024 6:18 pm
Re: ESP32 audio streaming with Raspberry Pi 4
Your mp3-tf-16p module should be wired correctly to the ESP32.
Connect the speaker to the MP3 decoder module. Here is a basic code:
Connect the speaker to the MP3 decoder module. Here is a basic code:
Code: Select all
#include <WiFi.h>
#include <AudioFileSourceHTTPStream.h>
#include <AudioGeneratorMP3.h>
#include <AudioOutputI2S.h>
const char* ssid = "YOUR_WIFI_SSID";
const char* password = "YOUR_WIFI_PASSWORD";
const char* audioURL = "http://streaming-radio-url"; // Replace with the URL from HA
AudioGeneratorMP3 *mp3;
AudioFileSourceHTTPStream *file;
AudioOutputI2S *out;
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
file = new AudioFileSourceHTTPStream(audioURL);
out = new AudioOutputI2S();
out->begin();
mp3 = new AudioGeneratorMP3();
mp3->begin(file, out);
}
void loop() {
if (mp3->isRunning()) {
if (!mp3->loop()) {
mp3->stop();
}
} else {
Serial.println("MP3 playback stopped.");
delay(1000);
}
}
Who is online
Users browsing this forum: Bing [Bot] and 81 guests