Skip to content

lua code to control dfPlayer mini using ESP8266 based boards

License

Notifications You must be signed in to change notification settings

limbo666/nodemcu-dfPlayer-lua-

Repository files navigation

dfPlayer mini control by lua

Control dfPlayer mini devices from nodemcu (firmware) based devices using their serial communication protocol. dfPlayer mini is a low cost mp3 player which can be used in many appliactions. *Secifications about the hardware can be found on https://www.dfrobot.com/wiki/index.php/DFPlayer_Mini_SKU:DFR0299 * The lack of standarized support on nodemcu was the motivation to create a simple file which send control commands to the device and can be re-used in any application. This code helps developers to integrate mp3 modules easily to any project is needed.

Important notice: dfPlayer serial communication is set by default to 9600 baud and according to DF player there is no commnds to allow change the communication speed. So nodemcu module (ESP, Wemos) should set its UART to 9600 as well Running a UART setup command is essential step to communicate with dfPlayer _uart.setup(0, 9600, 8, uart.PARITY_NONE, uart.STOPBITS_1, 1). UART setup can be called just once, for example in init.lua, but have in mind that this will switch also the communication and programming speed.

Hardware connection example

A simple connection diagram follows. Connection example

Command structure

The complete serial command structure is described in the device manual (http://www.picaxe.com/docs/spe033.pdf). User Yerke was a guy who took this info a bit further by testing most of these commnads under arduino environment and posted his results here ~~ https://forum.banggood.com/forum-topic-59997.html ~~ https://web.archive.org/web/20160510212536/http://forum.banggood.com:80/forum-topic-59997.html (the original post removed from their forums)

In the current lua solution we are working is similar way with Yerke, allowing the user/developer to call the commands by using only the command and parameters (if required).

This simplifies the development of devices and projects. Developer must add a "dofile" command to his code in order to send the requited commnd to the dfplayer.

A simple play command can be executed like this: dofile("cc.lua").ply(0x0d)

or a command which requires parammeters can be called like this: dofile("cc.lua").ply(0x0F,0x01,0x05)

As you can see the only thing whic needs to be set is the hex commands in .ply( ). Command expand

How to use

Upload the lua file (cc.lua) to your module and then from any part of your project you can call the command you like to use.

Examples are following

  1. Play command: dofile("cc.lua").ply(0x0d)
  2. Pause command: dofile("cc.lua").ply(0x0e)
  3. Play file 5 on folder 1: dofile("cc.lua").ply(0x0F,0x01,0x05)
  4. Volume up: dofile("cc.lua").ply(0x04)
  5. Volume down: dofile("cc.lua").ply(0x05)
  6. Volume 20 (of 30 max): dofile("cc.lua").ply(0x06,0x00,0x14)
  7. Select equalizer preset(Rock): dofile("cc.lua").ply(0x07,0x00,0x02)
  8. Play all repeat dofile("cc.lua").ply(0x11,0x00,0x01)
  9. Stop repeat player dofile("cc.lua").ply(0x11,0x00,0x00)

Commands supported

The commands can be found on dfPlayer manual, although there are some points where the command description is not clear enough or the commands are not giving results as described.

The following list is taken from Yerke's post on banggood forums. It contains only the a part of the commands without returned parameters. If you want more info on this visit https://forum.banggood.com/forum-topic-59997.html Update: The post is removed from banggood forums for an unknown reason. Please use web archive https://web.archive.org/web/20160510212536/http://forum.banggood.com:80/forum-topic-59997.html (the original post removed from their forums)

DF Player mini Commands without returned parameters (*=Confirmed command ?=Unknown, not clear or not validated)
CMD  CMD
HEX  DEC Function Description                  Parameters(2 x 8 bit)
==== === =================================== = ======================================================================
0x01   1 Next                                * [DH]=X, [DL]=X Next track in current folder.Loops when last file played
0x02   2 Previous                            * [DH]=X, [DL]=X Previous track in current folder.Loops when last file played
0x03   3 Specify track(NUM)                  * [DH]=highByte(NUM), [DL]=lowByte(NUM)
                                               1~2999 Playing order is order in which the numbers are stored.
                                               Filename and foldername are arbitrary, but when named starting with
                                               an increasing number and then placed in one folder, files are (often) 
                                               played in that order and with correct track number.
                                               e.g. 0001-Joe Jackson.mp3...0348-Lets dance.mp3)
