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);
}