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 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]);
}
`}
</>
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.