Giter VIP home page Giter VIP logo

dfplayer-mp's Introduction

DFPlayer Mini for MicroPython

This is a fully featured library for interfacing with the DFPlayer Mini MP3 player by DFRobot. It also supports other manufacturer's versions of the module.

Note

This driver is meant to be used in conjunction with the asyncio library.
Methods that interface with the DFPlayer should be awaited until the player acknowledged the command/returned a queried value.

Resources

๐Ÿ“– API Reference
๐ŸŒ Official DFRobot Wiki

Installation

Extract the latest release to your MCU's storage. On most platforms /lib/ is on the search path per default and should preferably be used to install libraries to. An example directory may look like this:

lib/
โ””โ”€โ”€ dfplayer/
    โ”œโ”€โ”€ __init__.py
    โ””โ”€โ”€ dfplayer.py
main.py

Tip

The source code contains lengthy Docstrings and is thus quite large. You may compile the library to .mpy files to remove this overhead.

Example Setup

Required Hardware

  • MicroPython-installed MCU with UART capabilities
  • DFPlayer Mini
  • microSD card (FAT16 or FAT32; up to 32 GB)
  • Speaker (3 W output power)

Wiring

For this wiring diagram an ESP32-DevKitC is used. Any MicroPython-supported MCU with RX/TX pins for UART communication should work.

Circuit Diagram

Make sure to connect the RX pin to a TX capable pin on the MCU and vice-versa. Boards with only a single UART bus are hard to develop with, due to that bus usually being used for serial debug communication (the MicroPython REPL). This example uses UART bus id 2 for which the board's default pins are 16 (RX) and 17 (TX).

An additional 1k Ohm resistor can be connected between the RX input of the DFPlayer Mini and the TX pin of the MCU, if commands are sometimes not being acknowledged due to board noise on the MCU.

DFPlayer Filesystem

Either MP3 or WAV files can be played back. For simple use cases, use this file structure:

01/
โ”œโ”€โ”€ 001.mp3
โ”œโ”€โ”€ 002.mp3
โ”œโ”€โ”€ 003.wav
    ...
โ””โ”€โ”€ 255.mp3
02/
โ”œโ”€โ”€ 001_Files-can-have.mp3
โ”œโ”€โ”€ 002_human-readable.mp3
โ””โ”€โ”€ 003_suffixes.mp3
    ...

Folders 01 - 15 can have up to ~3000 files if required.

Example Code

Basic Usage

from asyncio import run
from dfplayer import DFPlayer

async def main():
    df = DFPlayer(2) # using UART id 2
    df.init() # initialize UART connection
    await df.wait_available() # optional; making sure DFPlayer finished booting

    await df.volume(15)
    print("DFPlayer reports volume:", await df.volume())

    await df.play(1, 1) # folder 1, file 1

run(main())

Event Listeners

def handle_done(device: int, track_id: int):
    print("Playback ended on storage device: {} (Track ID: {})".format(device, track_id))
df.on_done(handle_done)
df.on_eject(lambda device: print("Device", device, "ejected!"))
df.on_insert(lambda device: print("Device", device, "inserted!"))
df.on_ready(lambda devices: print("Ready! Is the SD-card ready?", bool(devices & df.DEVICE_SDCARD)))

df.init()

Await Playback End

done = await df.play(2, 2540)
await done
print("Track done, playing another one.")

done = await df.play(1, 4)

dfplayer-mp's People

Contributors

muhlex avatar phlipinator avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.