Dieses Blog durchsuchen

Seiten

Labels

Raspberry (8) RPI (8)

Mittwoch, 31. Januar 2018

MPD auf dem Raspberry

All based on Raspberry Zero W with an "Debian Stretch" installed!

In meinem letzten blog hab ich eine Verbindung zwischen Amazon Echo und dem Raspberry Zero W hergestellt. Jetzt möchtich ich natürlich auch spachgesteuert meine Musik abspielen über eine Bluetooth Verbindung zwischen Rpi und Echo.



Installation of MPD



sudo apt-get install mpd mpc
sudo cp /etc/mpd.conf /etc/mpd.org
sudo pico /etc/mpd.conf


Ersetzte folgenden Abschnitt in der mpd.conf


audio_output {
        type            "alsa"
        name            "My ALSA Device"
#       device          "hw:0,0"        # optional
#       mixer_type      "hardware"      # optional
#       mixer_device    "default"       # optional
#       mixer_control   "PCM"           # optional
#       mixer_index     "0"             # optional
}

Durch


audio_output {
        type            "pulse"
        name            "MPD Pulse Output"
        server          "localhost"
}

Nun müssen wir den Service neu starten und die rechte setzten

sudo service mpd restart
sudo chgrp audio /var/lib/mpd/music
sudo chmod g+w /var/lib/mpd/music

Kopiert nun eure Musik nach

/var/lib/mpd/music

Nun über mpc die Datenbank aktualisieren


mpc --wait update



Beispiel einer playlist für den MPD


Pop.m3u 
Rio Reiser - König Von Deutschland.mp3
Red Hot Chili Peppers - Aeroplane.mp3
Stevie Nicks - Imperial Hotel.mp3
Laura Branigan - Self Control.mp3
Chaka Khan - Eye To Eye.mp3
Cyndi Lauper - Money Changes Everything.mp3
Herbert Grönemeyer - Alkohol.mp3
Fox The Fox - Precious Little Diamond.mp3
Melanie C - Northern Star.mp3
Silbermond - Nichts passiert.mp3
Frankie Goes To Hollywood - Relax.mp3
Michael Jackson - Earth Song.mp3

Wenn die Playliste erstellt wurde kann sie mit folgenden Kommandos benützt werden



mpc lsplaylists
mpc load Pop
mpc play 
 

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:]) 



Rasberry Zero W basic installation

All based on Raspberry Zero W with an "Debian Stretch" installed!

 
Here's an introduction on how to get your new Raspberry Zero W up and running without using a monitor.


Download the software we need:
  1. Download image „Raspbian Stretch Lite“
    https://www.raspberrypi.org/downloads/raspbian/
  2. Win32DiskImager
     
Now connect the SD card for the Raspberry to the computer, using a card reader and write the image to the SD card using Win32DiskImager.





Now we have to teach the written image that the Raspberry automatically connects to our Wifi network when booting and the SSH server is started. We can do that with two files in the boot partition.

1.) Create the file wpa_supplicant.conf on the drive named Boot and add the following content (Please change the SSID and PSK to the settings of your WIFI network).

wpa_supplicant.conf:
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
  ssid="Der ssid deines Wifi"
  psk="Dein geheimes Passwort"
  scan_ssid=1
}


2.) Create an empty file named ssh in the root of the partition.

After that it should look like this on the drive.


3.)
Now we insert the SD card into the RPI and provide it with power. After some time he should have automatically connected to the Wifi network and be found under the name raspberry. 
(This may take a few minutes for update the DNS)
You can then just test if everything works, by a ping of the DNS name raspberry:

ping raspberry


4.)  Access SSH over the standard User:
user: pi
password: raspberry


5.) After login over SSH we should start these commands

sudo apt-get update
sudo apt-get upgrade
sudo raspi-config

Now we configure our raspberry with assistance of  raspi-config :
  • at first set a new Password
  • set an new Hostname: Network Options ->Hostname
  • configure WIFI contry: Localisation Options-> Change Wi-fi Country
  • Expand SD-Card: Advanced Options->Expand Filesystem
  • Configure the memory: Advanced Options->Memory Split  16MB 
  • close Raspi-config and reboot.

So now your Raspberry should be ready,
have fun with your new gadget!

Matthias Zartmann