Can't get Serial2 to work on ESP32

Hi,
first of all I would like to mentioned that WLed is an awesome project. I recently started to learn to make usermods.
My goal is to add a cheap dfplayer mini (mini MP3 player) to my project. It is controllable using a simple Uart.
I followed the user mod v2 example and tailored it to my needs using the Makuna/DFMiniMp3 library. After the initial struggles to get everything building, I found out that I can only get the dfplayer to work using the default Serial Uart (and default pins for UART0) which is normally used for debugging. If I try to use Serial1 (redirecting pins to othe GPIOs) or Serial2 (default pins 16, 17) on my ESP32, the dfplayer does not work.
Does anybody know if I need to take special action to get the other Uarts working or do I need to manage GPIO pins for them to work?
Maybe good to mention that I tried using the library with Serial2 in my arduino environment first and that works perfectly well. So I assume it has to do with Wled, not with my ESP.
I have seen the example of the DMX code where Serial2 is used, so I assume it is possible to use it.
Thanks in advance for the help and/or ideas!

Hi,

I’ve been experimenting with a HLK2410 Radar sensor in my own user mod, that uses Hardware Serial 2 on the EPS32 and works fine for me, no additional things needed except for the obvious. Here’s my Setup() part:

        // Note! TX/RX wires are inverted, fixed here by inverting pin 17 & 16 !
        Serial2.begin (256000, SERIAL_8N1, 17, 16);
        if(radar.begin(Serial2))
        {
          DEBUG_PRINTLN(F("ESP32 - LD2410 Radar OK"));
          sensorPresent = true;
        }
        else
        {
          DEBUG_PRINTLN(F("LD2410 Radar not connected"));
        }

Thanks Martin for the help, at least it keeps me continuing to experiment :slight_smile:
Indeed I got my mp3 player finally working on Serial2, but only in the Setup() function! In the loop, nothing happens anymore. I don’t know if that is because no data is send over the Uart or because the Uart is not available for me or only part of the data is send.
Any ideas? Do I need to check if the Uart is available? I added flushes before sending data to be sure.
Thanks.

I think I finally found two issues: The library I was using was not working correctly with Serial2 and that might have to do with the fact that pin 16 was the default pin for the first WS2812 segment.
Before finding that out, I tried another library and that did work! I now use the DFRobotDFPlayerMini library. It is great to be able to control the mp3 player using JSON commands.
Up to the next challenge!
My first tests with writing a mod nicely showed setup parameters for the mod after following the example code. Since a while, that does not work anymore and results in a reboot of the ESP32, every time I save settings. Even removing all my parameters, I still end up with a reboot and no settings are stored.
I am suspecting there might be a problem with the littleFS library…

Just wondering if you had any more progress in your usermod for the DFPlayerMini. I too am interested in using the DFPlayerMini for a standalone project with WLED. Just wondering if you could share what you have so far to take a look.
Thanks.

Hi,
I do have progression :grinning: and thanks for the interest in the project. In fact, I am using serial2 to control the dfmini using json commands and I can save my own settings on the usermod page.
It turned out that something was corrupted in Visual Studio. That filled up the esp32 memory. I restarted the project from scratch and now it is ok.
That being said, I have not tested all functionality properly like interaction with led control. I also noticed that the dfmini sometimes does not respond anymore. Probably caused by sending commands too fast or a hw issue or not handling of the status correctly. For such a cheap component, some flaws are acceptable I think…

For me, it is good enough as it is, some more testing needs to be done but I can share what I have after cleaning up some test code. I will write down the functionality and try to post (will be my first, so need to find out how to do that) this weekend. If you like it, let me know…

Here is the code I have so far. It kind of works but is not finished yet. I am struggling most with reliably reading a status change to trigger a new action if needed.
visualCodeWledDfPlayer.zip (20.5 KB)
I assume you know how to add these files to a visualcode project based on 0.14.0-b2 of WLED and build the solution. Also make sure to add the dfRobotDfplayerMini (version 1.0.5) to your project. I use the esp32dev for building.

Some remarks on the hardware: The dfplayer is connected to pins 16 and 17 (default Serial2) but can be configured (did not test another pin configuration though). Make sure to use resistors in the RX/TX lines of the dfplayer otherwise it will produce very annoying noise!
The default configuration of the first led output pin was also pin 16 in my case, so make sure to check and change if that is the case.

