Downloading mp3 file using HTTP GET & Saving to SD card using ESP32 and play it using CODEC VS1053B

Thread Starter

Rajbir_Singh

Joined Oct 1, 2022
4
Hi everyone, I am working on a project where I have to save the mp3 file from custom API to sd card and after that play it using esp32 where I am using VS1053B Codec.

I have seen plenty of examples of internet radio which are playing audio through differrent web stations. But first I want to save it as mp3 format then play it using VS1053B codec.

Here is the code
C:
#include <WiFi.h>
#include <HTTPClient.h>
#include <Arduino_JSON.h>

const char* ssid = "MYACCESSPOINT";
const char* password = "MYPASSWORD";

//Your Domain name with URL path or IP address with path
String serverName = "MYAPI";

// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastTime = 0;
// Timer set to 10 minutes (600000)
//unsigned long timerDelay = 600000;
// Set timer to 5 seconds (5000)
unsigned long timerDelay = 5000;

String AUDIO_STRING ;
String AUDIO_STRING Arr[100];

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

  WiFi.begin(ssid, password);
  Serial.println("Connecting");
  while(WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to WiFi network with IP Address: ");
  Serial.println(WiFi.localIP());
 
  Serial.println("Timer set to 5 seconds (timerDelay variable), it will take 5 seconds before publishing the first reading.");
}

void loop() {
  //Send an HTTP POST request every 10 minutes
  if ((millis() - lastTime) > timerDelay) {
    //Check WiFi connection status
    if(WiFi.status()== WL_CONNECTED){
              
      AUDIO_STRING = httpGETRequest(serverName);
      Serial.println(AUDIO_STRING );
    }
    else {
      Serial.println("WiFi Disconnected");
    }
    lastTime = millis();
  }
}

String httpGETRequest(String serverName) {
  WiFiClient client;
  HTTPClient http;
    
  String serverPath = serverName + "?MYAPIPATH";
      
  // Your Domain name with URL path or IP address with path
  http.begin(serverPath.c_str());
 
  // Send HTTP POST request
  int httpResponseCode = http.GET();
 
  String payload = "{}";
 
  if (httpResponseCode>0) {
    Serial.print("HTTP Response code: ");
    Serial.println(httpResponseCode);
    payload = http.getString();
  }
  else {
    Serial.print("Error code: ");
    Serial.println(httpResponseCode);
  }
  // Free resources
  http.end();

  return payload;
}
Basically I want to get the audio from My custom API and want to play on speaker using esp32.
but i am getting this string data but don't know how to convert it in playable audio.

here is the reply

reply.JPG



If anyone has solution for my problem, please let me know
Regards
Rajbir
 

djsfantasi

Joined Apr 11, 2010
8,575
Hi everyone, I am working on a project where I have to save the mp3 file from custom API to sd card and after that play it using esp32 where I am using VS1053B Codec.

I have seen plenty of examples of internet radio which are playing audio through differrent web stations. But first I want to save it as mp3 format then play it using VS1053B codec.

Here is the code
C:
#include <WiFi.h>
#include <HTTPClient.h>
#include <Arduino_JSON.h>

const char* ssid = "MYACCESSPOINT";
const char* password = "MYPASSWORD";

//Your Domain name with URL path or IP address with path
String serverName = "MYAPI";

// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastTime = 0;
// Timer set to 10 minutes (600000)
//unsigned long timerDelay = 600000;
// Set timer to 5 seconds (5000)
unsigned long timerDelay = 5000;

String AUDIO_STRING ;
String AUDIO_STRING Arr[100];

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

  WiFi.begin(ssid, password);
  Serial.println("Connecting");
  while(WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to WiFi network with IP Address: ");
  Serial.println(WiFi.localIP());

  Serial.println("Timer set to 5 seconds (timerDelay variable), it will take 5 seconds before publishing the first reading.");
}

void loop() {
  //Send an HTTP POST request every 10 minutes
  if ((millis() - lastTime) > timerDelay) {
    //Check WiFi connection status
    if(WiFi.status()== WL_CONNECTED){
             
      AUDIO_STRING = httpGETRequest(serverName);
      Serial.println(AUDIO_STRING );
    }
    else {
      Serial.println("WiFi Disconnected");
    }
    lastTime = millis();
  }
}

String httpGETRequest(String serverName) {
  WiFiClient client;
  HTTPClient http;
   
  String serverPath = serverName + "?MYAPIPATH";
     
  // Your Domain name with URL path or IP address with path
  http.begin(serverPath.c_str());

  // Send HTTP POST request
  int httpResponseCode = http.GET();

  String payload = "{}";

  if (httpResponseCode>0) {
    Serial.print("HTTP Response code: ");
    Serial.println(httpResponseCode);
    payload = http.getString();
  }
  else {
    Serial.print("Error code: ");
    Serial.println(httpResponseCode);
  }
  // Free resources
  http.end();

  return payload;
}
Basically I want to get the audio from My custom API and want to play on speaker using esp32.
but i am getting this string data but don't know how to convert it in playable audio.

here is the reply

<omitted>

If anyone has solution for my problem, please let me know
Regards
Rajbir
Those bytes ARE the MP3 file. Writing them byte by byte to a file on the SD card is all that is necessary. Then pass the file name you created to a play method in the library associated with the VS1053B (you do have the associated library?)
 

Thread Starter

Rajbir_Singh

Joined Oct 1, 2022
4
Those bytes ARE the MP3 file. Writing them byte by byte to a file on the SD card is all that is necessary. Then pass the file name you created to a play method in the library associated with the VS1053B (you do have the associated library?)
Yes, I am just stuck in that process only, not able to get proper data
 
Top