0x04   4 Increase volume                     * [DH]=X, [DL]=X Increase volume by 1
0x05   5 Decrease volume                     * [DH]=X, [DL]=X Decrease volume by 1
0x06   6 Specify volume                      * [DH]=X, [DL]= Volume (0-0x30) Default=0x30
0x07   7 Specify Equalizer                   * [DH]=X, [DL]= EQ(0/1/2/3/4/5) [Normal/Pop/Rock/Jazz/Classic/Base]
0x08   8 Specify repeat(NUM)                 * [DH]=highByte(NUM), [DL]=lowByte(NUM).Repeat the specified track number
0x09   9 Specify playback source (Datasheet) ? [DH]=X, [DL]= (0/1/2/3/4)Unknown. Seems to be overrided by automatic detection
                                               (Datasheet: U/TF/AUX/SLEEP/FLASH)
0x0A  10 Enter into standby – low power loss * [DH]=X, [DL]=X Works, but no command found yet to end standby
                                               (insert TF-card again will end standby mode)
0x0B  11 Normal working (Datasheet)          ? Unknown. No error code, but no function found 
0x0C  12 Reset module                        * [DH]=X, [DL]=X Resets all (Track = 0x01, Volume = 0x30)
                                               Will return 0x3F initialization parameter (0x02 for TF-card)
                                               "Clap" sound after excecuting command (no solution found) 
0x0D  13 Play                                * [DH]=X, [DL]=X Play pointered track
0x0E  14 Pause                               * [DH]=X, [DL]=X Pause track
0x0F  15 Specify folder and file to playback * [DH]=Folder, [DL]=File
                                               Important: Folders must be named 01~99, files must be named 001~255 
0x10  16 Volume adjust set (Datasheet)       ? Unknown. No error code. Does not change the volume gain.
0x11  17 Loop play                           * [DH]=X, [DL]=(0x01:play, 0x00:stop play)
                                               Loop play all the tracks. Start at track 1.
0x12  18 Play mp3 file [NUM] in mp3 folder   * [DH]=highByte(NUM), [DL]=lowByte(NUM)
                                               Play mp3 file in folder named mp3 in your TF-card. File format exact
                                               4-digit number (0001~2999) e.g. 0235.mp3
0x13  19 Unknown                             ? Unknown: Returns error code 0x07 
											Correction: Place ADVERT folder in the root of sd card, and this code pause mp3... play advert, and then continue playing mp3 in the same position ... 
											so if there is 3 sounds in advert folder: 0001.mp3, 0002.mp3 and 003.mp3, 
											with 19 0 3... pause mp3 file... play advert and when finished continues playing mp3
0x14  20 Unknown                             ? Unknown: Returns error code 0x06
0x15  21 Unknown                             ? Unknown: Returns no error code, but no function found                                              
0x16  22 Stop                                * [DH]=X, [DL]=X, Stop playing current track
0x17  23 Loop Folder "01"                    * [DH]=x, [DL]=1~255, Loops all files in folder named "01"
0x18  24 Random play                         * [DH]=X, [DL]=X Random all tracks, always starts at track 1
0x19  25 Single loop                         * [DH]=0, [DL]=0 Loops the track that is playing
0x1A  26 Pause                               * [DH]=X, [DL]=(0x01:pause, 0x00:stop pause)




About

lua code to control dfPlayer mini using ESP8266 based boards

Topics

Resources

License

Stars

Watchers

Forks

Languages