Mastodon

Sunday 24 September 2023

The ProgPod Mini

I'm one of these people that needs some music or some sort of sounds playing to get me to sleep. I can sleep through music, a thunderstorm, heavy winds. But a dripping tap, someone snoring or even heavy breathing and I'm wide awake.


I wanted an mp3 player that I could easily drag and drop music onto, some of the discontinued Apple device were great for this even though you had to use iTunes. I wanted a Windows share I could use and so my Pipod Mini-ish was born.




I put together a Raspberry Pi zero, a set of bluetooth earbuds, an SD card, a plastic box, 4 PCB mount switches, some vero board and a few bits of wire. On top of this there's a small board that acts as the psu and of course some hot glue. It's not pretty but it does work.

The physical side is up to you, a 3d printer could come up with quite a nice case and you could look at adding a display too. For this, I wasn't bothered as battery life was important.

When you set the Pi up for the first time with raspi-config set your user to auto login at the console.

First off, get some software installed.

sudo apt update
sudo apt upgrade
sudo apt-get install pulseaudio pulseaudio-module-bluetooth mpg123 mc samba samba-common bluez-tools

Add your user to bluetooth
sudo usermod -G bluetooth -a username


And now time for a restart
sudo shutdown -r now


Once it's back up and running open up a terminal and run.
pulseaudio --start

Then run bluetoothctl
Use "help" if you want more details about the available commands. The first time, you'll have to run the following:

power on
agent on
scan on
wait for the device to be discovered, note it's address (you can then use tab for auto-completion). For example, a set of ear buds I have show as Device 58:B3:BD:18:21:CE A90 Pro
pair 58:B3:BD:18:21:CE
trust 58:B3:BD:18:21:CE
connect 58:B3:BD:18:21:CE wait for the confirmation of connection and then type quit


Now we edit edit /etc/pulse/default.pa
sudo nano edit /etc/pulse/default.pa

Add this to the bottom of the file, then save and exit.

# automatically switch to newly-connected devices
load-module module-switch-on-connect

Then edit edit /etc/bluetooth/main.conf
sudo nano /etc/bluetooth/main.conf

Make sure this is showing at the end.

[Policy]
AutoEnable=true

Edit the bluetooth service with
sudo nano /lib/systemd/system/bluetooth.service

Look for this line:
ExecStart=/usr/lib/bluetooth/bluetoothd
and add --plugin=a2dp to the end so it looks like:
ExecStart=/usr/lib/bluetooth/bluetoothd --plugin=a2dp


Restart the service with:
systemctl daemon-reload
systemctl restart bluetooth


I wanted to be able to switch tracks and control the volume from four keys, switching the tracks was easy and just needed this adding into the /boot/config.txt file.

sudo nano /boot/config.txt

Add this to the end.

dtoverlay=gpio-key,gpio=16,keycode=33,label="Next"
dtoverlay=gpio-key,gpio=19,keycode=32,label="Previous"
dtoverlay=gpio-shutdown


I used similar options for the volume but nothing happened, so a bit of python controls this.

nano mixer.py


Paste this lot in

#!/bin/python
from gpiozero import Button
from signal import pause
from subprocess import Popen


def increase_volume():
global is_muted
if is_muted:
toggle_volume()
Popen(['amixer', 'sset', 'Master', '2%+'])


def decrease_volume():
global is_muted
if is_muted:
toggle_volume()
Popen(['amixer', 'sset', 'Master', '2%-'])


def toggle_volume():
global is_muted
Popen(['amixer', 'sset', 'Master', 'toggle'])
is_muted = not is_muted


def mute_volume():
global is_muted
if not is_muted:
toggle_volume()


is_muted = False #set it off to begin with


button_up = Button(20)
button_up.when_pressed = increase_volume
button_down = Button(21)
button_down.when_pressed = decrease_volume
button_down.when_held = mute_volume
pause()




I've created the music folder with:
sudo mkdir /mnt/Music
sudo chmod 0777 /mnt/Music


Then created a link to the user folder with
cd ~/
ln -s /mnt/Music


Amend the smb.conf file with
sudo nano /etc/samba/smb.conf


Add this to the bottom, then save and exit.

[Music]
comment = MP3 Files
path = /mnt/Music
guest ok = yes
browseable = yes
create mask = 0777
directory mask = 0777
read only = no


Last thing is to get it all playing on startup.

I made a file to run at startup with:

sudo nano /usr/local/bin/runmusic

This is the contents of the file:

#!/bin/bash
cd /home/username/Music
pulseaudio --start
amixer sset Master 40%,40% on
python /home/username/mixer.py &
mpg123 -CvZ --stereo --gapless *.mp3


Save that and then run:
sudo chmod +x /usr/local/bin/runmusic


Next up is:
sudo nano ~/.bashrc

Add this to the end:
/usr/local/bin/runmusic


And finally, make a file called winstall.sh or something, it really isn't important.


nano winstall.sh


Copy this lot into it


mkdir ~/temp
cd ~/temp


echo "Installing Window Networking Service"
wget https://github.com/christgau/wsdd/archive/master.zip
unzip master.zip
sudo mv wsdd-master/src/wsdd.py wsdd-master/src/wsdd
sudo cp wsdd-master/src/wsdd /usr/bin


echo "[Unit]">wsdd.service
echo "Description=Web Services Dynamic Discovery host daemon">>wsdd.service
echo "; Start after the network has been configured">>wsdd.service
echo "After=network-online.target">>wsdd.service
echo "Wants=network-online.target">>wsdd.service
echo "; It makes sense to have Samba running when wsdd starts, but is not required">>wsdd.service
echo "Wants=smb.service">>wsdd.service
echo "[Service]">>wsdd.service
echo "Type=simple">>wsdd.service
echo "ExecStart=/usr/bin/wsdd --shortlog">>wsdd.service
echo "; The following lines can be used for a chroot execution of wsdd.">>wsdd.service
echo "; Also append '--chroot /run/wsdd' to ExecStart to enable chrooting">>wsdd.service
echo "; AmbientCapabilities=CAP_SYS_CHROOT">>wsdd.service
echo "ExecStartPre=/bin/mkdir -p /run/wsdd">>wsdd.service
echo "ExecStartPre=/usr/bin/install -d -o nobody -g nogroup -m 0700 /run/wsdd">>wsdd.service
echo "ExecStopPost=rmdir /run/wsdd">>wsdd.service
echo "[Install]">>wsdd.service
echo "WantedBy=multi-user.target">>wsdd.service


sudo mv wsdd.service /etc/systemd/system/wsdd.service
sudo systemctl daemon-reload
sudo systemctl start wsdd.service
sudo systemctl enable wsdd


sudo systemctl daemon-reload
sudo service smbd restart

cd ~/
rm -rf temp

run it with sh winstall.sh

Once completed you should find the Raspberry Pi visible in the Windows Network

Save and then reboot.


To get my headphones to connect I have to put them back in the case and take them out once the Pi has started up. You could use the audio out on a Pi 3 or Pi4 or add a USB sound card to your Pi Zero. I don't really want any more wires.


Last thing I'll be doing is adding a charge socket to the bottom and power off button to shut it down cleanly.


I'm not clever enough to think this lot up by myself so thanks to Actuino at https://gist.github.com/actuino/9548329d1bba6663a63886067af5e4cb for the initial guidance and someone else for the python mixer script.



No comments:

Post a Comment