Making MP3 file play through speaker using ESP32 and RFID-RC522

hi, I am trying to get the tutorial https://esp32io.com/tutorials/esp32-rfid-mp3-player to work.

#include <SPI.h> #include <MFRC522.h>

#define CMD_PLAY_NEXT 0x01 #define CMD_PLAY_PREV 0x02
#define CMD_PLAY_W_INDEX 0x03 #define CMD_SET_VOLUME 0x06
#define CMD_SEL_DEV 0x09 #define CMD_PLAY_W_VOL 0x22
#define CMD_PLAY 0x0D #define CMD_PAUSE 0x0E
`#define CMD_SINGLE_CYCLE 0x19

#define DEV_TF 0x02 #define SINGLE_CYCLE_ON 0x00
`#define SINGLE_CYCLE_OFF 0x01

#define SS_PIN 5 // ESP32 pin GPIO5 connected to the SS of the RFID reader #define RST_PIN 27 // ESP32 pin GPIO27 connected to the RST of the RFID reader

`#define SONG_NUM 2 // 3 songs + 3 RFID cards, change it as your need

`MFRC522 rfid(SS_PIN, RST_PIN);

byte RFID_UIDs[SONG_NUM][4] = { { 0x8C, 0x91, 0x08, 0xCD }, // UPDATE THIS VALUE FROM PREPARATION STEP
{ 0x03, 0x8A, 0x7B, 0xB7 } // UPDATE THIS VALUE FROM PREPARATION STEP // ADD MORE IF NEEDED
`};

void setup() { Serial.begin(9600);
Serial2.begin(9600); delay(500); // wait chip initialization is complete

mp3_command(CMD_SEL_DEV, DEV_TF); // select the TF card delay(200); // wait for 200ms

SPI.begin(); // init SPI bus rfid.PCD_Init(); // init MFRC522

Serial.println("Tap RFID Tag on reader");}

void loop() { if (rfid.PICC_IsNewCardPresent()) { // new tag is available
if (rfid.PICC_ReadCardSerial()) { // NUID has been readed Serial.print("Tag UID:");
for (int i = 0; i < rfid.uid.size; i++) { Serial.print(rfid.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(rfid.uid.uidByte[i], HEX); }
` Serial.println();

for (int index = 0; index < SONG_NUM; index++) { if (rfid.uid.uidByte[0] == RFID_UIDs[index][0] && rfid.uid.uidByte[1] == RFID_UIDs[index][1] && rfid.uid.uidByte[2] == RFID_UIDs[index][2] && rfid.uid.uidByte[3] == RFID_UIDs[index][3]) {
Serial.print("Playing song "); Serial.println(index);
mp3_command(CMD_PLAY_W_INDEX, index); // Play mp3 }
` }

rfid.PICC_HaltA(); // halt PICC rfid.PCD_StopCrypto1(); // stop encryption on PCD
} }
`}

void mp3_command(int8_t command, int16_t dat) { int8_t frame[8] = { 0 };
frame[0] = 0x7e; // starting byte frame[1] = 0xff; // version
frame[2] = 0x06; // the number of bytes of the command without starting byteand ending byte
frame[3] = command; // frame[4] = 0x00; // 0x00 = no feedback, 0x01 = feedback
frame[5] = (int8_t)(dat >> 8); // data high byte frame[6] = (int8_t)(dat); // data low byte
frame[7] = 0xef; // ending byte for (uint8_t i = 0; i < 8; i++) {
Serial2.write(frame[i]); }
`}
</>
I am having no luck with reading the tag and playing the mp3.

The RFID-RC522 reads the tag and runs the code, output;
Tag UID: 8C 91 08 CD
Playing song 0
Tag UID: 03 8A 7B B7
Playing song 1

However the file doesn't play. I did test the speakers and SD card separately so they are not the fault.

The only difference to the tutorial is that the speaker does not power the set-up, I had to use a USB cable from the ESP32.

Be grateful for any feedback.

Sorry, since you inserted the code incorrectly, all the lines of code, comments and your message are mixed up. It makes the reading of the code almost impossible.
Please consult with the forum guidelines how to insert the code, than post the code again using code tags.

You might want to look at this How to get the best out of this forum before you proceed any further.
We only know what you tell us, and without knowing what you have, we don't stand a chance.

As a beginner then please read about restrictions on new posters:-

and see what you need to do before you are trusted to post pictures.

Thank you, I thought I did the correct tags.

is this better:

```cpp #include #include

#define CMD_PLAY_NEXT 0x01
#define CMD_PLAY_PREV 0x02
#define CMD_PLAY_W_INDEX 0x03
#define CMD_SET_VOLUME 0x06
#define CMD_SEL_DEV 0x09
#define CMD_PLAY_W_VOL 0x22
#define CMD_PLAY 0x0D
#define CMD_PAUSE 0x0E
#define CMD_SINGLE_CYCLE 0x19

#define DEV_TF 0x02
#define SINGLE_CYCLE_ON 0x00
#define SINGLE_CYCLE_OFF 0x01

#define SS_PIN 5 // ESP32 pin GPIO5 connected to the SS of the RFID reader
#define RST_PIN 27 // ESP32 pin GPIO27 connected to the RST of the RFID reader

#define SONG_NUM 2 // 3 songs + 3 RFID cards, change it as your need

MFRC522 rfid(SS_PIN, RST_PIN);

byte RFID_UIDs[SONG_NUM][4] = {
{ 0x8C, 0x91, 0x08, 0xCD }, // UPDATE THIS VALUE FROM PREPARATION STEP
{ 0x03, 0x8A, 0x7B, 0xB7 } // UPDATE THIS VALUE FROM PREPARATION STEP
// ADD MORE IF NEEDED
};

void setup() {
Serial.begin(9600);
Serial2.begin(9600);
delay(500); // wait chip initialization is complete

mp3_command(CMD_SEL_DEV, DEV_TF); // select the TF card
delay(200); // wait for 200ms

SPI.begin(); // init SPI bus
rfid.PCD_Init(); // init MFRC522

Serial.println("Tap RFID Tag on reader");
}

void loop() {
if (rfid.PICC_IsNewCardPresent()) { // new tag is available
if (rfid.PICC_ReadCardSerial()) { // NUID has been readed
Serial.print("Tag UID:");
for (int i = 0; i < rfid.uid.size; i++) {
Serial.print(rfid.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(rfid.uid.uidByte[i], HEX);
}
Serial.println();

  for (int index = 0; index < SONG_NUM; index++) {
    if (rfid.uid.uidByte[0] == RFID_UIDs[index][0] && rfid.uid.uidByte[1] == RFID_UIDs[index][1] && rfid.uid.uidByte[2] == RFID_UIDs[index][2] && rfid.uid.uidByte[3] == RFID_UIDs[index][3]) {
      Serial.print("Playing song ");
      Serial.println(index);
      mp3_command(CMD_PLAY_W_INDEX, index);  // Play mp3
    }
  }

  rfid.PICC_HaltA();       // halt PICC
  rfid.PCD_StopCrypto1();  // stop encryption on PCD
}

}
}

void mp3_command(int8_t command, int16_t dat) {
int8_t frame[8] = { 0 };
frame[0] = 0x7e; // starting byte
frame[1] = 0xff; // version
frame[2] = 0x06; // the number of bytes of the command without starting byte and ending byte
frame[3] = command; //
frame[4] = 0x00; // 0x00 = no feedback, 0x01 = feedback
frame[5] = (int8_t)(dat >> 8); // data high byte
frame[6] = (int8_t)(dat); // data low byte
frame[7] = 0xef; // ending byte
for (uint8_t i = 0; i < 8; i++) {
Serial2.write(frame[i]);
}
}

</>

Sorry but no.
They still look wrong as if you are not using the correct key.
Can you post a picture of your keyboard please?

The three back ticks must be on a line all by itself at both the start and end of the code.

However, you code seems littered with what looks like a single back tick. I have removed these with a text editor so I think this might work:-

#include <SPI.h> #include <MFRC522.h>

#define CMD_PLAY_NEXT 0x01 #define CMD_PLAY_PREV 0x02
#define CMD_PLAY_W_INDEX 0x03 #define CMD_SET_VOLUME 0x06
#define CMD_SEL_DEV 0x09 #define CMD_PLAY_W_VOL 0x22
#define CMD_PLAY 0x0D #define CMD_PAUSE 0x0E
 #define CMD_SINGLE_CYCLE 0x19

#define DEV_TF 0x02 #define SINGLE_CYCLE_ON 0x00
 #define SINGLE_CYCLE_OFF 0x01

#define SS_PIN 5 // ESP32 pin GPIO5 connected to the SS of the RFID reader #define RST_PIN 27 // ESP32 pin GPIO27 connected to the RST of the RFID reader

 #define SONG_NUM 2 // 3 songs + 3 RFID cards, change it as your need

 MFRC522 rfid(SS_PIN, RST_PIN);

byte RFID_UIDs[SONG_NUM][4] = { { 0x8C, 0x91, 0x08, 0xCD }, // UPDATE THIS VALUE FROM PREPARATION STEP
{ 0x03, 0x8A, 0x7B, 0xB7 } // UPDATE THIS VALUE FROM PREPARATION STEP // ADD MORE IF NEEDED
 };

void setup() { Serial.begin(9600);
Serial2.begin(9600); delay(500); // wait chip initialization is complete

mp3_command(CMD_SEL_DEV, DEV_TF); // select the TF card delay(200); // wait for 200ms

SPI.begin(); // init SPI bus rfid.PCD_Init(); // init MFRC522

Serial.println("Tap RFID Tag on reader");}

void loop() { if (rfid.PICC_IsNewCardPresent()) { // new tag is available
if (rfid.PICC_ReadCardSerial()) { // NUID has been readed Serial.print("Tag UID:");
for (int i = 0; i < rfid.uid.size; i++) { Serial.print(rfid.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(rfid.uid.uidByte[i], HEX); }
  Serial.println();

for (int index = 0; index < SONG_NUM; index++) { if (rfid.uid.uidByte[0] == RFID_UIDs[index][0] && rfid.uid.uidByte[1] == RFID_UIDs[index][1] && rfid.uid.uidByte[2] == RFID_UIDs[index][2] && rfid.uid.uidByte[3] == RFID_UIDs[index][3]) {
Serial.print("Playing song "); Serial.println(index);
mp3_command(CMD_PLAY_W_INDEX, index); // Play mp3 }
  }

rfid.PICC_HaltA(); // halt PICC rfid.PCD_StopCrypto1(); // stop encryption on PCD
} }
 }

void mp3_command(int8_t command, int16_t dat) { int8_t frame[8] = { 0 };
frame[0] = 0x7e; // starting byte frame[1] = 0xff; // version
frame[2] = 0x06; // the number of bytes of the command without starting byteand ending byte
frame[3] = command; // frame[4] = 0x00; // 0x00 = no feedback, 0x01 = feedback
frame[5] = (int8_t)(dat >> 8); // data high byte frame[6] = (int8_t)(dat); // data low byte
frame[7] = 0xef; // ending byte for (uint8_t i = 0; i < 8; i++) {
Serial2.write(frame[i]); }
 }

Note you will not see the three back ticks but they look like this "```". I added the " " to stop the forum software thinking they are back ticks.

1 Like

Step by Step tutorial with pictures how to post code as a so called code-section.

Hi @milba1164

welcome to the arduino-forum.

the next lines that you posted say

Which sounds contradictionary to "having no luck reading the RFID"
an RFID-numbers gets printed to the serial monitor

good start for testing.
If the code runs and prints to the serial monitor
it is more likely that this is a hardware-bug.

You tested speaker separately result: speakers emits sound
You tested the SD-card separately result: SD-card contains what?
valid MP3-files?

For further analysing post a picture of your hardware-setup. This requires to reach trust-level 1 in the forum
To post images etc. you need trust level 1, you can get there by:

  • Entering at least 5 topics
  • Reading at least 30 posts
  • Spend a total of 10 minutes reading posts

For analysing what is wrong much more detail-information is needed.

I'm pretty sure that you agree and will follow the way how to solve your problem mimimum 200 minutes faster.
This requires to invest 20 minutes of your precious time to read how to speedup solving your problems.

Directly after registering you got presented informations how to speed up solving your problem.
You should really read it.

After that you know what additional informations to provide.

best regards Stefan

1 Like

All the points you raise in the whole of this post I covered in my Post #3

Just saying.

In addition I would like to say to @milba1164
RFID readers that are in close proximity to each other interfere with each other and stop them working, unless you make provisions for it. This involves designing a special RFID reader, not using the RC522 reader board.

I did this with this project, but it is a lot of work.

Also where did you get your code from? Did you write it yourself?

Thanks for taking the time to help me get up to speed on the forum. I have never used a forum before, clearly it shows :stuck_out_tongue:

The code is from tutorial https://esp32io.com/tutorials/esp32-rfid-mp3-player.

I think I will do as you suggested and start a new thread so it is more clear :slight_smile:

No.
This is where it ought to be, ought to stay.

ah ha! I dont write i click on the function :stuck_out_tongue:
The files on the player are mp3, labelled [0] and [1]

#include <SPI.h>
#include <MFRC522.h>

#define CMD_PLAY_NEXT 0x01
#define CMD_PLAY_PREV 0x02
#define CMD_PLAY_W_INDEX 0x03
#define CMD_SET_VOLUME 0x06
#define CMD_SEL_DEV 0x09
#define CMD_PLAY_W_VOL 0x22
#define CMD_PLAY 0x0D
#define CMD_PAUSE 0x0E
#define CMD_SINGLE_CYCLE 0x19

#define DEV_TF 0x02
#define SINGLE_CYCLE_ON 0x00
#define SINGLE_CYCLE_OFF 0x01

#define SS_PIN 5    // ESP32 pin GPIO5 connected to the SS of the RFID reader
#define RST_PIN 27  // ESP32 pin GPIO27 connected to the RST of the RFID reader

#define SONG_NUM 2  // 3 songs + 3 RFID cards, change it as your need

MFRC522 rfid(SS_PIN, RST_PIN);

byte RFID_UIDs[SONG_NUM][4] = {
  { 0x8C, 0x91, 0x08, 0xCD },  // UPDATE THIS VALUE FROM PREPARATION STEP
  { 0x03, 0x8A, 0x7B, 0xB7 }   // UPDATE THIS VALUE FROM PREPARATION STEP
                               // ADD MORE IF NEEDED
};

void setup() {
  Serial.begin(9600);
  Serial2.begin(9600);
  delay(500);  // wait chip initialization is complete

  mp3_command(CMD_SEL_DEV, DEV_TF);  // select the TF card
  delay(200);                        // wait for 200ms

  SPI.begin();      // init SPI bus
  rfid.PCD_Init();  // init MFRC522

  Serial.println("Tap RFID Tag on reader");
}

void loop() {
  if (rfid.PICC_IsNewCardPresent()) {  // new tag is available
    if (rfid.PICC_ReadCardSerial()) {  // NUID has been readed
      Serial.print("Tag UID:");
      for (int i = 0; i < rfid.uid.size; i++) {
        Serial.print(rfid.uid.uidByte[i] < 0x10 ? " 0" : " ");
        Serial.print(rfid.uid.uidByte[i], HEX);
      }
      Serial.println();

      for (int index = 0; index < SONG_NUM; index++) {
        if (rfid.uid.uidByte[0] == RFID_UIDs[index][0] && rfid.uid.uidByte[1] == RFID_UIDs[index][1] && rfid.uid.uidByte[2] == RFID_UIDs[index][2] && rfid.uid.uidByte[3] == RFID_UIDs[index][3]) {
          Serial.print("Playing song ");
          Serial.println(index);
          mp3_command(CMD_PLAY_W_INDEX, index);  // Play mp3
        }
      }

      rfid.PICC_HaltA();       // halt PICC
      rfid.PCD_StopCrypto1();  // stop encryption on PCD
    }
  }
}

void mp3_command(int8_t command, int16_t dat) {
  int8_t frame[8] = { 0 };
  frame[0] = 0x7e;                // starting byte
  frame[1] = 0xff;                // version
  frame[2] = 0x06;                // the number of bytes of the command without starting byte and ending byte
  frame[3] = command;             //
  frame[4] = 0x00;                // 0x00 = no feedback, 0x01 = feedback
  frame[5] = (int8_t)(dat >> 8);  // data high byte
  frame[6] = (int8_t)(dat);       // data low byte
  frame[7] = 0xef;                // ending byte
  for (uint8_t i = 0; i < 8; i++) {
    Serial2.write(frame[i]);
  }
}

Where did I ever say this?

What does that mean?

sorry my misinterpretation, as I said I am new to forums and as such ignore the second comment as I don't know how to articulate that and its not important.

Do you know you are permitted to answer more than one question in a reply? This is something I would have thought as being self evident, but seeing the number of people that only answer one point, no matter how many questions are asked I am beginning to wonder if it is self evident?

Or this is a generational thing with the short attention span that the young seem to have developed.

LOL I am hardly 'young', I am just new to the platform. And I did answer both questions

  1. sorry my misinterpretation, as I said I am new to forums and as such
  2. ignore the second comment as I don't know how to articulate that and its not important.

so now as I learn I am happy to move along and back to the problem at hand.
:slight_smile:

Was the unanswered question.

Now to the nub of the question.
You posted a link to the project you were trying to do.

Now then I go there I find it a site that requires a whole bunch of cookies, with many layers of ambiguous rubbish preventing me from preventing me to turn them off. They all claim to be essential or at least legitimate interest. I and many other members try to minimise the number of cookies invading my machine. So that means that you need to show the information and post it in the correct way for this forum. That means transferring the information to your machine first, and then posting it as if it were your own.

Then maybe we could make some progress.

1 Like

RFIDmp3_instructions.pdf (1.3 MB)

is this readable? snippings from the original tutorial.

Yes I can read it but it is not very useful.

First of all there are no labels on that physical layout diagram, and the note says always connect the labels not the positions. We need to see a schematic, not a physical layout diagram.

Are the components exactly like the kindergarten pictures you posted?

All the code is in the form of pictures. So we can't see all the code, and we can not run pictures on our own machine.

There is an example code that test just the RFID reading part did that work? Can you post the results of this please.

We also need to see your setup, not the tutorials childish pictures, so can you try and post those. In fact the pdf proves you can post your own pictures.

It would also help if you posted a link to where you bought the ESP32 processor and the MP3 module from.

Can you tell me what this means please as it doesn't make much sense to me?

Yes exactly like the kindergarden pic. I bought the compents from Jaycar and Amazon.

the testing code to read fob showed fob ID as

I wanted to also do a screen shot of the code output saying "playing song 1" ect but since I did the updates for Arduino 2.3.3 its flagging an error with 'mp3_command'

The actual code in full is:

#include <SPI.h>
#include <MFRC522.h>

#define CMD_PLAY_NEXT 0x01
#define CMD_PLAY_PREV 0x02
#define CMD_PLAY_W_INDEX 0x03
#define CMD_SET_VOLUME 0x06
#define CMD_SEL_DEV 0x09
#define CMD_PLAY_W_VOL 0x22
#define CMD_PLAY 0x0D
#define CMD_PAUSE 0x0E
#define CMD_SINGLE_CYCLE 0x19

#define DEV_TF 0x02
#define SINGLE_CYCLE_ON 0x00
#define SINGLE_CYCLE_OFF 0x01

#define SS_PIN 5    // ESP32 pin GPIO5 connected to the SS of the RFID reader
#define RST_PIN 27  // ESP32 pin GPIO27 connected to the RST of the RFID reader

#define SONG_NUM 2  // 3 songs + 3 RFID cards, change it as your need

MFRC522 rfid(SS_PIN, RST_PIN);

byte RFID_UIDs[SONG_NUM][4] = {
  { 0x8C, 0x91, 0x08, 0xCD },  // UPDATE THIS VALUE FROM PREPARATION STEP
  { 0x03, 0x8A, 0x7B, 0xB7 }   // UPDATE THIS VALUE FROM PREPARATION STEP
                               // ADD MORE IF NEEDED
};

void setup() {
  Serial.begin(9600);
  Serial2.begin(9600);
  delay(500);  // wait chip initialization is complete

  mp3_command(CMD_SEL_DEV, DEV_TF);  // select the TF card
  delay(200);                        // wait for 200ms

  SPI.begin();      // init SPI bus
  rfid.PCD_Init();  // init MFRC522

  Serial.println("Tap RFID Tag on reader");

void loop() {
  if (rfid.PICC_IsNewCardPresent()) {  // new tag is available
    if (rfid.PICC_ReadCardSerial()) {  // NUID has been readed
      Serial.print("Tag UID:");
      for (int i = 0; i < rfid.uid.size; i++) {
        Serial.print(rfid.uid.uidByte[i] < 0x10 ? " 0" : " ");
        Serial.print(rfid.uid.uidByte[i], HEX);
      }
      Serial.println();

      for (int index = 0; index < SONG_NUM; index++) {
        if (rfid.uid.uidByte[0] == RFID_UIDs[index][0] && rfid.uid.uidByte[1] == RFID_UIDs[index][1] && rfid.uid.uidByte[2] == RFID_UIDs[index][2] && rfid.uid.uidByte[3] == RFID_UIDs[index][3]) {
          Serial.print("Playing song ");
          Serial.println(index);
          mp3_command(CMD_PLAY_W_INDEX, index);  // Play mp3
        }
      }

      rfid.PICC_HaltA();       // halt PICC
      rfid.PCD_StopCrypto1();  // stop encryption on PCD
    }
  }
}

void mp3_command(int8_t command, int16_t dat) {
  int8_t frame[8] = { 0 };
  frame[0] = 0x7e;                // starting byte
  frame[1] = 0xff;                // version
  frame[2] = 0x06;                // the number of bytes of the command without starting byte and ending byte
  frame[3] = command;             //
  frame[4] = 0x00;                // 0x00 = no feedback, 0x01 = feedback
  frame[5] = (int8_t)(dat >> 8);  // data high byte
  frame[6] = (int8_t)(dat);       // data low byte
  frame[7] = 0xef;                // ending byte
  for (uint8_t i = 0; i < 8; i++) {
    Serial2.write(frame[i]);
  }
}

so now I have to backtrack to work out why its throwing up the mp3_command error before I can move forward again.
sorry and thanks for being so patient.

the old turn off and turn back on :stuck_out_tongue:
here is fob scan results, however no sound is heard through headphones or speakers.