I am trying to use a GPS module (NEO-6M V2) and a MP3 player (DFPlayer Mini) on an Arduino Nano. I have to communicate with both using SoftwareSerial, as I want to keep hardware Serial for USB debugging.
Based on what I found mainly on this forum, I should use SoftwareSerial::listen(). However, the program stops at some point and can't understand why. I successfully replicated the exact issue with the program below :
The GPS part doesn't do anything on the first run, leading to the first .mp3 being played properly. I then get a "reaction" I expect from it, but it then freezes the program before playing the next file. I can't figure out why...
Is there something I misunderstood related to the use of listen() ?
One last thing that may be helpful, I don't actually need to receive any data from the DFPlayer Mini, so if there is a way to send to it the track number without the use of SoftwareSerial, that may be a workaround.
From the SoftwareSerial docs:
Listen()
Enables the selected SoftwareSerial object to listen. Only one SoftwareSerial object can listen at a time; data that arrives for other ports will be discarded. Any data already received is discarded during the call to listen() function (unless the given instance is already listening).
That sounds about right.
Some times you'll get the GPS string with lat/long in it.
Other times, you'll throw out part of the string when calling listen() and the format will be corrupt.
I tried removing both gpsSerial.listen() and mp3Serial.listen(), given gpsSerial.begin(9600) already calls gpsSerial.listen() after having initialized the mp3 serial connection. I thought I didn't need to literally listen to mp3 serial to only send commands, however the program's behavior stays the same and freezes when the second track is supposed to be played
Tried that while also removing mp3Serial.listen() entirely, but mp3.play(i) still only works once. The program freezes at the moment of calling mp3.play(2)
Just to be clear, all tracks are played when gpsSerial.begin(9600) is commented out. That's why I am sure that the issue comes from the two SoftwareSerial not interacting well with each other
The behavior was the same when replacing SoftwareSerial mp3Serial(4, 3) by SendOnlySoftwareSerial mp3Serial(3)
I am currently trying to use this library to manually write bytes, so I avoid using the DFRobotDFPlayer library, I'll show the results
What version of the library are you using? The latest version doesn't seem to have a .begin function/method with that signature. The only one allowed is .begin(&Stream, bool, bool).
What I was going to say was: if you use
mp3.begin(mp3Serial, false, false);
this will tell the library not to expect any serial communications from the MP3 player, or send any commands to it that would cause it to send any serial data. So then you never need to use mp3Serial.listen().
// This is where it gets stuck
// It will read forever
while (gpsSerial.available() > 0)
{
gps.encode(gpsSerial.read());
}
// Replace with this:
// read the data until a valid sentance is received
int data = gpsSerial.read();
while (gps.encode(data) == false)
{
data = gpsSerial.read();
delayMicroseconds(1200);
}