-->
Page 1 of 3

Music Player

PostPosted: Wed Apr 06, 2016 4:55 am
by Electroguard
A browser-controlled music player in a few lines of ESP Basic (ESP Basic 2.0.Alpha 7).

It offer a simple minimalist functional starting point with Play, Pause, Next, Previous, and Volume controls, which means there's plenty of room for improvement both functionally and cosmetically, so I'm looking forward to seeing some improvements posted up by others.

Code: Select allserialprintln chr(126) & chr(2) & chr(12) & chr(239) 'Reset the JQ6500
button "Play" [play]
button "Pause" [pause]
button "<" [prev]
button ">" [next]
button "Vol -" [voldown]
button "Vol +" [volup]
[loop]
wait

[play]
serialprintln chr(126) & chr(2) & chr(13) & chr(239)
goto [loop]

[pause]
serialprintln chr(126) & chr(2) & chr(14) & chr(239)
goto [loop]

[prev]
serialprintln chr(126) & chr(2) & chr(2) & chr(239)
goto [loop]

[next]
serialprintln chr(126) & chr(2) & chr(1) & chr(239)
goto [loop]

[volup]
serialprintln chr(126) & chr(2) & chr(4) & chr(239)
goto [loop]

[voldown]
serialprintln chr(126) & chr(2) & chr(5) & chr(239)
goto [loop]


Voice Module
I used a JQ6500-28 MP3 Player module bought from ebay for just over £6...
http://www.ebay.co.uk/itm/JQ650-28P-U-disk-Audio-Player-TF-Card-Voice-Module-MP3-Sound-Module-Arduino-/181724697839?hash=item2a4fa2d4ef:g:NrQAAOSwpDdVOJ-I
I stuck it into a breadboard along with a breadboard power supply and connected an 8 ohm speaker to pins 1 & 2, but it can be used in other ways if preferred.
Ensure the voice module and ESP 0v are linked together,then cross connect their TXs and RXs as you would normally for connecing up any serial devices.
Plug in an SD with some music files on, then simply run the ESP Basic script above and listen to them.

A couple of gotchas to avoid...
1 Turning up the volume obviously increases power consumption - my little breadboard power supply couldn't cope above two thirds volume and would go into motor-boat oscillation. If you get similar, just remember to reduce the volume a bit before hitting play again.
2 If anything is connected to an ESPs TX at bootup it can prevent the ESP from booting - the simple solution is to disconnect the ESPs TX, reboot it, then reconnect TX.

More info...
I bought the JQ6500-28 MP3 Player module some while back to try out specifically because it had serial control capability plus SD slot for easy loading of media. But there are others available with serial control if you would prefer something more to your own liking, and the same principles of sending serial command strings to control it will still apply even if the actual command codes or structure varies.

Here's a good source for JQ650 command codes...
http://sparks.gogo.co.nz/jq6500/serial.html

The basic script (above) was kept smaller by hard-coding in the appropriate decimal command codes, but it's less tiresome to assign them to variables as in the next script. It's also much handier for creating new commands sequences, because it's easier to remember byte names like startbyte and endbyte than to use the corresponding raw numbers.

The JQ650 recognises 2 types of commands:

Control commands, which just tell it to do something (ie: Play).
Query commands, which expect returned information (ie: Get the name of the current file on the SD Card).

Both these 2 basic scripts send only Control commands, so because they are not looking for responses (and wouldn't know what to do with them anyway), they don't actually need the ESP RX connection, and function fine without it.

If you wish to use any of the Query commands you will obviously need to provide appropriate response handlers to deal with the received info, but I leave that up to you.

The following script is similar to the first but uses command variables for improved sanity, and has a slider volume control instead of up/down buttons.

Code: Select alllet vstart = 126 'Serial command start byte
let vlen = 2
let vend = 239 'Serial command end byte
let vplay = 13
let vreset = 12
let vpause = 14
let vnext = 1
let vprev = 2
let vup = 4 'not used
let vdown = 5 'not used
let vset = 6
let vol = 15

gosub [reset]
button "<" [prev]
button "Play" [play]
button "Pause" [pause]
button ">" [next]
html "<BR>"
html "<BR>"
slider "vol" 0 30 'If your psu is too week for max vol, reduce 30 to something like 20
button "Set Volume" [setvol]

[loop]
wait

[setvol]
serialprintln chr(vstart) & chr(3) & chr(vset) & chr(vol) & chr(vend)
goto [loop]

[play]
serialprintln chr(vstart) & chr(vlen) & chr(vplay) & chr(vend)
goto [loop]

[pause]
serialprintln chr(vstart) & chr(vlen) & chr(vpause) & chr(vend)
goto [loop]

[next]
serialprintln chr(vstart) & chr(vlen) & chr(vnext) & chr(vend)
goto [loop]

[prev]
serialprintln chr(vstart) & chr(vlen) & chr(vprev) & chr(vend)
goto [loop]

[volup]
serialprintln chr(vstart) & chr(vlen) & chr(vup) & chr(vend)
goto [loop]

[voldown]
serialprintln chr(vstart) & chr(vlen) & chr(vdown) & chr(vend)
goto [loop]

[reset]
serialprintln chr(vstart) & chr(vlen) & chr(vreset) & chr(vend)
return

Re: Music Player

PostPosted: Wed Apr 06, 2016 6:29 am
by viscomjim
Electroguard, you might want to consider using the second TX on the esp module. This way you won't have to worry about disconnecting and reconnecting. I think this was covered in one of the project files for the weather station. This should be good since you are not sending any queries to the mp3 module, so no RX is required. Nice project by the way!!!

Hi,
there are 3 commands linked to the serial port 2 :
serial2begin {baudrate}
serial2print var
serial2println var
these commands exist since some load, I think since the 1.83.
About the pins, unfortunately only the output pin is available as the RX is occupied for flash chip connection :-( .
Wrong choice done by the developers but this is outside our control :-).
You can find the details here, under the chapter Serial :
https://github.com/esp8266/Arduino/blob ... ference.md

For the pin, the TX2 is the GPIO2.

CiccioCB - See more at: viewtopic.php?f=41&t=8574#sthash.xlkWFhlz.dpuf

Re: Music Player

PostPosted: Wed Apr 06, 2016 7:57 am
by Electroguard
Thanks, and yes, that looks to be exactly what's needed.
How's it done cicciocb? - (I use the serial port 2 @ 115200)

Re: Music Player

PostPosted: Wed Apr 06, 2016 10:13 am
by Electroguard
I tried adding "serial2print.begin 9600" and changing all references of serialprintln to serial2println, then taking TX cable from GPIO02, but the JQ6500 module didn't receive (nothing it understood, anyway).

I'm not sure it would have helped though, because I suspect the problem with the ESP TX preventing normal bootup is probably due to it being linked with GPIO02, and GPIO02 is used to control boot mode, so it may be GPIO02 that is the real problem rather than a TX.

Need to try to get around it somehow though, else there's no guarantee that ESP Basic remote nodes which are using serial TX (or GPIO02) will be able to reliably recover from sleep or power failures etc.

So going on the assumption that the serial connection must be preventing TX (GPIO02) from floating at switchon/reset, I've tried sticking a diode in series with the TX cable to see if there's a simple way to prevent it interfering, and I'm quite surprised that it doesn't prevent serial transmission whichever way round it is inserted.

I haven't had an ESP bootup failure since, but it was something that was previously intermittent so I'm not rejoicing yet, but at least it looks hopeful.