Read status of serial mp3 player doesnt work

Hi, I am using

  • Arduino Mega
  • OPEN-SMART Serial MP3 Player

I want the Arduino to do something "as long as" a song is playing (for instance light a led), and stop it once the song finished.

I wrote the following code and it seems to successfully play audio:

// Include required libraries:
#include <SoftwareSerial.h>

// Define the RX and TX pins to establish UART communication with the MP3 Player Module.
#define MP3_RX 15 // to TX
#define MP3_TX 14 // to RX

// Define the Serial MP3 Player Module.
SoftwareSerial MP3(MP3_RX, MP3_TX);

///////////////////////////////////////////////////////////////////////////////////////////////////

static int8_t play_song[] = {0x7e, 0x04, 0x42, 0x01, 0x05, 0xef}; // 7E 04 41 00 01 EF, 5th Song
static int8_t player_status[] = {0x7e, 0x02, 0x10, 0xef};
static int8_t select_SD_card[] = {0x7e, 0x03, 0X35, 0x01, 0xef};

//// Play the song.
//static int8_t play[] = {0x7e, 0x02, 0x01, 0xef}; // 7E 02 01 EF
//static int8_t pause[] = {0x7e, 0x02, 0x02, 0xef}; // 7E 02 02 EF

void send_command_to_MP3_player(int8_t command[],int8_t len){

  Serial.print("\nMP3 Command => ");
  for(int i=0;i<len;i++){ 
    MP3.write(command[i]); 
    Serial.print(command[i], HEX); 
  }
  Serial.println();
}

void get_status() {

  if (MP3.available())
  {
    char player_status;

    Serial.println("MP3.available is True");

    send_command_to_MP3_player(player_status, 4);
    for (int i = 0; i < 8; i++) {
      while (!MP3.available()); // wait for a character
           
      player_status = MP3.read();
      Serial.print(player_status, HEX);
    }
    Serial.println();
    
  } else {
    Serial.println("MP3.available is False");
  }
}

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

  // Initiate the Serial MP3 Player Module.
  MP3.begin(9600);
  send_command_to_MP3_player(select_SD_card, 5);
}
void loop() {
  
  ///////////////////////////////////////////////////
  get_status(); 
  delay(500); 
  
  send_command_to_MP3_player(play_song, 6);
  delay(500); 
  
  get_status();
  delay(5000); 
}

However, I'm not quite sure how to check the status of the MP3 player.
According to the manual I need to write "7E 02 10 EF". I tried to as suggested here - this is the get_status() function.
However, MP3 is always not available even if a song is playing.

What am i missing? Is there an easier way to achieve what i need?

Thanks in advance

EDIT -
Adding the part in the manual referring to checking the status.

The documentation says that the "player_status" command returns 9 bytes. You only read 8 bytes.

    for (int i = 0; i < 8; i++) {
      while (!MP3.available()); // wait for a character
           
      player_status = MP3.read();
      Serial.print(player_status, HEX);
    }

Are you saying you get NO responses from the player? If so, that will freeze your sketch at:
while (!MP3.available()); // wait for a character

You are not asking for status unless you have input from the device and you shouldn't get input from the device until you ask for status. You should send the command and then wait a limited amount of time for a response:

void get_status()
{
  char buffer[9];
  unsigned long timer = millis();

  send_command_to_MP3_player(player_status, 4);

  while (!MP3.available() >= 9)
  {
    if (millis() - timer >= 5000)
    {
      Serial.println("Response from player_status timed out.")
      return; // Give up
    }
  }

  for (int i = 0; i < 9; i++)
  {
    buffer[i] = MP3.read();
    Serial.print(buffer[i], HEX);
  }
  Serial.println();

  switch (buffer[7])
  {
    case 0: Serial.println("Status: STOP"); break;
    case 1: Serial.println("Status: PLAY"); break;
    case 2: Serial.println("Status: PAUSE"); break;
    case 3: Serial.println("Status: FAST FORWARD"); break;
    case 4: Serial.println("Status: FAST REWIND"); break;
    default: 
      Serial.print("Status: unknown: "); 
      Serial.println(buffer[7], HEX); 
      break;
  }
}

Thank you very much!

Is there a way to probe the player for status when the player is "idle"?
Putting getStatus() before sending play command returns "Status: unknown: " if anything

Does it display a hex number after "Status: unknown: "? It's supposed to. I would have expected the status to be "STOP" when nothing is playing since the documentation you provided doesn't specify a status for IDLE.

That condition can never be true.

Oops, you're right. I should have written: while (!(MP3.available() >= 9))

I also forgot the ';' at the end of:
Serial.println("Response from player_status timed out.")

Here is the whole thing:

// Include required libraries:
#include <SoftwareSerial.h>

// Define the RX and TX pins to establish UART communication with the MP3 Player Module.
#define MP3_RX 15 // to TX
#define MP3_TX 14 // to RX

// Define the Serial MP3 Player Module.
SoftwareSerial MP3(MP3_RX, MP3_TX);

///////////////////////////////////////////////////////////////////////////////////////////////////

static uint8_t play_song[] = {0x7e, 0x04, 0x42, 0x01, 0x05, 0xef}; // 7E 04 41 00 01 EF, 5th Song
static uint8_t player_status[] = {0x7e, 0x02, 0x10, 0xef};
static uint8_t select_SD_card[] = {0x7e, 0x03, 0X35, 0x01, 0xef};

//// Play the song.
//static int8_t play[] = {0x7e, 0x02, 0x01, 0xef}; // 7E 02 01 EF
//static int8_t pause[] = {0x7e, 0x02, 0x02, 0xef}; // 7E 02 02 EF

void send_command_to_MP3_player(uint8_t command[],int8_t len){

  Serial.print("\nMP3 Command => ");
  for(int i=0;i<len;i++){ 
    MP3.write(command[i]); 
    Serial.print(command[i], HEX); 
  }
  Serial.println();
}

void get_status()
{
  char buffer[9];
  unsigned long timer = millis();

  send_command_to_MP3_player(player_status, 4);

  while (!(MP3.available() >= 9))
  {
    if (millis() - timer >= 5000)
    {
      Serial.println("Response from player_status timed out.");
      return; // Give up
    }
  }

  for (int i = 0; i < 9; i++)
  {
    buffer[i] = MP3.read();
    Serial.print(buffer[i], HEX);
  }
  Serial.println();

  switch (buffer[7])
  {
    case 0: Serial.println("Status: STOP"); break;
    case 1: Serial.println("Status: PLAY"); break;
    case 2: Serial.println("Status: PAUSE"); break;
    case 3: Serial.println("Status: FAST FORWARD"); break;
    case 4: Serial.println("Status: FAST REWIND"); break;
  }
}

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

  // Initiate the Serial MP3 Player Module.
  MP3.begin(9600);
  send_command_to_MP3_player(select_SD_card, 5);
}

void loop()
{

  ///////////////////////////////////////////////////
  get_status();
  delay(500);

  send_command_to_MP3_player(play_song, 6);
  delay(500);

  get_status();
  delay(5000);
}
1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.