BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

ESP32 + BLE with DFPlayerMini to play specific MP3 audio files

Hello Everyone! :raising_hand_man:
I made this project ESP32 + BLE with DFPlayerMini to play specific MP3 audio files and this was inspired by this amazing project:- Audio / MP3 music: ESP8266 + DFRobot DFPlayer Mini + Music Player widget + Wifi (NodeMCU, Wemos D1, …) by @federicobusero.

About the project:-
This project helps you to play different MP3/WAV audio files stored in the DFPlayerMini by pressing Virtual buttons allocated for each audio. This Project uses BLE i.e. Bluetooth Low Energy which V4.0 of Bluetooth and works mainly on iOS devices. This project also had a volume control slider which helps to control volume from 0 - 30.

Things used:-

  1. ESP32 BLE
  2. DFPlayerMini
  3. Some Jumper wires
  4. Breadboard

code-


//This Project was made by Animesh Bhatt https://community.blynk.cc/u/myselfanimesh and posted on Blynk Community 

/*************************************************************
  Download latest Blynk library here:
    https://github.com/blynkkk/blynk-library/releases/latest

  Blynk is a platform with iOS and Android apps to control
  Arduino, Raspberry Pi and the likes over the Internet.
  You can easily build graphic interfaces for all your
  projects by simply dragging and dropping widgets.

    Downloads, docs, tutorials: http://www.blynk.cc
    Sketch generator:           http://examples.blynk.cc
    Blynk community:            http://community.blynk.cc
    Social networks:            http://www.fb.com/blynkapp
                                http://twitter.com/blynk_app

  Blynk library is licensed under MIT license
  This example code is in the public domain.

 *************************************************************
  This example shows how to use ESP32 BLE
  to connect your project to Blynk.

  Warning: Bluetooth support is in beta!
 *************************************************************/

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial

#define BLYNK_USE_DIRECT_CONNECT
#include <HardwareSerial.h>
HardwareSerial mySoftwareSerial(1);
#include <Arduino.h>
#include <BlynkSimpleEsp32_BLE.h>
#include <BLEDevice.h>
#include <BLEServer.h>
#include "DFPlay.h"
DFPlay dfPlay;
#include <DFRobotDFPlayerMini.h>
DFRobotDFPlayerMini myDFPlayer;

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "YourAuthToken";


BLYNK_CONNECTED(){
   Blynk.syncVirtual(V0);
  }

BLYNK_WRITE(V0)
{
  int paramVol = param.asInt();
 uint8_t volume = constrain(paramVol, 0, 30);
#ifdef BLYNK_PRINT
  BLYNK_PRINT.print("VOLUME: ");
  BLYNK_PRINT.println(paramVol);
#endif
  myDFPlayer.volume(volume);
}

BLYNK_WRITE(V1)
{ int i=param.asInt();

  if (i==1){
  
   Serial.print("V1 value is: ");
  Serial.println(i);
   myDFPlayer.play(1);
  }
  else{

  }
}

BLYNK_WRITE(V2)
{ int i=param.asInt();

  if (i==1){
  
   Serial.print("V2 value is: ");
  Serial.println(i);
   myDFPlayer.play(2);
  }
  else{

  }
}
BLYNK_WRITE(V3)
{ int i=param.asInt();

  if (i==1){
  
   Serial.print("V3 value is: ");
  Serial.println(i);
   myDFPlayer.play(3);
  }
  else{

  }
}

BLYNK_WRITE(V4)
{ int i=param.asInt();

  if (i==1){
  
   Serial.print("V4 value is: ");
  Serial.println(i);
   myDFPlayer.play(4);
  }
  else{

  }
}

BLYNK_WRITE(V5)
{ int i=param.asInt();

  if (i==1){
  
   Serial.print("V5 value is: ");
  Serial.println(i);
   myDFPlayer.play(5);
  }
  else{

  }
}

BLYNK_WRITE(V6)
{ int i=param.asInt();

  if (i==1){
  
   Serial.print("V6 value is: ");
  Serial.println(i);
   myDFPlayer.play(6);
  }
  else{

  }
}

BLYNK_WRITE(V7)
{ int i=param.asInt();

  if (i==1){
  
   Serial.print("V7 value is: ");
  Serial.println(i);
   myDFPlayer.play(7);
  }
  else{

  }
}

BLYNK_WRITE(V8)
{ int i=param.asInt();

  if (i==1){
  
   Serial.print("V8 value is: ");
  Serial.println(i);
   myDFPlayer.play(8);
  }
  else{

  }
}
BLYNK_WRITE(V9)
{ int i=param.asInt();

  if (i==1){
  
   Serial.print("V9 value is: ");
  Serial.println(i);
   myDFPlayer.play(10);
  }
  else{

  }
}

void setup()
{
  // Debug console
  Serial.begin(9600);
  Serial.println("Waiting for connections...");

  Blynk.setDeviceName("Blynk");

  Blynk.begin(auth);

  mySoftwareSerial.begin(9600, SERIAL_8N1, 16, 17);  // speed, type, RX, TX
 
  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.play(9);
}

void loop()
{
  Blynk.run();

}

I made this for my hobby project and I posted this on the community in the hope that it helps someone :smiley:

I had to buy 2,400 Blynk energy for this project but if you do not need more than 5 audios (OR 5 Buttons) then you don’t need to buy :slight_smile:

It is just a small contribution to this community. :smile:

Did you like it? And any suggestions for improvements? Did I miss anything? Please feel free to criticize :wink:
Also, feel free to ask for help. :innocent:

  • Yes
  • No

0 voters


-Animesh Bhatt
1 Like

Hi,

Great that you worked it out! I don’t understand fully why you make a BLYNK_WRITE for each track. With only one, you can do the same: just make 9 buttons in your user interface, value off=0, value on=tracknr

BLYNK_WRITE(V1)
{ 
  int tracknr=param.asInt();

  if (tracknr>0)
  {
    Serial.print("V1 value is: ");
    Serial.println(tracknr);
    myDFPlayer.play(tracknr);
  }
}

Further, I see you prefer to make use of the “standard” DFRobotDFPlayerMini library instead of the one I use. In that case, you don’t need to include the “DFPlay.h” and dfPlay dfplay stuff.
The reason why I didn’t use that library, is that when you have long audio files, Blynk connection will go (in fact, it acts as a long delay).

Here’s another challenge :smiley: In fact you don’t need a DFplay module on ESP32/ESP8266, as it can play audio itself, of course you need some storage (internal memory or sd-card on some modules) and maybe a mosfet.

That’s an amazing idea! Why didn’t I think of that :thinking:

I have read every line of your project at least 3 times but used that standard library because I was having some problems with DFPlay.h and also my audio files were very small like 2-3 seconds long. :smile:

Is this possible?! I never knew about it! How do we do it? Can you please explain? But anyways DFPlayer provides an inbuilt audio amplifier and that’s good :smiley: .

Thanks :smiley:

1 Like