Wishful Coding

Didn't you ever wish your
computer understood you?

Raspberry Pi mp3 player

connected

Ingredients:

  • Raspberry Pi
  • USB battery
  • Headphones
  • Case(optional)
  • Bunch of wires
  • Female 1” header
  • Pile of resistors
  • Leftover shard of breadboard

Put the buttons, resistors and wires on the breadboard in a basic pull-up configuration.

schematic

Use tin, glue and pushpins to connect everything together.

inside

Admire.

case

Install cmus and write some code to control it using the GPIO buttons.

import RPi.GPIO as GPIO
import socket
from time import sleep

GPIO.setmode(GPIO.BOARD)
GPIO.setup(23, GPIO.IN)
GPIO.setup(21, GPIO.IN)
GPIO.setup(19, GPIO.IN)
GPIO.setup(15, GPIO.IN)

s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
s.connect('/home/pi/.cmus/socket')

def play_pause(e):
    s.send(b'player-pause\n')

def next(e):
    s.send(b'player-next\n')

def volume_up(e):
    s.send(b'vol +10%\n')

def volume_down(e):
    s.send(b'vol -10%\n')

GPIO.add_event_detect(23, GPIO.FALLING, callback=play_pause, bouncetime=200)
GPIO.add_event_detect(21, GPIO.FALLING, callback=next, bouncetime=200)
GPIO.add_event_detect(19, GPIO.FALLING, callback=volume_up, bouncetime=200)
GPIO.add_event_detect(15, GPIO.FALLING, callback=volume_down, bouncetime=200)

while True:
    sleep(1)

Make sure the code runs at startup.

# /etc/rc.local
su - pi -c "screen -d -m cmus"
sleep 5
python /home/pi/play.py >> /var/log/pyplay.log 

Enjoy!

ssh interface

Oh, the audio is pretty bad. It might need an USB sound card.