Hello, I am trying to migrate a project that I had with an old arduino board to esp32, but it is not working for me; I test with an esp32 connection example with dfplayermini, but it never connects to serial. I really don't know what I'm doing wrong with the ESP32 board.
My sample code, for tray connection ESP32 with DFPlayermini
#include <Arduino.h>
//#include <SoftwareSerial.h>
//#include "AltSoftSerial.h"
#include "DFRobotDFPlayerMini.h"
//SoftwareSerial mySoftwareSerial(16, 17); // RX, TX
//AltSoftSerial mySoftwareSerial(16, 17); // RX, TX
HardwareSerial mySoftwareSerial(1);
DFRobotDFPlayerMini myDFPlayer;
void printDetail(uint8_t type, int value);
void setup()
{
mySoftwareSerial.begin(9600, SERIAL_8N1, 16, 17); // speed, type, RX, TX
Serial.begin(115200);
Serial.println();
Serial.println(F("DFRobot DFPlayer Mini Demo"));
Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));
if (!myDFPlayer.begin(mySoftwareSerial)) { //Use softwareSerial to communicate with mp3.
Serial.println(myDFPlayer.readType(),HEX);
Serial.println(F("Unable to begin:"));
Serial.println(F("1.Please recheck the connection!"));
Serial.println(F("2.Please insert the SD card!"));
while(true);
}
Serial.println(F("DFPlayer Mini online."));
myDFPlayer.setTimeOut(500); //Set serial communictaion time out 500ms
//----Set volume----
myDFPlayer.volume(10); //Set volume value (0~30).
myDFPlayer.volumeUp(); //Volume Up
myDFPlayer.volumeDown(); //Volume Down
//----Set different EQ----
myDFPlayer.EQ(DFPLAYER_EQ_NORMAL);
// myDFPlayer.EQ(DFPLAYER_EQ_POP);
// myDFPlayer.EQ(DFPLAYER_EQ_ROCK);
// myDFPlayer.EQ(DFPLAYER_EQ_JAZZ);
// myDFPlayer.EQ(DFPLAYER_EQ_CLASSIC);
// myDFPlayer.EQ(DFPLAYER_EQ_BASS);
//----Set device we use SD as default----
// myDFPlayer.outputDevice(DFPLAYER_DEVICE_U_DISK);
myDFPlayer.outputDevice(DFPLAYER_DEVICE_SD);
// myDFPlayer.outputDevice(DFPLAYER_DEVICE_AUX);
// myDFPlayer.outputDevice(DFPLAYER_DEVICE_SLEEP);
// myDFPlayer.outputDevice(DFPLAYER_DEVICE_FLASH);
//----Mp3 control----
// myDFPlayer.sleep(); //sleep
// myDFPlayer.reset(); //Reset the module
// myDFPlayer.enableDAC(); //Enable On-chip DAC
// myDFPlayer.disableDAC(); //Disable On-chip DAC
// myDFPlayer.outputSetting(true, 15); //output setting, enable the output and set the gain to 15
int delayms=100;
//----Mp3 play----
/*
Serial.println(F("myDFPlayer.next()"));
myDFPlayer.next(); //Play next mp3
delay(delayms);
Serial.println(F("myDFPlayer.previous()"));
myDFPlayer.previous(); //Play previous mp3
delay(delayms);
Serial.println(F("myDFPlayer.play(1)"));
myDFPlayer.play(1); //Play the first mp3
delay(delayms);
Serial.println(F("myDFPlayer.loop(1)"));
myDFPlayer.loop(1); //Loop the first mp3
delay(delayms);
Serial.println(F("myDFPlayer.pause()"));
myDFPlayer.pause(); //pause the mp3
delay(delayms);
Serial.println(F("myDFPlayer.start()"));
myDFPlayer.start(); //start the mp3 from the pause
delay(delayms);
Serial.println(F("myDFPlayer.playFolder(15, 4)"));
myDFPlayer.playFolder(15, 4); //play specific mp3 in SD:/15/004.mp3; Folder Name(1~99); File Name(1~255)
delay(delayms);
Serial.println(F("myDFPlayer.enableLoopAll()"));
myDFPlayer.enableLoopAll(); //loop all mp3 files.
delay(delayms);
Serial.println(F("myDFPlayer.disableLoopAll()"));
myDFPlayer.disableLoopAll(); //stop loop all mp3 files.
delay(delayms);
Serial.println(F("myDFPlayer.playMp3Folder(4)"));
myDFPlayer.playMp3Folder(4); //play specific mp3 in SD:/MP3/0004.mp3; File Name(0~65535)
delay(delayms);
Serial.println(F("myDFPlayer.advertise(3)"));
myDFPlayer.advertise(3); //advertise specific mp3 in SD:/ADVERT/0003.mp3; File Name(0~65535)
delay(delayms);
Serial.println(F("myDFPlayer.stopAdvertise()"));
myDFPlayer.stopAdvertise(); //stop advertise
delay(delayms);
Serial.println(F("myDFPlayer.playLargeFolder(2,999)"));
myDFPlayer.playLargeFolder(2, 999); //play specific mp3 in SD:/02/004.mp3; Folder Name(1~10); File Name(1~1000)
delay(delayms);
Serial.println(F("myDFPlayer.loopFolder(5)"));
myDFPlayer.loopFolder(5); //loop all mp3 files in folder SD:/05.
delay(delayms);
Serial.println(F("myDFPlayer.randomAll()"));
myDFPlayer.randomAll(); //Random play all the mp3.
delay(delayms);
Serial.println(F("myDFPlayer.enableLoop()"));
myDFPlayer.enableLoop(); //enable loop.
delay(delayms);
Serial.println(F("myDFPlayer.disableLoop()"));
myDFPlayer.disableLoop(); //disable loop.
delay(delayms);
*/
//----Read imformation----
Serial.println(F("readState--------------------"));
Serial.println(myDFPlayer.readState()); //read mp3 state
Serial.println(F("readVolume--------------------"));
Serial.println(myDFPlayer.readVolume()); //read current volume
//Serial.println(F("readEQ--------------------"));
//Serial.println(myDFPlayer.readEQ()); //read EQ setting
Serial.println(F("readFileCounts--------------------"));
Serial.println(myDFPlayer.readFileCounts()); //read all file counts in SD card
Serial.println(F("readCurrentFileNumber--------------------"));
Serial.println(myDFPlayer.readCurrentFileNumber()); //read current play file number
Serial.println(F("readFileCountsInFolder--------------------"));
Serial.println(myDFPlayer.readFileCountsInFolder(3)); //read fill counts in folder SD:/03
Serial.println(F("--------------------"));
Serial.println(F("myDFPlayer.play(1)"));
myDFPlayer.play(1); //Play the first mp3
}
void loop()
{
// static unsigned long timer = millis();
if (Serial.available()) {
String inData = "";
inData = Serial.readStringUntil('\n');
if (inData.startsWith("n")) {
Serial.println(F("next--------------------"));
myDFPlayer.next();
Serial.println(myDFPlayer.readCurrentFileNumber()); //read current play file number
} else if (inData.startsWith("p")) {
Serial.println(F("previous--------------------"));
myDFPlayer.previous();
Serial.println(myDFPlayer.readCurrentFileNumber()); //read current play file number
} else if (inData.startsWith("+")) {
Serial.println(F("up--------------------"));
myDFPlayer.volumeUp();
Serial.println(myDFPlayer.readVolume()); //read current volume
} else if (inData.startsWith("-")) {
Serial.println(F("down--------------------"));
myDFPlayer.volumeDown();
Serial.println(myDFPlayer.readVolume()); //read current volume
} else if (inData.startsWith("*")) {
Serial.println(F("pause--------------------"));
myDFPlayer.pause();
} else if (inData.startsWith(">")) {
Serial.println(F("start--------------------"));
myDFPlayer.start();
}
}
if (myDFPlayer.available()) {
if (myDFPlayer.readType()==DFPlayerPlayFinished) {
Serial.println(myDFPlayer.read());
Serial.println(F("next--------------------"));
myDFPlayer.next(); //Play next mp3 every 3 second.
Serial.println(F("readCurrentFileNumber--------------------"));
Serial.println(myDFPlayer.readCurrentFileNumber()); //read current play file number
delay(500);
}
}
// if (myDFPlayer.available()) {
// printDetail(myDFPlayer.readType(), myDFPlayer.read()); //Print the detail message from DFPlayer to handle different errors and states.
// }
}
void printDetail(uint8_t type, int value){
switch (type) {
case TimeOut:
Serial.println(F("Time Out!"));
break;
case WrongStack:
Serial.println(F("Stack Wrong!"));
break;
case DFPlayerCardInserted:
Serial.println(F("Card Inserted!"));
break;
case DFPlayerCardRemoved:
Serial.println(F("Card Removed!"));
break;
case DFPlayerCardOnline:
Serial.println(F("Card Online!"));
break;
case DFPlayerPlayFinished:
Serial.print(F("Number:"));
Serial.print(value);
Serial.println(F(" Play Finished!"));
break;
case DFPlayerError:
Serial.print(F("DFPlayerError:"));
switch (value) {
case Busy:
Serial.println(F("Card not found"));
break;
case Sleeping:
Serial.println(F("Sleeping"));
break;
case SerialWrongStack:
Serial.println(F("Get Wrong Stack"));
break;
case CheckSumNotMatch:
Serial.println(F("Check Sum Not Match"));
break;
case FileIndexOut:
Serial.println(F("File Index Out of Bound"));
break;
case FileMismatch:
Serial.println(F("Cannot Find File"));
break;
case Advertise:
Serial.println(F("In Advertise"));
break;
default:
break;
}
break;
default:
break;
}
}
And for last, this is the error that show me the serial monitor:
DFRobot DFPlayer Mini Demo
Initializing DFPlayer ... (May take 3~5 seconds)
0
Unable to begin:
1.Please recheck the connection!
2.Please insert the SD card!
I have also been trying to get this to work with an esp 32. I have ran examples from multiple libraries. they all seem to be having issues communicating. I ran the code you have with the suggested changes, and I got this error
exit status 1
invalid conversion from 'int' to 'SoftwareSerialConfig' [-fpermissive]
Thanks for the response. I got the code from this thread. I have been using the dfrovot library in arduino IDE. Examples as well. I will look into one of those testers it looks useful. I can try the dfplayer on a arduino nano tonight. I am not good with the serial communication on the microcontrollers. I’m trying to understand the differences.
I could post the code but I just copied and pasted the code from the first post and made the changes in post #2 by runaway pancake. Actually, I will try to post it. I don't understand the error. unless I forgot to comment something out. Also I could not find an Arduino board that I haven't already burnt up, so I didn't get to test it on another board. I did order the cable from post 5. Hopefully I will be able to figure out how it works.
#include <Arduino.h>
#include <SoftwareSerial.h>
//#include "AltSoftSerial.h"
#include "DFRobotDFPlayerMini.h"
SoftwareSerial mySoftwareSerial(16, 17); // RX, TX
//AltSoftSerial mySoftwareSerial(16, 17); // RX, TX
//HardwareSerial mySoftwareSerial(1);
DFRobotDFPlayerMini myDFPlayer;
void printDetail(uint8_t type, int value);
void setup()
{
mySoftwareSerial.begin(9600, SERIAL_8N1, 16, 17); // speed, type, RX, TX
Serial.begin(115200);
Serial.println();
Serial.println(F("DFRobot DFPlayer Mini Demo"));
Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));
if (!myDFPlayer.begin(mySoftwareSerial)) { //Use softwareSerial to communicate with mp3.
Serial.println(myDFPlayer.readType(),HEX);
Serial.println(F("Unable to begin:"));
Serial.println(F("1.Please recheck the connection!"));
Serial.println(F("2.Please insert the SD card!"));
while(true);
}
Serial.println(F("DFPlayer Mini online."));
myDFPlayer.setTimeOut(500); //Set serial communictaion time out 500ms
//----Set volume----
myDFPlayer.volume(10); //Set volume value (0~30).
myDFPlayer.volumeUp(); //Volume Up
myDFPlayer.volumeDown(); //Volume Down
//----Set different EQ----
myDFPlayer.EQ(DFPLAYER_EQ_NORMAL);
// myDFPlayer.EQ(DFPLAYER_EQ_POP);
// myDFPlayer.EQ(DFPLAYER_EQ_ROCK);
// myDFPlayer.EQ(DFPLAYER_EQ_JAZZ);
// myDFPlayer.EQ(DFPLAYER_EQ_CLASSIC);
// myDFPlayer.EQ(DFPLAYER_EQ_BASS);
//----Set device we use SD as default----
// myDFPlayer.outputDevice(DFPLAYER_DEVICE_U_DISK);
myDFPlayer.outputDevice(DFPLAYER_DEVICE_SD);
// myDFPlayer.outputDevice(DFPLAYER_DEVICE_AUX);
// myDFPlayer.outputDevice(DFPLAYER_DEVICE_SLEEP);
// myDFPlayer.outputDevice(DFPLAYER_DEVICE_FLASH);
//----Mp3 control----
// myDFPlayer.sleep(); //sleep
// myDFPlayer.reset(); //Reset the module
// myDFPlayer.enableDAC(); //Enable On-chip DAC
// myDFPlayer.disableDAC(); //Disable On-chip DAC
// myDFPlayer.outputSetting(true, 15); //output setting, enable the output and set the gain to 15
int delayms=100;
//----Mp3 play----
/*
Serial.println(F("myDFPlayer.next()"));
myDFPlayer.next(); //Play next mp3
delay(delayms);
Serial.println(F("myDFPlayer.previous()"));
myDFPlayer.previous(); //Play previous mp3
delay(delayms);
Serial.println(F("myDFPlayer.play(1)"));
myDFPlayer.play(1); //Play the first mp3
delay(delayms);
Serial.println(F("myDFPlayer.loop(1)"));
myDFPlayer.loop(1); //Loop the first mp3
delay(delayms);
Serial.println(F("myDFPlayer.pause()"));
myDFPlayer.pause(); //pause the mp3
delay(delayms);
Serial.println(F("myDFPlayer.start()"));
myDFPlayer.start(); //start the mp3 from the pause
delay(delayms);
Serial.println(F("myDFPlayer.playFolder(15, 4)"));
myDFPlayer.playFolder(15, 4); //play specific mp3 in SD:/15/004.mp3; Folder Name(1~99); File Name(1~255)
delay(delayms);
Serial.println(F("myDFPlayer.enableLoopAll()"));
myDFPlayer.enableLoopAll(); //loop all mp3 files.
delay(delayms);
Serial.println(F("myDFPlayer.disableLoopAll()"));
myDFPlayer.disableLoopAll(); //stop loop all mp3 files.
delay(delayms);
Serial.println(F("myDFPlayer.playMp3Folder(4)"));
myDFPlayer.playMp3Folder(4); //play specific mp3 in SD:/MP3/0004.mp3; File Name(0~65535)
delay(delayms);
Serial.println(F("myDFPlayer.advertise(3)"));
myDFPlayer.advertise(3); //advertise specific mp3 in SD:/ADVERT/0003.mp3; File Name(0~65535)
delay(delayms);
Serial.println(F("myDFPlayer.stopAdvertise()"));
myDFPlayer.stopAdvertise(); //stop advertise
delay(delayms);
Serial.println(F("myDFPlayer.playLargeFolder(2,999)"));
myDFPlayer.playLargeFolder(2, 999); //play specific mp3 in SD:/02/004.mp3; Folder Name(1~10); File Name(1~1000)
delay(delayms);
Serial.println(F("myDFPlayer.loopFolder(5)"));
myDFPlayer.loopFolder(5); //loop all mp3 files in folder SD:/05.
delay(delayms);
Serial.println(F("myDFPlayer.randomAll()"));
myDFPlayer.randomAll(); //Random play all the mp3.
delay(delayms);
Serial.println(F("myDFPlayer.enableLoop()"));
myDFPlayer.enableLoop(); //enable loop.
delay(delayms);
Serial.println(F("myDFPlayer.disableLoop()"));
myDFPlayer.disableLoop(); //disable loop.
delay(delayms);
*/
//----Read imformation----
Serial.println(F("readState--------------------"));
Serial.println(myDFPlayer.readState()); //read mp3 state
Serial.println(F("readVolume--------------------"));
Serial.println(myDFPlayer.readVolume()); //read current volume
//Serial.println(F("readEQ--------------------"));
//Serial.println(myDFPlayer.readEQ()); //read EQ setting
Serial.println(F("readFileCounts--------------------"));
Serial.println(myDFPlayer.readFileCounts()); //read all file counts in SD card
Serial.println(F("readCurrentFileNumber--------------------"));
Serial.println(myDFPlayer.readCurrentFileNumber()); //read current play file number
Serial.println(F("readFileCountsInFolder--------------------"));
Serial.println(myDFPlayer.readFileCountsInFolder(3)); //read fill counts in folder SD:/03
Serial.println(F("--------------------"));
Serial.println(F("myDFPlayer.play(1)"));
myDFPlayer.play(1); //Play the first mp3
}
void loop()
{
// static unsigned long timer = millis();
if (Serial.available()) {
String inData = "";
inData = Serial.readStringUntil('\n');
if (inData.startsWith("n")) {
Serial.println(F("next--------------------"));
myDFPlayer.next();
Serial.println(myDFPlayer.readCurrentFileNumber()); //read current play file number
} else if (inData.startsWith("p")) {
Serial.println(F("previous--------------------"));
myDFPlayer.previous();
Serial.println(myDFPlayer.readCurrentFileNumber()); //read current play file number
} else if (inData.startsWith("+")) {
Serial.println(F("up--------------------"));
myDFPlayer.volumeUp();
Serial.println(myDFPlayer.readVolume()); //read current volume
} else if (inData.startsWith("-")) {
Serial.println(F("down--------------------"));
myDFPlayer.volumeDown();
Serial.println(myDFPlayer.readVolume()); //read current volume
} else if (inData.startsWith("*")) {
Serial.println(F("pause--------------------"));
myDFPlayer.pause();
} else if (inData.startsWith(">")) {
Serial.println(F("start--------------------"));
myDFPlayer.start();
}
}
if (myDFPlayer.available()) {
if (myDFPlayer.readType()==DFPlayerPlayFinished) {
Serial.println(myDFPlayer.read());
Serial.println(F("next--------------------"));
myDFPlayer.next(); //Play next mp3 every 3 second.
Serial.println(F("readCurrentFileNumber--------------------"));
Serial.println(myDFPlayer.readCurrentFileNumber()); //read current play file number
delay(500);
}
}
// if (myDFPlayer.available()) {
// printDetail(myDFPlayer.readType(), myDFPlayer.read()); //Print the detail message from DFPlayer to handle different errors and states.
// }
}
void printDetail(uint8_t type, int value){
switch (type) {
case TimeOut:
Serial.println(F("Time Out!"));
break;
case WrongStack:
Serial.println(F("Stack Wrong!"));
break;
case DFPlayerCardInserted:
Serial.println(F("Card Inserted!"));
break;
case DFPlayerCardRemoved:
Serial.println(F("Card Removed!"));
break;
case DFPlayerCardOnline:
Serial.println(F("Card Online!"));
break;
case DFPlayerPlayFinished:
Serial.print(F("Number:"));
Serial.print(value);
Serial.println(F(" Play Finished!"));
break;
case DFPlayerError:
Serial.print(F("DFPlayerError:"));
switch (value) {
case Busy:
Serial.println(F("Card not found"));
break;
case Sleeping:
Serial.println(F("Sleeping"));
break;
case SerialWrongStack:
Serial.println(F("Get Wrong Stack"));
break;
case CheckSumNotMatch:
Serial.println(F("Check Sum Not Match"));
break;
case FileIndexOut:
Serial.println(F("File Index Out of Bound"));
break;
case FileMismatch:
Serial.println(F("Cannot Find File"));
break;
case Advertise:
Serial.println(F("In Advertise"));
break;
default:
break;
}
break;
default:
break;
}
}
Hmmm. . .
Though there is SoftwareSerial for ESP8266
there is NOT for ESP32, because it has 3 UARTs (but the '8266 has 2 1/2, one is TX only). Anyway.
This compiles, but Untested (my ESP32 is in the wash, it's busy right now).
Your test mp3 is called 0001.mp3 (everything on the SD should be 001.mp3, 002.mp3, 0001.mp3, 0002.mp3, 0003.mp3 and so on)
It just starts playing 001.mp3, goes on for 2seconds and starts it over again (and again, . . .)
//#include "Arduino.h"
#include "HardwareSerial.h" // w/o this, get verbose nastygrams (orange)
#include "DFRobotDFPlayerMini.h"
// Use pins 2 and 3 to communicate with DFPlayer Mini
const byte RXD2 = 16; // Connects to module's RX
const byte TXD2 = 17; // Connects to module's TX
HardwareSerial dfSD(1); // Use UART channel 1
DFRobotDFPlayerMini player;
void setup()
{
Serial.begin(19200);
dfSD.begin(9600, SERIAL_8N1, RXD2, TXD2);
delay(2000);
if (player.begin(dfSD))
{
Serial.println("OK");
// Set volume to maximum (0 to 30).
player.volume(22); //30 is very loud
}
else
{
Serial.println("Connecting to DFPlayer Mini failed!");
}
}
void loop()
{
Serial.print("Playing #1 \t");
player.play(1);
Serial.println("play start");
delay(2000);
Serial.println("played 2 sec.");
}
Thank you for looking into that. I will look into serial communication for esp32 and see if I can find something. I ordered a pack of 5 DFplayers I will also try using a different one.
PE -
Located another ESP32, but not connected to a player module -
on TeraTerm I see the player INIT stream and then what are PLAY commands.
So, at this point, I'm 'fairly certain' that that should be 'working'.