Raspberry Pi Bluetooth Speaker: Difference between revisions

From Alpine Linux
Line 10: Line 10:


=Getting the Speaker(s) Working=
=Getting the Speaker(s) Working=
The audio hardware can range from using the Raspberry Pi's on board audio with an external (active) travel speaker to wiring up an amplifier to some high-end passive speakers.
To get the best results, you'll need an dedicated audio add-on board and matching speakers. Higher end passive speakers need a proper amplifier (e.g. HiFiBerry Amp2 or IQaudIO IQaudIO DigiAMP+).


I'm testing this using Raspberry Pi's excellent IQaudio DigiAMP+ board with a pair bookshelf speakers and an old (active) travel speaker using the on board audio. It is possible to test by using headphones instead of active speakers.
I've used the whole range of IQaudio audio boards with different speakers and headphones. I'd also recommend a dedicated USB Bluetooth dongle (don't get the cheapest versions of these). It is possible to test by using the on-board Bluetooth and the on-board audio with headphones but the audio quality, owing to dropped packets, isn't great.


Once the speaker(s), and possibly amplifier, are wired into the Pi, it's time to install a fresh version of Alpine Linux.  The armv7 version from the [https://alpinelinux.org/downloads/ Downloads] page works on almost all Pis. This Wiki has several articles about installing Alpine on a Raspberry Pi.
Once the speakers and audio card and all connected to the Raspberry Pi, it's time to install a fresh version of Alpine Linux.  The armv7 version from the [https://alpinelinux.org/downloads/ Downloads] page works on almost all Pis. This Wiki has several articles about installing Alpine on a Raspberry Pi.


Enable writing to the boot media:
Enable writing to the boot media:

Revision as of 19:39, 4 August 2021

How To Build a Raspberry Pi Bluetooth Speaker

This articles describes how to build a Bluetooth speaker. This article is being actively written. Currently it is full of bugs but will provide some useful pointers.

Before You Start

You’ll need:

  • A Raspberry Pi
  • A Bluetooth USB dongle (if your Pi doesn’t have Bluetooth on board)
  • Sound card and speaker(s)

Getting the Speaker(s) Working

To get the best results, you'll need an dedicated audio add-on board and matching speakers. Higher end passive speakers need a proper amplifier (e.g. HiFiBerry Amp2 or IQaudIO IQaudIO DigiAMP+).

I've used the whole range of IQaudio audio boards with different speakers and headphones. I'd also recommend a dedicated USB Bluetooth dongle (don't get the cheapest versions of these). It is possible to test by using the on-board Bluetooth and the on-board audio with headphones but the audio quality, owing to dropped packets, isn't great.

Once the speakers and audio card and all connected to the Raspberry Pi, it's time to install a fresh version of Alpine Linux. The armv7 version from the Downloads page works on almost all Pis. This Wiki has several articles about installing Alpine on a Raspberry Pi.

Enable writing to the boot media:

 mount /media/mmcblk0p1 -o rw,remount

Then either enable the on board sound:

 echo "dtparam=audio=on" >> /media/mmcblk0p1/usercfg.txt

or your fancier sound card (e.g. IQaudIO):

 echo "dtoverlay=iqaudio-dacplus,unmute_amp" >> /media/mmcblk0p1/usercfg.txt

and then reboot your Pi.

Follow these instructions to enable ALSA. In summary

 apk add alsa-utils alsa-utils-doc alsa-lib alsaconf # the required software for sound
 aplay -l # should display a List of PLAYBACK Hardware Devices

In my case my list is:

 **** List of PLAYBACK Hardware Devices ****
 card 0: Headphones [bcm2835 Headphones], device 0: bcm2835 Headphones [bcm2835 Headphones]
   Subdevices: 8/8
   Subdevice #0: subdevice #0
   Subdevice #1: subdevice #1
   Subdevice #2: subdevice #2
   Subdevice #3: subdevice #3
   Subdevice #4: subdevice #4
   Subdevice #5: subdevice #5
   Subdevice #6: subdevice #6

Before you play anything from your speakers, I advise you to lower the volume first.

 amixer

displays a list of "simple controls"; for my headphones and the on-board sound, the output is this:

 Simple mixer control 'Headphone',0
   Capabilities: pvolume pvolume-joined pswitch pswitch-joined
   Playback channels: Mono
   Limits: Playback -10239 - 400
   Mono: Playback 0 [96%] [0.00dB] [on]

In this case there is only 1 control, 'Headphone', so I issue this command to lower the maximum volume to a comfortable level (50% in my case).

 amixer sset Headphone 50%

The IQaudIO DAC that I use has a much longer set of controls. I issued this command to set the volume:

 amixer sset 'Digital' 50 # quotes may be required if there are spaces in the control name

Note that there can be several interlinked controls, some of which are muted by defualt. ALSA (and other audio software on Linux) is notoriously under-documented, try `man amixer` for more information. Sometimes it is easier to use a more visual control to change the configuration:

 alsamixer

Finally, if you issue this command:

 speaker-test -t wav -c 2

Then you should hear "Front Left, Front Right" repeating from your chosen speakers. Now it's time to setup Bluetooth. Don't forget to save your changes (lbu commit) before moving on.

Bluetooth

I used Raspberry Pi 3 - Setting Up Bluetooth as a reference with some slight modifications as I am using a Pi 4.

Raspberry Pi 4

 apk add bluez
 btattach -B /dev/ttyAMA0 -P bcm -S 3000000 &
 # btattach -B /dev/ttyAMA0 -P bcm -S 115200 -N & # Pi 3 - not tested by me
 rc-service bluetooth start

edit /etc/mdev.conf and enable bluetooth. We're using `sed`, where s/#rpi bluetooth/rpi bluetooth/ means replace #rpi bluetooth with rpi bluetooth.

 sed -i 's/#rpi bluetooth/rpi bluetooth/' /etc/mdev.conf
 sed -i 's/#ttyAMA0         root:tty 660 @btattach -B \/dev\/$MDEV -P bcm -S 115200/ttyAMA0         root:tty 660 @btattach -B \/dev\/$MDEV -P bcm -S 3000000/' /etc/mdev.conf

Note: the last command uncomments the btattach command and changes it to work with the Pi 4.

Changes to /etc/bluetooth/main.conf

 Name = Pi-Bluetooth-Speaker # This is what you'll see when connecting 
 Class = 0x41C # Adding audio playback and recording to this bluetooth device
 DiscoverableTimeout = 0 # Always discoverable
 AlwaysPairable = true # Always pairable
 PairableTimeout = 0 # no timeime
 AutoEnable=true # will start when Linux 'sees' the bluetooth device at boot

Ensure that bluetooth is started at boot:

 rc-update add bluetooth

Bluetooth's state, including paired devices, in held in `/var/lib/bluetooth` so you'll need to add this to `lbu` state:

 lbu include /var/lib/bluetooth
 lbu commit && reboot

Device pairing:

 apk add python3 py3-dbus py3-gobject3

Getting this to work currently involves running the bluez-simple-agent after having edited it to always return sucessful. Note the comments below:

       def RequestPinCode(self, device):                                      
               print("RequestPinCode (%s)" % (device))                       
               set_trusted(device)                                          
               return #ask("Enter PIN Code: ")
         def RequestConfirmation(self, device, passkey):                      
               print("RequestConfirmation (%s, %06d)" % (device, passkey)) 
               #confirm = ask("Confirm passkey (yes/no): ")                
               #if (confirm == "yes"):                                      
               set_trusted(device)                                          
               return                                                       
               #raise Rejected("Passkey doesn't match")

And then running the revised agent in the background:

 bluez-simple-agent &

Notes: Pairing Agents in BlueZ stack Since Bluez 5.48, iPhones require pairing when connecting on a BLE GAP peripheral, why? Headless A2DP Audio Streaming on Raspbian Stretch

bluez-alsa

 vi /etc/apk/repositories

remove the comment from your community repository, mine is:

 http://uk.alpinelinux.org/alpine/v3.14/community

This is the final stretch. We've got bluetooth working and now we want to link bluetooth to the speakers

 apk add bluez-alsa
 bluealsa -p a2dp-source -p a2dp-sink &
 bluealsa-aplay &

Doing Something Title

Write the steps of how to do something in this section. If possible, show an example first, then tell your readers the steps. Break this procedure into separate procedures to avoid more than about 7-9 steps per procedure.

INSERT GREAT EXAMPLE HERE
  1. Answer the following three questions, at least in your head.
    1. What do I explain how to do?
    2. How do I do that?
    3. What do HOWTO readers already know about doing that?
  2. Write up a good example. This ensures you can do it.
  3. Write up the steps to do it.
  4. Write the background info/prerequisites readers need.
  5. Write the summary.
  6. Clean up your work.
  7. Revise, revise, revise.

See Also

Raspberry Pi's blog on How to play sound and make noise with your Raspberry P

There are lots of speaker and amplifier options: