ESP32 audio streaming with Raspberry Pi 4

alimert7
Posts: 1
Joined: Fri Nov 15, 2024 8:33 am

ESP32 audio streaming with Raspberry Pi 4

Postby alimert7 » Fri Nov 15, 2024 10:14 am

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?

aliarifat794
Posts: 200
Joined: Sun Jun 23, 2024 6:18 pm

Re: ESP32 audio streaming with Raspberry Pi 4

Postby aliarifat794 » Sun Nov 17, 2024 7:30 am

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

Who is online

Users browsing this forum: Google [Bot] and 78 guests