Mastodon
Showing posts with label RaspberryPi Zero. Show all posts
Showing posts with label RaspberryPi Zero. Show all posts

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.



Tuesday, 1 August 2023

Raspberry Pi Access Point

 So you go on holiday and you take your tablets, laptop, phones and other stuff that connects to wifi. You then have to go and enter the new network details into each device to the new network and you put the details away and then find there's something else you haven't connected.

Wouldn't it be nice to take your own wifi connection with you.

So, I had a spare Raspberry Pi zero W version 2, broken camera port, a nice little case, a micro USB to large USB adaptor and some hot glue. All I needed was a second wifi adaptor.

I got this one from our friends at Amazon.

You don't have to get this one, I bought it and it works but feel free to try out any other ones.

First thing of course is to install Raspbian onto the Pi. I've gone for the lite 32 bit version. No desktop but it really isn't a problem. I'm not going through the setup process as if you can't do that you shouldn't do this. And all the config is done through ssh to the onboard wifi. Anything below that's in bold and italics is ok to copy and paste. 

Once you have finished setting it up, plug the adaptor in and you'll find like I did that although it's detected it doesn't work, yet.

The documentation with the adaptor gives you a link to the manufacturers website.

On the webpage it tells you to run the following.

 sh -c 'wget linux.brostrend.com/install -O /tmp/install && sh /tmp/install'

It takes a while and then it just works, you'll have the onboard adaptor of wlan0 and this one wlan2. 

Now we start doing other things and getting some software installed.

sudo apt install hostapd dnsmasq mc

I like mc, it's like Xtree Gold from the old dos days and is a great way to navigate the file system.

Now we run this.

sudo DEBIAN_FRONTEND=noninteractive apt install -y netfilter-persistent iptables-persistent

After that reboot with sudo reboot.

Once it's restarted more configuring files to get it all working.

sudo nano /etc/dhcpcd.conf

Paste all this in at the end.

interface wlan1

    static ip_address=192.168.4.1/24
    nohook wpa_supplicant

Save the files each time you finish, then onto.

sudo nano /etc/sysctl.d/routed-ap.conf

Paste this in and then save.

# Enable IPv4 routing
net.ipv4.ip_forward=1


Add a firewall rule with 
sudo iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE

Save the firewall rules with:

sudo netfilter-persistent save

And now setup DHCP and DNS stuff

sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.orig
sudo nano /etc/dnsmasq.conf

Add the following to the file and save it.

interface=wlan1
dhcp-range=192.168.4.2,192.168.4.20,255.255.255.0,24h
domain=wlan
address=/gw.wlan/192.168.4.1


Now we setup the access point software.

sudo nano /etc/hostapd/hostapd.conf

Paste this in, changing the country code to where you live, the ssid to the same as your home one if you want and the passphrase needs to be changed too.

country_code=UK

interface=wlan1

ssid=SSID

hw_mode=g

channel=7

macaddr_acl=0

auth_algs=1

ignore_broadcast_ssid=0

wpa=2

wpa_passphrase=PWD

wpa_key_mgmt=WPA-PSK

wpa_pairwise=TKIP

rsn_pairwise=CCMP

Now we run this to get it starting up when the pi boots.


sudo systemctl unmask hostapd

sudo systemctl enable hostapd

sudo systemctl start hostapd


Then give it one final reboot and you should see a new wifi connection pop up.

That's about it really.

Thanks to hackster.io for the initial guidance and Github for a little help in making it work.

You can use just about any Pi for this, seems to work quite well and I may even add  a connection to my vpn from it.

If you want to connect the Pi to a different wifi connection when you're away and travelling, boot it up, ssh into it and run 

sudo raspi-config



Select option 1

Then select S1 and enter the wifi connection details there. There may be a more elegant way like setting up a graphical desktop but this is nice and minimal and it works.

Here's some pictures of the complete thing with a few magnets attached to the back to make it easy to stick to things. And yes, I went overboard with the hot glue gun as I was going to make something that was waterproof first.

Hope this all makes sense to you and have fun.