WiFi Enabled MP3 Player Using the ESP8266 Module and CATALEX Serial MP3 Player

10,739

84

23

Posted

Introduction: WiFi Enabled MP3 Player Using the ESP8266 Module and CATALEX Serial MP3 Player

A really simple solution to building a Network controlled MP3 player.

The project utilizes the ESP8266 module, I am using a Wemos D1 mini that is really inexpensive . The MP3 player is available from various supplier on eBay and AliExpress, AliExpress seems to be cheaper but takes for ever to arrive.

D1 Mini ESP8226

YX5300 UART MP3 Player

both items available on eBay here.

Step 1: ESP8266 Web Controlled MP3 Player

I am using the ESP8266 with the MicroPython firmware, head over to the MicroPython website for instructions on how to do that.

Step 2: The MP3 Player

For the MP3 Player I am using the CATALEX uart controlled MP3 player. The player expects commands on the serial ports and then carries them out.

Commands include

Play

Pause

Next

Previous

Stop

Volume Up

Volume Down

Volume Set

Sleep

Wake up

and many more ...

Step 3: Connecting the Two Components.

There are just 4 wires to connect. I am using jumper leads in my test setup.

  • Connect the Gnd from the MP3 player to the ground on the ESP8266
  • Connect the VCC from the MP3 player to the 5v pin on the ESP8266
  • Connect the RX from the MP3 player to the TX on the ESP8266
  • Connect the TV from the MP3 player to the RX on the ESP8266

That's all for the wiring, just plug an amplified speaker to the audio jack of the MP3 player and we are done with the setup.

Step 4:

Go over to the GitHub repository and download all the files,

You will need to upload the following to the ESP8266 module.

  • yx5300.py
  • mp3.py
  • main.py

The MicroPython firmware automatically runs the main.py script at startup if it exists. So once these three files have been transferred you should have a working WiFi MP3 player.

Transferring the files to the ESP8266 Module.

I am aware of at least two ways to transfer the files.

Ampy

A command line utility that connects to the ESP8266 module over the serial port.

WebRepl

The official MicroPython utility that connects to the module over IP. One useful advantage of the WebRepl in this instance is that it displays the console output. This is not possible otherwise as we are using the serial port on the ESP8266 to control the MP3 player.

