0% found this document useful (0 votes)
9 views2 pages

Platform Io BT

The document is a code implementation for an ESP32 that streams audio via Bluetooth A2DP and plays it through a MAX98357 DAC using the I2S interface. It sets up the necessary configurations for I2S and Bluetooth, including handling incoming audio data. The main loop continuously checks for Bluetooth connections and processes received audio data.

Uploaded by

louis
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views2 pages

Platform Io BT

The document is a code implementation for an ESP32 that streams audio via Bluetooth A2DP and plays it through a MAX98357 DAC using the I2S interface. It sets up the necessary configurations for I2S and Bluetooth, including handling incoming audio data. The main loop continuously checks for Bluetooth connections and processes received audio data.

Uploaded by

louis
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <BluetoothA2DP.

h>
#include <driver/i2s.h>

BluetoothA2DP a2dp;

#define I2S_BCK_PIN 26
#define I2S_WS_PIN 25
#define I2S_DATA_PIN 27

void setup() {
Serial.begin(115200);

// Initialize the I2S interface for the MAX98357 DAC


i2s_setup();

// Initialize the Bluetooth A2DP


a2dp.begin();

// Set the device name


a2dp.setName("ESP32 Audio Stream");

// Set the callback function for audio data


a2dp.onDataReceived(onAudioDataReceived);
}

void loop() {
// Continuously check for incoming Bluetooth connections
a2dp.checkAudio();

// Your other code here


}

void onAudioDataReceived(uint8_t* data, size_t length) {


// This function is called when audio data is received

// Process the audio data (e.g., play it through the MAX98357 DAC)
i2s_write(data, length);

// Example: Print the length of the received audio data


Serial.printf("Received audio data: %u bytes\n", length);
}

void i2s_setup() {
// Configure I2S for the MAX98357 DAC
i2s_config_t i2s_config = {
.mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_TX),
.sample_rate = 44100,
.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT,
.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
.communication_format = (i2s_comm_format_t)(I2S_COMM_FORMAT_I2S |
I2S_COMM_FORMAT_I2S_MSB),
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
.dma_buf_count = 8,
.dma_buf_len = 64,
.use_apll = false,
.tx_desc_auto_clear = true,
.fixed_mclk = 0
};

i2s_pin_config_t pin_config = {
.bck_io_num = I2S_BCK_PIN,
.ws_io_num = I2S_WS_PIN,
.data_out_num = I2S_DATA_PIN,
.data_in_num = I2S_PIN_NO_CHANGE
};

i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL);


i2s_set_pin(I2S_NUM_0, &pin_config);
}

You might also like