Saturday, June 18, 2016

NodeMCU (ESP8266) + DFPlayer - mp3 music from SD card

I have some plans to make a doorbell as a part of my smart home and one of the parts required for that is the music player to play some rock for postman :) The first step for this project is establishing connection between NodeMCU (ESP8266 devboard) I use for my smart home and DFPlayer - mp3 module with builtin microSD cardreader.

So, let's rock!

Preparing the Card

I've tried with 2GB and 4GB - both works.
The FS format should be either FAT16  or FAT32 (MS DOS filesystems)
Create "mp3" folder in the root of the card and put there several mp3 files with the following names:
[4 digits][anything_else].mp3. e.g.:
0001.mp3
0002_AC_DC_hell_bells.mp3

The final file structure should be like following:

mp3
 |
 |--- 0001.mp3
 |--- 0002_AC_DC_hell_bells.mp3

Wiring


Note that DFplayer module requires 5v. This voltage can be accessed from Vin pin on NodeMCU - it's direct power line from USB connector.
The logic level for DFPlayer is 3.3V, so it's safe to connect it with NodeMCU without any resistors.

Arduino Code

Player can be controlled several ways, but I chose serial port connection since it used in the library I found. 
There are 1.5 hardware serial ports in NodeMCU: both shares the same RX. I tried to make it work simultaneously with the default Serial, but didn't succeed. Thus I decided to try software serial with great results.
Also note the connection between D5 on NodeMCU and BUSY pin on Player side. This pin can be used to get the status from it (playing or not)

#include <DFPlayer_Mini_Mp3.h>
#include <SoftwareSerial.h>

#define PIN_BUSY D5

SoftwareSerial mp3Serial(D1, D2); // RX, TX

void setup () {
  pinMode(PIN_BUSY, INPUT);
  Serial.begin (115200);
  Serial.println("Setting up software serial");
  mp3Serial.begin (9600);
  Serial.println("Setting up mp3 player");
  mp3_set_serial (mp3Serial);  
  // Delay is required before accessing player. From my experience it's ~1 sec
  delay(1000); 
  mp3_set_volume (15);
}

void loop () {
  Serial.println("Stop");
  mp3_stop ();
  Serial.print("Busy: ");
  Serial.println(digitalRead(PIN_BUSY));
  delay(500);
  
  Serial.println("play next");
  mp3_next ();
  Serial.print("Busy: ");
  Serial.println(digitalRead(PIN_BUSY));
  delay (12000);
}



11 comments:

  1. hay buddy, i have a problem with this modul, can u plan me, how the code for the RTC. i can't play the sound for my Alarm.

    ReplyDelete
  2. That is enterprise associated knowledge gaining article. This put up is truly the first-class on this valuable subject matter. motu patlu

    ReplyDelete
  3. how to code to play only the selected mp3 file among the rest of the file? mp3_play(1) for example won't play the first file, but removing the "1" works. however if i want to use the 2 or 3 file, the code won't play accordingly.

    ReplyDelete
  4. I am facing the same problem.
    Did you know the command to play only one track?
    I want to put this command in an if statement.
    If this play that.
    Thanks in advance

    ReplyDelete
    Replies

    1. you need to have a folder called "mp3"
      and the files must be in the format "0001.mp3"
      "0002.mp3"

      command = mp3_play(number_music);
      mp3_play(2);

      Delete
  5. i am getting compilation error "error compiling to nodemcu esp-12e module" can some one help with it please

    ReplyDelete
  6. Amazing nice work. This is very useful article. Thank you. For more

    ReplyDelete
  7. Hello, I get the error:

    Halloween_Box_Head_Skeleton1.ino: In function void setup()

    Halloween_Box_Head_Skeleton1.ino: 99:33: error: invalid initialization of reference of type 'HardwareSerial&' from expression of type 'SoftwareSerial
    mp3_set_serial (dfplayerSerial)

    Halloween_Box_Head_Skeleton1.ino:17: In file included from

    DFPlayer_Mini_Mp3.h: 78:6: error: in passing argument 1 of 'void mp3_set_serial(HardwareSerial&)
    void mp3_set_serial (HardwareSerial &theSerial)
    Error compiling project sources
    Build failed for project 'Halloween_Box_Head_Skeleton1'

    Do yo uknow how to fix this?

    Thanks!

    ReplyDelete
  8. 1xBet Korean Bet in South Korea - legalbet
    A guide 1xbet to the best legal sports betting and casino games in South Korea. We recommend the ⭐bookmakers,

    ReplyDelete