redoxcode/micropython-dfplayer

get_files_in_folder(1) always returns -1, while play(1,1) plays the mp3

Closed this issue · 9 comments

I'm using a Raspberry Pico with the DFPlayer Mini. I'm also using the TX/RX pins 4,5 as shown in your example.

The file structure is as suggested (only one directory with one file):

.
├── 01
   └─ 001.mp3

From the example:

import time
from dfplayer import DFPlayer
df=DFPlayer(uart_id=1,tx_pin_id=4,rx_pin_id=5)
time.sleep(0.2)
print(df.get_files_in_folder(1))

This always returns -1 for me. However, df.play(1,1) works just fine and plays the file 001.mp3.

Hey, thank you for your bug report.
I will try to reproduce (and then hopefully fix) the issue as soon as possible.
Oh and welcome to github!

We know the Pico can send data to the DFPlayer, since you can start a playback.
But can you verify that the DFPlayer can send data back to the Pico at all?
Is get_volume() or ìs_playing() working?

The code should be changed to debug this kind of issues more easily...

Thank you for all the work!

I do receive data back, it it seems very inconsistent.

For both get_volume() and is_playing(), I occasionally receive the correct response (e.g. 1 if it's playing and the previously set volume). But I also get incorrect 0 as a response on both and even:

>>> df.set_volume(20)
>>> df.play(1,1)
>>> df.is_playing()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "dfplayer/__init__.py", line 65, in is_playing
IndexError: bytes index out of range
>>> df.get_volume()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "dfplayer/__init__.py", line 68, in get_volume
IndexError: bytes index out of range

However, I don't understand what the result depends on.

As for get_volume(), it only seems to work while the file is playing. If I try to get the volume, if nothing is playing, it will return 0. If I reset(), set_volume(20), play(1,1), get_volume()` will return the correct value one or multiple times, but will eventually throw the above error.


I also managed to get the correct value for get_files_in_folder(1), if I run the command while the file is playing. But I also ran into the same IndexError: bytes index out of range error, just in line 72.

Hmm... It's strange that they work sometimes and some times not. Maybe this is something with your wiring?
What's happening in get_volume() or ìs_playing() is that the pico sends data and waits for an answer.
If it doesn't receive anything it times out and returns 0. If it gets something invalid (could be noise) it will throw that error.
I am sorry that the error handling is so poor in the current state of the project.

I'm currently using jumper wires for the temporary setup. I'll try to replicate the setup on a breadboard, maybe the responses will be a bit more stable then. I also have a second DFPlayer lying around I can switch out, so I can check if the module may be a bit faulty.

I'll report back with the behavior, once I built a hopefully more stable setup.

No worries about that, your library is very helpful!

Okay so I redid the wiring and added 1kΩ resistors for the RX/TX connection, as recommended by this bloke: https://electronoobs.com/eng_arduino_tut122.php

However, the results are the same. I only get the volume or number of files, while a file is playing and the responses are not very stable.

>>> df.get_files_in_folder(2)
6
>>> df.get_files_in_folder(2)
-1
>>> df.get_files_in_folder(2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "dfplayer/__init__.py", line 74, in get_files_in_folder
IndexError: bytes index out of range

As a workaround, I implemented a loop at startup like this, as I'll only need this value right after boot:

df.volume(0)
df.play(1,1)
num=-1
while num<0:
    try:
        num=df.get_files_in_folder(1)
    except:
        pass
df.stop()

Hey, I just tested on my hardware and everything works fine. Strange.
However: Check out this branch (https://github.com/redoxcode/micropython-dfplayer/tree/1.0.3) where I added some error checking. It's not on pypi yet, so you have to download from git.
cheers

Hello @secrichard,
did you have a chance to test with the new code?
Or did you manage to resolve the issue?

Without further input I will mark this issue as closed.