Dieses Blog durchsuchen

Seiten

Labels

Raspberry (8) RPI (8)

Dienstag, 30. Januar 2018

Raspberry Bluetooth Pulseaudio and Alexa

All based on Raspberry Zero W with an "Debian Stretch" installed!
 
Hi, Now that I spent some nights connecting my Raspberry Zero W to the echo via Bluetooth, I decided to write this blog.

What do you need:
  • a Raspberry Pi Zero W
  • Amazon Echo
  • I hope only a fraction of the time I needed
I start with this introduction from the ground state after the installation. For the basic device I wrote this blog: Basic installation


Here we go!

1.) Bluetooth installation

It is important to assign the user groups. Unfortunately, this is missing in many descriptions in the INet.

sudo apt-get install bluetooth bluez bluez-tools bluez-firmware alsa-tools
sudo gpasswd -a pi bluetooth 
sudo gpasswd -a root bluetooth

2.) Pulseaudio installation

sudo apt-get install pulseaudio pulseaudio-module-bluetooth 
sudo gpasswd -a pi pulse-access
sudo gpasswd -a pi lp
sudo gpasswd -a pi pulse
sudo gpasswd -a pulse lp
sudo gpasswd -a pulse audio
sudo gpasswd -a pulse bluetooth
sudo gpasswd -a pulse input
sudo gpasswd -a root audio 
sudo gpasswd -a root pulse
sudo gpasswd -a root pulse-access
cd ~
wget -O pulse "https://docs.google.com/uc?export=download&id=1HpO5JHcY64g06Zig7PPijtBwRjfgVFHp"
sudo cp pulse /etc/init.d
sudo chmod +xwr,+xr,+xr /etc/init.d/pulse
sudo update-rc.d pulse defaults
sudo update-rc.d pulse enable

configure Pulse as service

sudo cp /etc/pulse/system.pa /etc/pulse/system.org
sudo pico /etc/pulse/system.pa

Now enter the following at the end of the file:
 
## --Ich start
### Automatically load driver modules for Bluetooth hardware
.ifexists module-bluetooth-policy.so
 load-module module-bluetooth-policy
.endif

.ifexists module-bluetooth-discover.so
  load-module module-bluetooth-discover
.endif

##wird benötigt damit man über TCP/IP auf Pulse zugreifen kann
load-module module-native-protocol-tcp auth-anonymous=1 auth-ip-acl=127.0.0.1
## --Ich ende 

3.) Now it's time

We start now the Pulseaudio service and see if everything is there.

 sudo service pulse start 
If it looks like that, everything is good :-)
Now we have to register our Raspberry on the Alexa, you should see your bluetooth controller in /var /lib/bluetooth/.
 If you want to give your controller a special name, edit  /var/lib/bluetooth/<Bluetooth MAC>/settings

In my case, I call my Bluetooth controller Hugo.

[General]
Name = Hugo

So now execute the following commands:
sudo service bluetooth restart
bluetoothctl

The following output should appear:


Now enter the following commands:

scan on
agent on

Start now on the phone the Alexa App and go into the Bluetooth device and then press "NEW DEVICE".
After some time bluetoothctl shows the following line:
 
The Mac address will be different for you, so replace it with your own in the following commands!



pair FC:A1:83:B5:8C:E7
trust FC:A1:83:B5:8C:E7
exit
 
The following should be displayed:


We are now creating the configuration of Pulse for the user PI.

cd ~
pico .asoundrc

Fügt folgenden Inhalt ein:

pcm.pulse {
    type pulse
}

ctl.pulse {
    type pulse
}

pcm.!default {
    type pulse
}

ctl.!default {
    type pulse
}

Reboot PI

sudo reboot

Now we can connect our Raspberry to the Alexa, now enter the following commands:

bluetoothctl
connect FC:A1:83:B5:8C:E7
exit

Now comes the final test:
sudo install mpg321
wget -O test.mp3 "https://docs.google.com/uc?export=download&id=1IEe2Teo87RYudCqigxXGCWpSiIFDVnGC"
mpg321 test.mp3

 
Important when bluetooth play the sound only 1-3 second, the problem is the UART driver.
You must disable it in /boot/config.txt with enable_uart=0. Edit or add this line at /boot/conig.txt: 



If everything is well you should hear something on your echo :-) 

 

Debugging Bluetooth helps you if somehow not work. 

For Bluetooth with the command:


sudo sed -i 's/bluetoothd/bluetoothd \-d/g' /lib/systemd/system/bluetooth.service
sudo systemctl daemon-reload


yodablue.py

The Bluetoothctl tool is not so handy for automation, while the other tools like bt-device on my Rpi not work i write a little python script to connect and disconnect from a device.

Please install the follow Python packages with Pip:

sudo apt-get install libdbus-1-dev
sudo apt-get install libdbus-glib-1-dev
sudo pip install dbus-python
sudo pip install bluetool
sudo apt-get install python-bluez



connect to device : python yodablue.py -c FC:A1:83:B5:8C:E7
disconnect from device : python yodablue.py -d FC:A1:83:B5:8C:E7

This is the code from yodablue.py to connect and disconnect :

#!/usr/bin/python
from bluetool import Bluetooth
import sys, getopt

def main(argv):

  bluetooth = Bluetooth()
  res = False
  if len(sys.argv) != 3:
     print 'yodablue.py -c |-d '
     sys.exit(2)
  if (sys.argv[1] =='-c'):
    print 'connect to',sys.argv[2]
    res = bluetooth.connect(sys.argv[2])
  if (sys.argv[1] =='-d'):
    print 'disconnect from',sys.argv[2]
    res = bluetooth.disconnect(sys.argv[2])
  if res == False:
    print 'fail'
    sys.exit(1)
  else:
    print 'OK'
    sys.exit(0)

if __name__ == "__main__":
  main(sys.argv[1:]) 



Keine Kommentare:

Kommentar veröffentlichen