Page 1 of 1

ESP32 audio streaming with Raspberry Pi 4

Posted: Fri Nov 15, 2024 10:14 am
by alimert7
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?

Re: ESP32 audio streaming with Raspberry Pi 4

Posted: Sun Nov 17, 2024 7:30 am
by aliarifat794
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:

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);
  }
}