Share

    Recommendations

    • Space Challenge

      Space Challenge
    • Microcontroller Contest

      Microcontroller Contest
    • Spotless Contest

      Spotless Contest
    user

    We have a be nice policy.
    Please be positive and constructive.

    Tips

    Questions

    20 Comments

    Can you give me a file "main.py" without this mistake ? Thank you per advance. DOM from france.

    Please see my recent post. Line 123 is not indented correctly.

    OK, having played with this all day, the issue is that line 123 of main.py is not indented in-line with the 'elif' command on line 127 (Phyton is very fussy about these things). Fix that and it works.

    Also the author forgot to mention that you need to set-up your network connection in boot.py first (Obvious for old hands but not for newbies!). Read the bottom of section 4.1 here:

    http://docs.micropython.org/en/v1.9.3/esp8266/esp8...

    Hello, I think there is a mistake line 123 : (file main.py)

    Canyou help me ?

    # Acknowledgement
    # Code in the file is based on the code share in the following Github Gist
    # https://gist.github.com/zatarra/fcb9409ef1c1835fa5fb25a860991751
    # https://www.davidgouveia.net/category/iot/

    import machine
    import socket
    import ure
    import mp3

    RELAYS = [machine.Pin(i, machine.Pin.OUT) for i in (12, 13, 14, 15)]
    html = """<!DOCTYPE html>
    <html>
    <head>
    <style>
    .btn {
    background-color: #0c7538;
    border: none;
    color: white;
    padding: 15px 32px;
    text-align: center;
    text-decoration: none;
    display: inline-block;
    font-size: 16px;
    margin: 4px 2px;
    cursor: pointer;
    }
    .lnk {
    background-color: #500c75;
    padding: 5px 32px;
    }
    body {background-color: powderblue;}
    h1 {color: blue;}
    p {color: red;}
    </style>
    <title>MP3 Player</title>
    </head>
    <body>
    <h1>WiFi MP3 Player</h1>
    <p>
    <a href="/prev">Prev</a>
    <a href="/play">Play</a>
    <a href="/pause">Pause</a>
    <a href="/resume">Resume</a>
    <a href="/next">Next</a>
    </p>
    <p>
    <a href="/play?track=1">1</a>
    <a href="/play?track=5">5</a>
    <a href="/play?track=10">10</a>
    <a href="/play?track=15">15</a>
    <a href="/play?track=20">20</a>
    </p>
    </body>
    </html>
    """

    def play_track(track):
    if track is None:
    track=1
    mp3.play_track(int(track))
    return "track %s" % (int(track))

    def play_next():
    mp3.next()
    return "done"

    def play_prev():
    mp3.previous()
    return "done"

    def pause_play():
    mp3.pause()
    return "Paused"

    def resume_play():
    mp3.resume()
    return "resumed"

    def parseURL(url):
    #PARSE THE URL AND RETURN THE PATH AND GET PARAMETERS
    parameters = {}

    path = ure.search("(.*?)(\?|$)", url)

    while True:
    vars = ure.search("(([a-z0-9]+)=([a-z0-8.]*))&?", url)
    if vars:
    parameters[vars.group(2)] = vars.group(3)
    url = url.replace(vars.group(0), '')
    else:
    break

    return path.group(1), parameters

    def buildResponse(response):
    # BUILD DE HTTP RESPONSE HEADERS
    global html
    return '''HTTP/1.0 200 OK\r\nContent-type: text/html\r\nContent-length: %d\r\n\r\n%s''' % (len(html), html)

    addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]

    s = socket.socket()
    s.bind(addr)
    s.listen(1)

    print('listening on', addr)

    while True:
    cl, addr = s.accept()
    print('client connected from', addr)
    request = str(cl.recv(1024))
    print("REQUEST: ", request)

    obj = ure.search("GET (.*?) HTTP\/1\.1", request)
    print(obj.group(1))

    if not obj:
    cl.send(buildResponse("INVALID REQUEST"))
    else:
    path, parameters = parseURL(obj.group(1))

    if path.startswith("/play"):
    track=parameters.get("track",None)
    cl.send(buildResponse("Playing:\n%s" % play_track(track)))

    elif path.startswith("/next"):
    cl.send(buildResponse("Next :\n%s" % play_next()))

    elif path.startswith("/prev"):
    cl.send(buildResponse("Previous :\n%s" % play_prev()))

    elif path.startswith("/resume"):
    cl.send(buildResponse("Pause play:\n%s" % resume_play()))

    elif path.startswith("/pause"):
    cl.send(buildResponse("Pause play:\n%s" % pause_play()))

    elif path.startswith("/halt"):
    cl.send(buildResponse("Shutting down server\n"))
    break
    else:
    cl.send(buildResponse("UNREGISTERED ACTION\r\nPATH: %s\r\nPARAMETERS: %s" % (path, parameters)))

    cl.close()

    Hello, Nice project.I have a problem,my file boot.py, after transfer, is :

    # This file is executed on every boot (including wake-boot from deepsleep)
    #import esp
    #esp.osdebug(None)
    import gc
    import webrepl
    webrepl.start()
    gc.collect()

    What should my boot.py file look like?

    Hi, nice instructable. I have one question: if I were to use another MP3 module, I would have to change the protocol defintion inside the YX5300.py file, is that correct? (Grove mp3 2.0)

    Thanks.

    Hi,

    Yes that should be all that is needed. The protocol for the Grove module seems very similar so I think you should have little problem. If you get that to work feel free to add that to the protocol definition to github, it may help others.

    regards

    Hi, in your guide it says to copy the three files. Shoud I remove the original boot.py file from the esp8266?

    Thank you.

    Yes, back up the existing one and replace it. or you could modify the existing one. the boot.py is automatically run when the module restarts.

    A better solution maybe to have all the code for the web server and mp3 module handling external and launch it from a generic boot.py it's been a long time since I have reviewed the code so there may be other possibilities too.

    Thanks for a swift reply, and sorry for bothering you.

    After making a back up of boot.py, do I just delete boot.py and then copy your files? That's what I did now, but it doesn't work just yet.
    It does show up as an access point. I've checked the the ESP's IP on my router and tried to connect to this IP on my phone (which was connected to the ESP acces point), but no result.
    Are you certain the three files you've listed are all the files required?

    Thank you for your time.