My code supports the following instructions (based on playing from a SD card):

  • reset (true/false): 'true’will trigger a dfplayer reset.
  • play (true/false): ‘true’ will trigger a start using track, folder, random and loop settings. ‘false’ will stop playback
  • Track number: which track to play in the given folder
  • Folder number: which folder to select
  • random (true/false): ‘true’ sets random track selection within the folder, otherwise just the next file.
  • volume ( 0 -30): volume setting (max is 30 the dfplayer supports)
  • loop (true/false): ‘true’ triggers the next track after the current one finished.

I used curl to send commands to WLed, e.g.:
curl-7.42.0-win64>curl -X POST “http://[WLed IP address]/json/state” -d {“dfplayer”:{“volume”:12,“play”:true,“loop”:true,“random”:true,“tracknr”:2,“folder”:4}} -H “Content-Type: application/json”
I think I covered the most important things. Enjoy. Any questions, let me know.

Can you help me install this mod?

Hi, what help would you like to have?
If you can build the standard wled code as explained in an excellent step by step guide on kno.wled.ge/advanced/compiling-wled then you are well on your way.
After that, simply put the files from the zip file in the project, some overwrite existing files. Install the dfRobotdfPlayer library, rebuild and done.

have you tried using the Preset API Command for the JSON command instead of curl?
e.g.
{“mydfplayer”:{“volume”:12,“play”:true,“loop”:true,“random”:true,“tracknr”:1,“folder”:1}}

I am not sure if I did to be honest. My main goal is to control wled from my home control system (Openhab). And that is working :grinning:
I think I tried using the preset API command once to successfully start a playback. Is there a reason why it shouldn’t work?
I am still not happy with the way the driver detects whether a song is playing. That seems unreliable. It is the basis for triggering the next song. So for now I removed all functionality from my code except ‘play’ and ‘volume’.
When I get back from holidays, I will try to use a different driver.

ok thanks. Let me know if you have any luck with the updated code.

JwTielen, just wondering if you are using a clone of the DFPlayer Mini? Reading this article that the clones have delays between command and may skip commands that you send. Just thought it might be the reason you are having reliability issue.

Hi, thanks for the link to the article about different type of boards. It might explain some difficulties I have experienced during development. Although cheap, quality and reliability does not seem to be a high priority in the design :slight_smile:

Last week I started using a different library to control my board. I also adapted my logic of controlling the board. And I must say, I am happy with the result. I can control the board the way I want. So supporting random play per folder and looping folders etc.
I will post the code soon for those interested. I am now using the powerbroker2/DFPlayerMini_Fast@^1.2.4 / powerbroker2/FireTimer@^1.0.5 library which is a very basic and easy to understand library.

usermod_v2_dfplayermini.h (21.1 KB)
usermods_list.cpp (8.5 KB)
Hi,
this is my latest version of the dfplayer mp3 usermod. I used a different library that seems to work well for me as mentioned in my previous post: DFPlayerMini_Fast
So if you want to use my code, include the mentioned libraries into your project.
Copy the usermod_v2_dfplayermini.h into your project and adapt or copy the usermod_list.cpp file over the existing one. Compile and you should be good to go.

Maybe good to mention: I only compiled and tested for my ESP32dev board. Furthermore I did not test while running a LED string at the same time yet. The following curl example shows all supported commands:
curl -X POST “http://192.168.1.183/json/state” -d {“dfplayer”:{“volume”:12,“play”:true,“loopfolder”:false,“random”:true,“tracknr”:2,“folder”:4}} -H “Content-Type: application/json”
‘play’ is the most important obviously to start and stop a song. Random takes precedence over ‘tracknr’ if used together.
For those who like to give the code a try: let me know how it works out for you!

Thank you for working on this! I am seeing WLED as a great framework for making gadgets intended for use by folks other than me (ie it needs to just work). One project that has me stumped is adding lights and sounds to my kids’ toddler kitchen. Multiple triggers, sounds and lights.

Once I get through my “holiday backlog,” I hope to try this out for this purpose.

You might want to have a look for the “Turn Signal” mods various people have worked on in the Forum and/or usermods. It’s an application that needs multiple switch inputs to control lights in a particular fashion. I would guess you might end up with similar issues in your toddler kitchen. Could be some helpful hints there.

Awesome, thank you! This project seems like such a common use case, but I have struggled to find good examples. I always assume I just not using the best search terms.